help with transfering from _getch() to string

so i have this.. and im trying to basically get a character from the user, 1 at a time and input it to a string. i am doing it this way so that the user still has the option to press the Escape key and have it jump back to another page. I have this so far and it seems right but i have no clue what i did wrong. it starts with asking the user to enter their name
(in the format lastname, firstname) *i specifically want the comma space*
followed by a do-while loop that inputs a char and outputs it on the screen, and then goes through a series of "if" statements to see if the user inputted certain keys. to exit this first loop, the user must input a comma, which then should begin the next loop using the same do-while loop, except this time to exit it they must push the Enter key... except when they enter the enter key it just returns to the beginning of the line and the cursor is on the C in Customers Name >:[
any idea what i did wrong?


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
string firstName, lastName;
    stringstream lastNameStr, firstNameStr;
    char input, input2;
    cout << "Customers Name: ";
    do
    {
        input = _getch();
        if(input==',')
            {
                cout << ",";
                break;
            }
        else if(input=='\n')
        {
            cout << endl << "Please input name in format Kovacs, Bradley";
            Customer();
            break;
        }
        else if(input==27)
            getMenuOption;
        else if(input=='a' | 'A' | 'b' | 'B' | 'c' | 'C' | 'd' | 'D' | 'e' | 'E' | 'f' | 'F' | 'g' | 'G' | 'h' | 'H' | 'i' | 'I' | 'j' | 'J' | 'k' | 'K' | 'l' | 'L' | 'm' | 'M' | 'n' | 'N' | 'o' | 'O' | 'p' | 'P' | 'q' | 'Q' | 'r' | 'R' | 's' | 'S' | 't' | 'T' | 'u' | 'U' | 'v' | 'V' | 'w' | 'W' | 'x' | 'X' | 'y' | 'Y' | 'z' | 'Z')
        {
            cout << input;
            lastNameStr << input;
            lastNameStr >> lastName;
        }
    }
    while(input != ',');
    do
    {
        input2 = _getch();
        if(input2==27)
            getMenuOption();
        else if(input2=='a' | 'A' | 'b' | 'B' | 'c' | 'C' | 'd' | 'D' | 'e' | 'E' | 'f' | 'F' | 'g' | 'G' | 'h' | 'H' | 'i' | 'I' | 'j' | 'J' | 'k' | 'K' | 'l' | 'L' | 'm' | 'M' | 'n' | 'N' | 'o' | 'O' | 'p' | 'P' | 'q' | 'Q' | 'r' | 'R' | 's' | 'S' | 't' | 'T' | 'u' | 'U' | 'v' | 'V' | 'w' | 'W' | 'x' | 'X' | 'y' | 'Y' | 'z' | 'Z')
        {
            cout << input2;
            firstNameStr >> input2;
            firstNameStr << firstNameStr;
        }
        else if(input2=='\n')
        break;
    }
    while(input2!='\n');
Rather than using __getch() to identify delimeters, then mixing that with cin to complete the read, why not just use __getch() in a loop to read the whole line yourself and forget about cin?

You can append to the string with:
1
2
3
4
{
    char s[2] = { input, '\0' };
    str += s;
}
What do you mean, I'm not using cin?
Also line 34 probably does not do what you think it does (as it is, it will always return true). Try isalpha() instead.
Sorry, your right, you're not using cin. My code above shows how to append that character to a string.

It's conventional to use a switch statement to handle states. As Zhuge has pointed out, your comparison is wrong.
I just tried your way, it didn't work :(
What didn't work?
Topic archived. No new replies allowed.