+ 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.
9 Antworten
+ 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;
+ 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.
+ 3
Besides following Arsenic correct answer, did you do the string lessons of the C++ course?
+ 1
Alright I just realized if this was c++, thanks for reminding Emerson Prado , can you please help me with my question?
+ 1
Oh I see, let me try that on my code, Thank you very much! Arsenic
+ 1
No, i learn until if else conditional. I didn't yet learn string lessons.
+ 1
Mahdy N. That is a basic lessons on string
+ 1
Mahdy N. You can also use getline for getting input in a string
Ex: getline (cin, name)
+ 1
Thank you ! shlee_