Really Really could use help please!!

Hello everyone, I am having a problem with this project at the moment so far i have this. I would be very thankful if someone can help me figure out what i am doing wrong and how i can finish writing this code getting so confused. :S

The assignment is:

You are to write a program to accept a single entry (with spaces) of a person's name in the form of; Mr. John Smith
Then out put the following;
4 letters in the first name (John)
5 letters in the last name (Smith)
The program should ask if you would like to process another student (loop).


The code so far is:

#include
<iostream>

using
namespace std;

int
main ()

{

string name;

string first;

string last;

int
firspace=0;

int
secspace=0;

char
runagain;

do

{

cout<< name;

cin.getline (cin, name);

firspace=name.find(
" ",0);

secspace=name.find(
" ",firspace+1);

first=name.substr(firspace+1, secspace-firspace+1);

last=name.substr(secspace+1);

cout<<
"First name: "<<first"\n\n";

cout<<
"Last name: "<<last"\n\n";

cout<< "Run Again? Y or N ";

cin>> runagain;

runagain=toupper(runagain);

if( runagain!='Y'&&runagain!='N')

{

cout<< "Invaild. Run Again? Y or N ";

cin>> runagain;

runagain=toupper(runagain);

}

}

while(runagain=='Y');

system(
"pause");

return 0;

}
Could you use the code tags to format your code so that it is more readable?
Here is a version that should work (I tried it on Xcode)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
int main(int argc, const char *argv[])
{
    
    const int MAX_NAME_LENGTH = 4;
    const int MAX_FAMILY_LENGTH = 5;
    string name;
    string first;
    string last;
    size_t firspace = 0;
    size_t secspace = 0;
    char runagain;
    
    do {
      
        // Assumes 3 entries separated by a space. 
        // Doesn't check if entries are incomplete or more than one space is entered.
        
        cout << "Enter Title, First and Last Name: ";
        getline (cin,name);
        
        firspace=name.find(" ",0);
        secspace=name.find(" ",firspace+1);
     
        if ((secspace-1) - (firspace+1) >= MAX_NAME_LENGTH)
            first = name.substr(firspace+1, MAX_NAME_LENGTH);
        else
            first = name.substr(firspace+1, secspace-(firspace+1));
        
        last = name.substr(secspace+1,MAX_FAMILY_LENGTH);
        
        cout << "\n\nFirst name: " << first << "\n";
        cout << "Last name: " << last << "\n\n";
        
        do {
            cout << "Run again (Y/N)? ";
            cin >> runagain;
            cin.ignore(); // need this to flush the buffer for the getline().
            cin.clear();
            runagain = toupper(runagain);
        } while ((runagain != 'Y') && (runagain != 'N'));
        
    } while (runagain == 'Y');
    
    //system("pause");
    
    return 0;
    
}
Last edited on
Topic archived. No new replies allowed.