cin input errors

Started by
6 comments, last by C_Programmer0101 18 years, 4 months ago
Hello community, I posted this a while back, but could not get a valid answer so I am hoping someone can help. I have all my classes made and in my main function I was to get the user to input his/her choices. I have an char array[12] and the user is asked to input a word in the array. Lets say they input a numeric value of 1. Being it is called into my constuctor to set the values c1(array, x, y, z); char arrays seem to accept values and will return that value without crashing. I would like to know how to prevent or make the program change the value or character to the proper choice presented to them.


//for example

  cout<<"-----------Choose your weapon----------\n";
  cout<<"\n 1. staff";
  cout<<"\n 2. sword";
  cout<<"\n 3. Broom Stick";
  cin>>choice;
 
//For the first example lets assume choice is a char array
//it will be inserted into a constructor(char*)
//How can I switch 1 if entered to print staff

//For the last example lets assume choice is a int
//it will be inserted into a constructor(int)
//How can I switch the word staff if entered to print 1 to the screen



I hope you understand what I am trying to do?
Computers are worlds of Exploration?
Advertisement
Are you asking how can you make it so either 'staff' or '1' could be entered and the correct option would be selected?

Dave
If choice was a char array of twelve, for example char choice[12]; and I asked the user to input the name of their choice. The user decides to choose staff and enters 1. Remember choice is a character array and I want to print the users weapon name to the screen so I will need to convert that 1 into staff to print to the screen.

If choice was an integer, for example: int choice; and I wanted the user to input the number next to the name of the weapon, the user inputs staff. How can I prevent the program from crashing and have the computer convert staff to print 1 to the screen.

This is just to learn how to convert proper results when the user inputs the wrong thing and to print out what the user should have input.
Computers are worlds of Exploration?
You could do:

char myChar;cin.get( myChar );


And make the number the only option, ie no words. To be honest noone is going to enter the name of the option if they can just enter the number. Since the user could still enter the number you could do a range check and see if it is a number, if not chuck a message back at them.

Hope that helps,

Dave
Hypothecally speaking, what if a person did type the name is what I am asking to prevent it.

Realistically, a person could accidently type a character s when they should have input a number so I am back to my first question.
Computers are worlds of Exploration?
I think you should write a parser. It sounds like you're always getting a char array back (choice) so you need to go through the string entered and if the array is equal to a "1","2","3","staff", etc run the code for that.

example:

// stricmp returns 0 if they're the same
if( stricmp( "1", choice ) == 0 )
{
// Run code for the choice = 1
}
else if( stricmp( "2", choice ) == 0 )
{
// Run code for the choice = 2
}
...
else // the choice doesn't exist
{
// Run code for the choice = user being stupid
}

ps: if you're running vs2005 stricmp has been replaced with _stricmp

Luck and muffins
Quote:Original post by C_Programmer0101
Realistically, a person could accidently type a character s when they should have input a number so I am back to my first question.


Like i said in my previous post you would range check the character.

To solve the actual problem at hand you use atoi() to convert the string to an integer an if the string cannot be converted then zero is returned. This means that you can't have a menu option of zero.

So once you have attempted to convert the string to an integer and if it fails, and the string is only one char long, you can then switch on it for the menu options.

Although this is fiddle and i would probably stick to just the numbers.

Hope that helps mate,

Dave
Thanks alot guys for your input and I will give it a try tonight and post my problems tommorrow.

-Thanks
Computers are worlds of Exploration?

This topic is closed to new replies.

Advertisement