0

How can I get the user to input text instead of numbers in this program. haw can i get the cin statement to accept text? Do i have to use char?

int main()
{
    using namespace std;
    int x = 5;
    int y = 8;
    int z;
    cout << "X=" << x << endl;
    cout << "Y=" << y << endl;
    cout << "Do you want to add these numbers?" << endl;
    int a;
    cin >> a;
    if (a == 1) {
        z = add(x, y);
    }
    if (a == 0) {
        cout << "Okay" << endl;
        return  0;
    }
    cout << x << "+" << y << "=" << z << endl;
    return 0;
}

---EDIT--- Why won't this work?

int main()
{
    using namespace std;
    int x = 5;
    int y = 8;
    int z;
    cout << "X = " << x << " Y = " << y << endl;
    string text;
    cout << "Do you want to add these numbers together?" << endl;
    cin >> text;
    switch (text) {
        case yes:
            z = add(x, y);
            break;
        case no: cout << "Okay" << endl;
        default:cout << "Please enter yes or no in lower case letters" << endl;
            break;
}
    return 0;
}

Thank you everybody! If you're interested, you can check out the game I made here. http://pastebin.com/pmCEJU8E You are helping a young programmer accomplish his dreams.

Lemonizer
  • 83
  • 2
  • 4
  • 14
  • Please remember to tag your questions with the relevant programming language(s). – Matt Ball Mar 02 '13 at 15:51
  • You should probably get yourself a decent introductory book - there are a bunch in [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Mar 02 '13 at 15:58
  • Look up the correct usage and rules that apply to the `switch` statement -- for example, here: http://www.tutorialspoint.com/cplusplus/cpp_switch_statement.htm – Josh Greifer Mar 02 '13 at 17:11

3 Answers3

2

You can use std::string for that purpose. Remeber that cin reads your text until white space. If you want to read whole line use getline function from the same library.

Fuv
  • 922
  • 2
  • 12
  • 25
0

You can use std::string.

std::string str;
std::cin>>str; //read a string 

// to read a whole line
std::getline(stdin, str);
P.P
  • 117,907
  • 20
  • 175
  • 238
0

Since you are concerned with only 1 character response from the user, as in Do you want to add these numbers? might be concatenated by a (Y/N), you should (in my opinion) use getchar() function if you have intentions to read only 1 character. This is how I would do for an error prone 1 character input handling:

bool terminate = false;
char choice;
while(terminate == false){
    cout << "X=" << x << endl;
    cout << "Y=" << y << endl;
    cout << "Do you want to add these numbers?" << endl;

    fflush(stdin);
    choice = getchar();
    switch(choice){
    case 'Y':
    case 'y':
        //do stuff
        terminate = true;
        break;
    case 'N':
    case 'n':
        //do stuff
        terminate = true;
        break;
    default:
        cout << "Wrong input!" << endl;
        break;
    }
}

As a reply to your edit

That doesn't work because you cannot pass std::string as an argument to the switch. As I told you, you should read just one character for that purpose. If you insist on using strings, do not use switch, rather go for if else blocks by using the string comparator ==.

cin >> text;
if(text == "yes"){
    z = add(x, y);
}
else if(text == "no")
{
    cout << "Okay" << endl;
}
else
{
    cout << "Please enter yes or no in lower case letters" << endl;
}
Varaquilex
  • 3,447
  • 7
  • 40
  • 60