-
Variables? Intergers.
Hi, i've been wondering if i have this program
Code:
#include <iostream.h>
int
main()
{
int you;
cout<<"how are you?";
cin>>you;
cout<<"You are:"<<you;
cin.get();
cin.get();
return 0;
}
before u start saying how bad it is e.t.c if i want to store a couple letter what would it be? because an interger can only store numbers, what can store letters? Also were should i replace this?(i'm looking to replace "in you;" part of the code i think!)
-
You can use arrays of char to act as a string of characters:
Code:
#include <iostream.h>
int
main()
{
char you[50];
cout<<"how are you?";
cin>>you;
cout<<"You are:"<<you;
cin.get();
cin.get();
return 0;
}
For responses with more than one word, cin>> won't get it all. In that case you need cin.getline():
Code:
#include <iostream.h>
int
main()
{
char you[50]
cout<<"how are you?";
cin.getline(you, 50);
cout<<"You are:"<<you;
cin.get();
cin.get();
return 0;
}
And if your compiler is new enough, C++ style strings are preferable to C style strings:
Code:
#include <iostream>
#include <string>
using namespace std;
int
main()
{
string you;
cout<<"how are you?";
getline(cin, you);
cout<<"You are:"<<you;
cin.get();
cin.get();
return 0;
}
-
Thanks alot it works!!! You are truly a god :p:p