Getting User Input | C++ | Tutorial 10

preview_player
Показать описание

Throughout the course we'll be looking at various topics including variables, arrays, getting user input, loops, conditionals, object orientation, and much more.
Рекомендации по теме
Комментарии
Автор

SOURCE CODE

#include <iostream>
#include <string>
using namespace std;

int main() {

int age;
cout << "Enter your age: ";
cin >> age;
cin.ignore(256, '\n');

string name;
cout << "Enter your name: ";
getline(cin, name);

cout << "Hello " << name << " you are " << age << " years old.\n";

return 0;
}

martijnholleboom
Автор

Just a heads up to everyone 'getLine()' doesn't work for, at the top be sure to put in #include <string>

Your code should then work :)

RekAkATom
Автор

I found out that getline() won't work after some inputs, for example cin before getline(), but if I put cin.ignore before getline(), that would mean put it between cin and getline(), then it works, can someone explain tell me the reason why this happened?

twiggy
Автор

If I enter string or char if variable is int the program crashes how to do that?

JagdishOnYT
Автор

id make sure you tell people to include the string thingy because the getline wont work if u dont

JohnSmith-lrxg
Автор

what if i want from the user the number of hours what i should do?

AYA-jii
Автор

The getline doesn't that way, you need to put cin.ignore() before.

vladyslavkorenyak
Автор

What if I need this in a Word document and not in a separate window?

diakritika
Автор

Amazing Video Bro! .. May god bless you!!

kalimudinansari
Автор

Thx i got a perfect score today because of this video!

marbledotanks
Автор

I am using cin instead of getline and it is working for me ..

#include <iostream>

using namespace std;

int main()
{
string name;

cout << "Enter your name: " << endl;
cin >> name;

cout << "Your name is " << name <<"." << endl;


return 0;
}

sareer