Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Char Int problem

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I'm working on a program I just can't get to work. Here's the deal. It asks the user for a grade and when the user types in "*" the program ends and displays stuff. The problem is that if the cin for the grade is type int that the * will make it go crazy. But if it is type char than it can't deal with 2 or 3 digit numbers. Is there a way to make this program work? Thanks a lot.
 
Unfortunately, I don't know what you mean by grade, and I don't know what subjects you're grading, so my solutions may not be worth much to you, but here are two things you might try.

1. Have you considered using any other character other than "*" such as "-" or"." or "!" or "A".
2. What's wrong with (0) zero. It's an int.
 
I agree with bsisko, just use another int for your sentinal value...something that would never be used in grades....such as '999'. Something else you might be able to do is use cin.getline() function to get a string. Then you could covert your character number into an int value. This way you could use ANY value from the keyboard and it would not crash your program.

To use the cin.getline function you have to declare an array of characters..such as char grade[3]; Then you use cin.getline() to get a string from the user. C++ should have some good help on cin.getline(). Hope I didn't confuse you more.

Niky Williams
NTS Marketing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top