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.
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
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
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.