Declarations of string variables

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>

using namespace std;

int main()
{
int num1;
  int num2 ;
char symbol;
    
     47 18 * 28;

cin >> num1 >> symbol >> num2;
    
cout << num1 << num2 << symbol  ;


	return 0;
unclear what you are asking.

char is NOT a string. char is generally just a 1-byte integer. C uses arrays of char as 'strings', c++ uses a wrapper for this called string.

you want

string s;
s = "47 18 * 28"; //the literal text assigned into the string. needs double quotes


note that num1 and num2 have no values assigned and will print junk (possibly, zeroed out, some compilers do this for you).

oh. I am going to venture a guess at what you want!

int num1 = 48;
int num2 = 18;
string symbol = "*";

cout << num1<<symbol<<num2<<"="<<num1*num2<<endl;
something like this?!

in the above case, where your symbol is a single character, it would also be ok to do this:

char s = '*'; //note the single quotes
and
cout << s << endl;//ok, this will write *

but that is ONLY useful for single characters which is uncommon in code.

Last edited on
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.
Last edited on
@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>
using namespace 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).
Last edited on
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.

Topic archived. No new replies allowed.