Accepting Text Responses

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();

return (0);
}
What exactly are you getting for an error?
I'm not quite sure I understand your question.

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.
You can use strings:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name;
    cout << "What's your first name? ";
    cin >> name;//gets 1 word
    cout << "\nHello " << name << " !\nWhat's your full name? ";
    getline(cin,name);//gets full line;
    cout << "\nHello " << name;
    return 0;
}
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.
Last edited on
Take a look at Bazzy's code above for an example on strings. Also review this http://www.cplusplus.com/doc/tutorial/variables.html when you have a moment.
Ok i read that page and I got the same error as his code.

fatal error LNK1169: one or more multiply defined symbols found

I have no idea what that means. Can someone tell me?
Bazzy's code? Post what you have now and place it between the tags.
This is exactly what I am trying to run. Only thing i'm thinking i need is an char name; but i could be wrong

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>

using namespace 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);
}
At lines 20 an 22 you are declaring two variables with the same name, remove int name;, you dont need it.
It doesn't seem to like the getline(cin.name);
Well, duh.
It's getline(cin,name);
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?
You may have to call cin.sync(); before getline(cin,name);
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!
Topic archived. No new replies allowed.