How can I use variable to input a word (bunch of alphabet letter) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can I use variable to input a word (bunch of alphabet letter)

Hi! I want to ask. ━━━━━━━━━━━━━━━━━ int main () { int m, s, h, e, i ,q, t, z ,k; int name; cout << "Name : "; cin >> name; cout << "Math : "; cin >> m; ━━━━━━━━━━━━━━━━━ When I input a word in name variable (ex : Adam) it comes to error so i can't input the next variable type. What variable should I used to replace (int) so I can input a word (real word, not number) Thank You! Your help will fully appreciated.

31st Dec 2021, 1:21 AM
Mahdy N.
Mahdy N. - avatar
9 Answers
+ 8
"name" variable in your program is of integer type, and the name you are going to enter would be a string ( collection of characters ) You can either use an array of characters to store that name char name [10] std::cin >> name Or More preferably use "string" class defined in c++'s standard library. // Make sure to include <string> header before std::string name; std::cin >> name;
31st Dec 2021, 1:44 AM
Arsenic
Arsenic - avatar
+ 3
You tagged css, but this is obviously not css. Pls correct the tag for the actual language. This is needed for you to get a correct answer.
31st Dec 2021, 1:25 AM
Emerson Prado
Emerson Prado - avatar
+ 3
Besides following Arsenic correct answer, did you do the string lessons of the C++ course?
31st Dec 2021, 1:53 AM
Emerson Prado
Emerson Prado - avatar
+ 1
Alright I just realized if this was c++, thanks for reminding Emerson Prado , can you please help me with my question?
31st Dec 2021, 1:27 AM
Mahdy N.
Mahdy N. - avatar
+ 1
Oh I see, let me try that on my code, Thank you very much! Arsenic
31st Dec 2021, 1:45 AM
Mahdy N.
Mahdy N. - avatar
+ 1
No, i learn until if else conditional. I didn't yet learn string lessons.
31st Dec 2021, 1:55 AM
Mahdy N.
Mahdy N. - avatar
+ 1
Mahdy N. That is a basic lessons on string
1st Jan 2022, 10:43 AM
shlee_
shlee_ - avatar
+ 1
Mahdy N. You can also use getline for getting input in a string Ex: getline (cin, name)
1st Jan 2022, 10:44 AM
shlee_
shlee_ - avatar
+ 1
Thank you ! shlee_
2nd Jan 2022, 11:07 AM
Mahdy N.
Mahdy N. - avatar