3 Replies - 1336 Views - Last Post: 28 April 2011 - 08:00 AM Rate Topic: -----

#1 vease1999  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 10
  • Joined: 31-July 10

Limiting user input to one character

Posted 28 April 2011 - 04:52 AM

All I'm trying to do is limit user input to a single character and output a message error if they enter more. Right now, I have an error that spits out if they enter an incorrect response (not one of the designated options), but if they enter more than one character (let's say 20, and one of the 20 is a valid option), it will take the valid option and move to the next question. I'm pretty sure that 'cin' is not the right way to go about this, but i'm not sure which route to take. Here's an example of my trouble:

int main()
{
    char answer;
    double correct = 0;
    double incorrect = 0;
    const int TOTAL = 6;
    double grade;
    
    cout << "1. Who was the first president of the U.S. " << endl;
    cout << endl;
    cout << "(A). Thomas Jefferson" << endl;
    cout << "(B)/>. Andrew Jackson" << endl;
    cout << "(C). George Washington" << endl;
    cout << "(D). George Bush" << endl;
    cout << "(E). Bill Clinton" << endl;
    cout << "(F). Chester A. Arthur" << endl;
    cin >> answer;
    answer = toupper(answer);
    
    
    
    while (answer != 'A' & answer != 'B' & answer != 'C' & answer != 'D' & answer != 'E' & answer != 'F'){
          cout << "Please enter a valid answer. " << endl;
          cin >> answer;
          answer = toupper(answer);
          }
    if (answer == 'C'){
               correct++;
               }
               else{
               incorrect++;
               }


Also, if you see anything else that I could do to make the code more efficient, that would appreciated also. Thanks for the help.

Is This A Good Question/Topic? 0
  • +

Replies To: Limiting user input to one character

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Limiting user input to one character

Posted 28 April 2011 - 05:45 AM

I'd go by accepting a std::string instead of a char, as input:
std::string answer;
std::cin >> answer;

if (answer.length() != 1)
{
   // answer holds more than one character, wrong input then.
   // handle error as you wish
}
else
{
   // answer holds only one character, held in answer[0]
   char ans = answer[0];
   // check if ans is A, B, or whatever ...
}


Hope you get it.
Was This Post Helpful? 0
  • +
  • -

#3 Keerigan  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 55
  • Joined: 04-February 10

Re: Limiting user input to one character

Posted 28 April 2011 - 06:00 AM

if you make answer a char, then use
 cin.get(answer) 

answer will equal the first character that is entered.

For efficiency, use a switch statement
switch(answer)
{
case('a'):
    // enter code
    // enter code
    break;
}

Was This Post Helpful? 0
  • +
  • -

#4 vease1999  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 10
  • Joined: 31-July 10

Re: Limiting user input to one character

Posted 28 April 2011 - 08:00 AM

Thanks for the response. The cin.get worked. I did consider the switch statement before I started, but just never followed through with it. Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1