Hi all, I'm quite new at this, only a few weeks in!
I'm trying to write a code for finding out how string variable declarations work, but am not sure how to get the line of numbers to be assigned to the string variables below it. What am I missing?
Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int num1;
int num2 ;
char symbol;
47 18 * 28;
cin >> num1 >> symbol >> num2;
cout << num1 << num2 << symbol ;
return 0;
This is exactly what I'm struggling with, I don't have the terms to explain the code I can't create!
It is based off of an example in a textbook. It gives that string of numbers as an input line, along with the cin line to execute. Then it asks how each variable is assigned given that input line. I know the outcome, but have failed to come up with a code to replicate it.
@scottpj, I think you are a little confused, and your terminology "a string of numbers" is going to cause havoc on a programming website!
A "string" is a particular type of variable in c++. (It's actually a sequence of characters.) Best to avoid its common-English usage in a programming-related topic.
If your input line (from the keyboard) is known to contain two integers, followed by a single non-whitespace character, followed by another integer then you can stream it in (operator >> ) with the following. Note that, by default, the stream input treats blank spaces as a separator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
usingnamespace std;
int main()
{
char symbol;
int a, b, c;
cout << "Enter a line with two integers, followed by a symbol, followed by another integer:\n";
cin >> a >> b >> symbol >> c;
cout << "This is what you entered\n";
cout << "a = " << a << '\n'; // '\n' will just give you a newline
cout << "b = " << b << '\n';
cout << "symbol = " << symbol << '\n';
cout << "c = " << c << '\n';
}
Enter a line with two integers, followed by a symbol, followed by another integer:
47 18 * 28
This is what you entered
a = 47
b = 18
symbol = *
c = 28
Did you know that this website has quite a brilliant tutorial: http://www.cplusplus.com/doc/tutorial/
which is well worth working through (several times).
you may need to type the code or textbook question for us to see it too. Otherwise we are guessing a lot at what you need to see.
all I can offer right now is to review what we said and ask questions about anything you did not understand that we said, or, ask a different question if you understand what we said and it didnt actually apply to your need.