#include <iostream>
usingnamespace std;
int main()
{
cout << "Hello everyone, this is C++ language." << endl;
int a = 250;
int b = 250;
cout << "250 + 250 =" << endl;
int price = a + b;
cout << "Final result is \b " << price << endl;
int c;
int d;
int e;
cout << "What is your name?" << endl;
cin >> c;
cout << "Where are you from?" << endl;
cin >> d;
cout << "How old are you?" << endl;
cin >> e;
cout << "My name is" << c << "." << endl;
cout << "I am from" << d << "." << endl;
cout << "I am" << e << "\n years old." << endl;
return 0;
}
Line 16: c is an int. How many people do you know with a number for a name?
If you try to enter alphas in response to the cin, that cin and subsequent cins will fail. .
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
cout << "Hello everyone, this is C++ language." << endl;
int a = 250;
int b = 250;
cout << "250 + 250 =" << endl;
int price = a + b;
cout << "Final result is \b " << price << endl;
string c,d,e;
cout << "What is your name?" << endl;
cin >> c;
cout << "Where are you from?" << endl;
cin >> d;
cout << "How old are you?" << endl;
cin >> e;
cout << "My name is " << c << "." << endl;
cout << "I am from " << d << "." << endl;
cout << "I am " << e << " years old." << endl;
return 0;
}
This should work for you ...
change the type of c,d,e to string will solve this problem
@DCisthebest I have only read about it and never experienced it before. so i don't know if it correct or if it would work but try this:
1 2 3 4 5 6 7 8 9
cout << "How old are you?" << endl;
cin >> e;
cin.ignore();///add this
cout << "My name is " << c << "." << endl;
cout << "I am from " << d << "." << endl;
cout << "I am " << e << " years old." << endl;
cin.get();///add this too
return 0;
@DCisthebest in most cases it doesn't happen(never happened to me before) but sometimes, program exits immediately. if we add cin.get(), it waits for user input to move forward. eg: cout<<"hello world";///exits immediately but
1 2
cout<<"hello world";
cin.get();///now program waits for user
but when we use cin>> it takes the input until but including the spaces or newline(enter) character. eg: for string a; cin>>a;, if we type: "games" and then press enter, "games " is saved in 'a' but newline character(enter) is present in the buffer(temporary memory) that would trigger cin.get() so that it would no longer wait for user's input. using cin.ignore() would clear anything that is present in the buffer(in this case, enter key) and cin.get() would again wait for external input before terminating.
@AbstractionAnon: Yes, I did. I was surprised that they use std::cout and std::cin. I never use std:: part. I keep learning. I am going to have to learn array and if else again.
@koopey: Those codes work great. I would have to remember them. Thank you so much.
I was surprised that they use std::cout and std::cin. I never use std:: part.
std:: prefix is required if usingnamespace std; is not specified.
Specifying usingnamespace std; is generally considered a poor practice since it brings in the entire std:: namespace creating potential naming conflicts. That being said, usingnamespace std; is commonly used here for brevity and clarity.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string a, b, c, d, e;
int f;
std::cout << "Hello there!" << endl;
std::cout << "What is your first name?" << endl;
std::getline(std::cin, a);
std::cout << "What is your middle name?" << endl;
std::getline(std::cin, b);
std::cout << "What is your last name?" << endl;
std::getline(std::cin, c);
std::cout << "Where are you from?" << endl;
std::getline(std::cin, d);
std::cout << "Where are you working at?" << endl;
std::getline(std::cin, e);
std::cout << "How old are you?" << endl;
std::cin >> f;
cin.ignore();
std::cout << "My full name is \b " << a << " \b " << b << " \b " << c << "." << endl;
std::cout << "I am from \b " << d << "." << endl;
std::cout << "I am \b " << e << "." << endl;
std::cout << "I am \b " << f << " years old." << endl;
cin.get();
system("Pause");
return 0;
}