0
Cheer creater in C++
Can someone please help me with this code. Why am I getting shh when the input is 5? https://code.sololearn.com/c8ARhwvCRB9S/?ref=app
4 Answers
+ 2
// try this
int x;
cin>>x;
if(x<1) cout<<"shh";
else if(x>10) cout<<"High Five";
else for(int i=0;i<x;i++) cout<<"Ra!";
+ 3
When I entered 5 it printed:
a
which came from
else
cout << "a" ;
Notice the "a" is in quotes, so it is a literal string, not a variable name.
Plus there was a warning about line 12,
a = 'Ra!' * n ;
In C++, please understand that strings do not multiply like they do in Python syntax. You will have to use something else, like a loop, to repeat the string. Also unlike Python, C++ distinguishes between " (double quote) for strings and ' (single quote) for single characters. That is what the warning is about -- using multiple characters inside a single-character quotation.
+ 2
SoloProg thank you. I didn't think about using for loop. Now it works!
+ 1
Brian ah right! Thank you. Now I understand what was wrong.