Saturday, May 22, 2010

C++ problem?

can some 1 give an example code for entering a name








example





{


char name;





cout %26lt;%26lt; " hello name?" %26lt;%26lt; endl;


cin %26gt;%26gt; name;


cout %26lt;%26lt; name %26lt;%26lt; endl;


system("pause");


return 0;





}





i need it to display whatever that person entered for the name





tyvm for the help

C++ problem?
Well, you cant use a char(unless you declare it to be a certain length)





Use a string instead... that is..





int main(){


string name;





cout %26lt;%26lt; "Hey! Whats your name?" %26lt;%26lt; endl;


cin %26gt;%26gt; name;


cout %26lt;%26lt; "Hello " %26lt;%26lt; name %26lt;%26lt; endl;


system("pause");


return 0;


}





Also, note that a system call is OS specific - this program would work on a Windows machine but not a *UNIX one





If you need anymore help, feel free to message me!





EDIT: Assuming you still wanted to use char.. you chould declare your char variable like this





char[10] name;





this declares char and gives it 10 spaces in which to use.. so as long as the name does not exceed 10 letters you would be fine(otherwise it would chop off the extra part of the name)





Like I said.. easier to just use a string ;)


No comments:

Post a Comment