Can’t use variables | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can’t use variables

So basicly when writing c++ code in code blocks, I try to use variables and it doesn’t work. I’m not sure why and any help would be great. Here is the code I have #include <iostream> using namespace std; int main() { int thevariable = 22; cout << “thevariable”; return 0; } Ok so thats the code, in console after i run it and build, it only says “thevariable” and not “22”, hopefuly this helps.

29th May 2019, 6:27 AM
Skyler
2 Answers
+ 11
Remove the double quotes(" ")from the cout line. cout<<thevariable; ------- When you type the variable name enclosed in double quotes, the compiler thinks of it as a string literal and displays it as is. When you just type the variable name (without the quotes), it displays the value of the variable.
29th May 2019, 6:31 AM
Nikhil
Nikhil - avatar
+ 1
ahhh, thanks so much. I was so confused.
29th May 2019, 6:51 AM
Skyler