Grabbing different type variables

So, I'm not sure how to really go about this. Basically the user is going to enter 2 different pieces of data. A character and a number, think of it as a grade. For example.

1
2
3
4
  cout << "How many classes are you taking?: ";
  cin >> numClasses //lets say user enters 4
  cout << "Enter the letter and grade" << endl;
  // The format of input would be (ex. A 95) 


We're going to assume that they enter only the one character and just any digit number. How do I go about grabbing the 'A' and storing it into a char variable and also grabbing the 95 and storing it into a int variable? We aren't using strings in this case. We have to loop grabbing the letter and grade x times (depending on what they input)

What I thought about doing was something along the lines of:
1
2
3
4
5
6
7
  char letterGrade, garbage;
  int gradePerc;
  do
  {
    cin >> letterGrade >> garbage >> gradePerc;
    count++;
  } while (count <= numClasses):


The idea was to take in the whole line and use the "garbage" variable to "Eat" the space. However I'm not entirely sure how to do this correctly because I can enter in for example B 86 but it won't let me loop for however many times that I need. For example, if I say like 7 classes, it will let me enter like 2 grades then stop (this is with a do-while loop)

All help is appreciated.
Last edited on
You don't need to do anything special to skip the space. Just do:

 
    cin >> letterGrade >> gradePerc;

@dutch welp that fixed it, i feel dumb now lmao, thank you
Hello gradePerc,

Since you asked for all help.

Your first bit of posted code has problems.

Line 2 is missing the semi-colon.

The comment on line 4 is nice, but should be part of the prompt. Playing with the program I did this:
std::cout << "\n Enter the letter and grade (A 95) for class " << count + 1 << ": ";
The "\n " at the beginning of the string is optional, it makes it easier for me to read the screen. The end of the string, (for class) and the "count + 1" is just a help and is also optional. The "(A 95)" is what you really need. Even with that you are likely to get some people enter the letter and pressing enter. I would consider making each variable a separate input with its own prompt.

As dutch pointed out and in case you did not know. cin >> letterGrade is formatted input which means that the keyboard input goes into the input buffer until you press "Enter". When "Enter" is pressed the program will extract from the input buffer up to a white space or new line whichever comes first. So as dutch is saying cin >> letterGrade will extract up to and including the space then disgacd the space leaving the pointer at what is next and putting the letter into "letterGrade".

Now when formatted input it to a numeric variable "cin" is expecting a number. So anything other than a number will put "cin" into a failed state that would need to be dealt with. This failure can be caused by enter anything that is not considered a number like: a letter, punctuation character, including a decimal point as in .125. This will set one or more of the state bits making "cin" unusable the rest of the program unless fixed.

In the do/while loop you should include the prompt. otherwise the output to the screen would look like this:

 How many classes are you taking?: 2

 // The comment in the code is nice, but should be included in the prompt.
 Enter the letter and grade: a 95  // First run through the loop.
 //< cursor is here. What to enter. No idea.

 b 85  // second time through loop.
 //< cursor is here. What to enter. No idea.

 c 70  // Third time through loop, but only wanted 2.


The code you have posted and when run leaves a lot of guess work.

Be careful when using "<=". In the while condition it causes it to loop three times. One more than it should. Unless you initialized "count" to (1) and it does not show in the code posted.

Hope that helps,

Andy
Topic archived. No new replies allowed.