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

23rd Feb 2022, 6:16 PM
Kruti
Kruti - avatar
4 odpowiedzi
+ 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!";
23rd Feb 2022, 10:07 PM
SoloProg
SoloProg - avatar
+ 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.
23rd Feb 2022, 11:15 PM
Brian
Brian - avatar
+ 2
SoloProg thank you. I didn't think about using for loop. Now it works!
24th Feb 2022, 1:54 AM
Kruti
Kruti - avatar
+ 1
Brian ah right! Thank you. Now I understand what was wrong.
24th Feb 2022, 1:53 AM
Kruti
Kruti - avatar