Friday, 13 September 2013

Beginner's C++ program for a class

Beginner's C++ program for a class

the assignment is the basic "cin a full name" and then "retrieve First
Middle Last" bit, where you create a program that asks the user to type in
their full first name into a single string and the programs picks apart
the name and outputs it organized seperately. this is what i wrote:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
int index;
index = name.find(' ');
cin >> name;
cout << "First name: " << name.substr(0, index) << endl;
name = name.substr(index+1, name.length()-1);
index = name.find(' ');
cout << "Middle Name: " << name.substr(1, index) << endl;
name = name.substr(index+1, name.length()-1);
cout << "Last name: " << name;
return 0;
}
the code just wont seperate them right, and will not redefine 'name'
correctly. It always just bounces back to the beginning of the string. any
help for a newbie? here's an example output: Teenage Wonder Land First
name: Teenage Middle Name: eenag Last name: Teena Process returned 0 (0x0)
execution time : 7.942 s Press any key to continue.

No comments:

Post a Comment