I just started C++ with Visual Basic '08 and have been messing around with it and noticed that I can't use the same code for text responses that I use for numerical responses. For example I can make it say "What is your name" and have it accept any name.Heres my code so far....mind you its not much
#include <iostream>
using namespace std;
int main()
{
int yearborn; //inputing the year you were born
cout<<"!!!PLEASE USE NUMBERS ONLY!!! "<<endl;
cout<<"What year were you born? ";
cin>> yearborn;
cin.ignore();
cout<<"So you were born in "<< yearborn <<"\n";
int question; //inputing the age
cout<<"So how old would that make you? ";
cin>> question;
cin.ignore(); //ignores the anwser i think, so it accepts all anwsers
cout<<"So to be clear you are "<< question <<"\n";
int time; //inputing the time
cout<<"What time is it by the way? ";
cin>> time;
cin.ignore();
cout<<"Damn it's already: "<< time <<"\n";
int responce;
cout<<"Well sorry I have to run, what was your age again?";
cin>> responce;
cin.ignore();
cout<<"Thanks i wont forget that your "<< responce <<"! \n";
cin.get();
First of all, you can't be using C++ with Visual Basic '08. I'm assuming you are referring to the IDE you're using??? Also, remove those cin.ignore()s as they don't really have a use in your program. As far as using the same code for text and numeric... your code only uses integer data types and therefore should only be accepting integers as entry. If you want the user to enter text you need to use a string or char datatype.
That code doesn't give me an error , but when I type say "joe" my screen just flashes and it ends. Sorry its Visual C++ '08. I removed the cin.ignore()s all except the last one so you can see the last text. Thank you bazzy ill try that now.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int yearborn;
cout<<"What year were you born? ";
cin>> yearborn;
cout<<"So you were born in "<< yearborn <<"\n";
int question;
cout<<"So how old would that make you? ";
cin>> question;
cout<<"So to be clear you are "<< question <<"\n";
int name;
string name;
cout << "What's your first name? ";
cin >> name;
cout << "\nHello " << name << " !\nWhat's your full name? ";
getline(cin.name);
cout << "\nHello " << name;
int time;
cout<<"What time is it by the way? ";
cin>> time;
cout<<"Damn it's already: "<< time <<"\n";
int responce;
cout<<"Well sorry I have to run, what was your age again?";
cin>> responce;
cin.ignore();
cout<<"Thanks i wont forget that your "<< responce <<"! \n";
cin.get();
return (0);
}
I tried that and it didn't work so I thought that he nay have miss typed it and put a . instead of a , . It still does not work. Does it work for everyone else?
That was it. Thanks a ton guys. I'm probably gonna mess around for a day or 2. I'll read up on C++ and look for ways to make it only respond to one anwser. If someone could link me it that would be awesome. If not whatever. Well thanks again!