- 1

What's wrong with this code??

what I am trying to achieve is that the program salute you. #include <iostream> using namespace std; int main() { int name; int last; cout<<"Write your first and lastname"<<endl; cin>>name>>last; cout<<"its a pleassure Mr"<<endl; cout<<name<<last<<endl; return 0; } WHAT'S WRONG WITH IT???

13th Nov 2016, 4:20 PM
Jorge Gaytan
7 Answers
+ 3
You are inputting name use a string(array of characters). #include <iostream> using namespace std; int main() { char name[20],last[20]; cout<<"Write your first and lastname"<<endl; cin>>name>>last; cout<<"its a pleassure Mr"<<endl; cout<<name<<last<<endl; return 0; }
13th Nov 2016, 4:44 PM
Megatron
Megatron - avatar
+ 3
int means integer. int can only store numbers from -32767 to 32768. char stores one alphabet or any other characters even tab spaces,new lines etc. If we write char a; then it can store one character only. char name[20]; this means I declared a variable that is named 'name' and can store up to 19 characters. That is I allocated it memory equal to 20 bytes.
13th Nov 2016, 4:57 PM
Megatron
Megatron - avatar
+ 1
use array of char for holding character values , they wont store in int u need string
13th Nov 2016, 5:13 PM
Dhruv Saxena
Dhruv Saxena - avatar
0
int?
13th Nov 2016, 4:34 PM
Jorge Gaytan
0
use char*name or string name datatype. string data type works on latest compiler only
13th Nov 2016, 5:23 PM
P Sandesh Baliga
P Sandesh Baliga - avatar
- 1
actually the problem comes once I feed my info it does not act the way its suppose to be. instead of greeting it outputs "it's a pleasure Mr. 01"
13th Nov 2016, 4:34 PM
Jorge Gaytan
- 1
so if I'm right what Mr.Megatron means is that integer does not work for things more complex than numbers right? in order to input more characters we use "char"? I also see that you wrote [20] that means that the length of the input can be no longer than 20 characters right? what does "char" means?? Thank you folks.
13th Nov 2016, 4:51 PM
Jorge Gaytan