Question regarding assigning inputs to different data types

Hello all,
This is my first post.
I am taking an online summer beginning c++ course.
We are using C++ programming from problem analysis to program design 7th edition by malik.
I understand some basic things with c++ such as how to write a program that prompts users to enter test score data and obtain the average of these test scores. I have learned about different data types, assignment statements, how to assign variables etc. some pretty basic things.
I just learned about the get, putback, peek etc functions.
There is an exercise at the end of chapter 3 (which I just read) that I do not understand. I am hoping someone can shed some light on it or at least have a discussion with me and point me in the right direction.

Heres the assignment:
Suppose num1 and num2 are int variables and symbol is a char variable. Consider the following input: (2)
47 18 * 28 $
what value (if any) is assigned to num1,num2 and symbol after each of the following statements executes? (use the same input for each statement.)
a. cin>>num1>>symbol>>num2;
b. cin>>symbol>>num1>>num2;
c. cin>>num1;
cin.get(symbol);
cin>>num2;
d. cin>>num1>>num2;
cin.get(symbol);
e. cin.get(symbol);
cin>>num1>>num2;


That is the assignment.

What I get out of what it is asking:
First of all I understand that the input is 5 things, some are int values and some are symbols (the * and $)
for a: so num1's value would be 47 and then the program has an input failure because the next thing that is entered is not a symbol? is this the correct way to think about it?
for b: the program fails right off the bat because the first thing we input is not a symbol it is a number?
for c: num1=47
symbol= (blank)
num2=18?
really not sure about this one..
for d: num1=47
num2=18
symbol=*
for e: program fails because first character the 4 is not a symbol?

anyway if someone could steer me in the right direction that would be great.
Thanks and let me know if the formatting of my question is correct or if i could write more/less to make it clear since I know nothing about programming language as I am just starting this course.

Cheers,

William
There are two things that you need to remember. First, the extraction operators (<<) skip leading white space. The get() member does not. So:
a. cin>>num1>>symbol>>num2; This reads 18 into num1, then skips white space. Then reads '1' into symbol. There is no whitespace after the '1' to skip, so it then reads 8 into num2.
b. cin>>symbol>>num1>>num2; This reads 4 into num1, then '7' into symbol, then 18 into num2.
c.
1
2
3
cin>>num1;
cin.get(symbol);
cin>>num2;
This reads 47 into num7, then doesn't skip whitespace and reads ' ' into symbol. Then it reads 18 into num2

etc.
Ok I believe I see what you are saying,
Wouldn’t it be true for A that num1 is assigned to 47? Because why would you skip a white space if 47 is the first inputed value?
Then you skip a white space and symbol would be 1 and num 2 would be 8.
I was not aware that a symbol could be considered a 1.

So for B it seems flip-flopped to me, shouldn’t 4 be assigned to symbol and 7 be assigned to num1? Then I agree with 18 for num 2

C makes sense to me
Hello cookiemnstr510,

If you have not tried this try this code to see what happens:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <limits>

int main()
{
	int num1{ 0 }, num2{ 0 };
	char symbol{ ' ' };

	std::cin >> num1 >> symbol >> num2;
	std::cout << "\na. " << num1 << " " << symbol << " " << num2 << "\n" << std::endl;
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

	std::cin >> symbol >> num1 >> num2;
	std::cout << "\nb. " << symbol << " " << num1 << " " << num2 << "\n" << std::endl;
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

	std::cin >> num1;
	std::cin.get(symbol);
	std::cin >> num2;
	std::cout << "\nc " << num1 << " " << symbol << " " << num2 << "\n" << std::endl;
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

	std::cin >> num1 >> num2;
	std::cin.get(symbol);
	std::cout << "\nd. " << num1 << " " << num2 << " " << symbol << "\n" << std::endl;
	std::cout << "The charaacter after 18 is a space. The character after that is: " << static_cast<char>(std::cin.peek()) << std::endl;
	std::cout << "This demonstrtes the use of \"std::cin.get()\". You can also see the same in c.\n" << std::endl;
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

	std::cin.get(symbol);
	std::cin >> num1 >> num2;
	std::cout << "\ne. " << symbol << " " << num1 << " " << num2 << "\n" << std::endl;

	return 0;
}  //  End of main 


Hope that helps,

Andy

Edit: updated program
Last edited on
My mistake on 'a'. You are correct that it would read 47, '1' and 8
I was not aware that a symbol could be considered a 1.

"symbol" is just the name of the variable. There is no such thing as a symbol in C++. It could just as easily have been called joseph. What matters is that the variable's type is char.
So for B it seems flip-flopped to me

Dang. You're right again. My mistake was that I was reading the output of my test program rather than paying attention to the order of the input.

Sorry about mistakes :(
Topic archived. No new replies allowed.