Wrestling Forum banner

1 - 6 of 6 Posts

·
Registered
Joined
·
391 Posts
Discussion Starter #1
Code:
#include <iostream>

using namespace std;

int main()
{
  char name;

  cout<<"Please enter your name: ";
  cin>> name;
  cin.ignore();
  cout<<"Welcome, "<< name <<"\n";
  cin.get();
}
Above is my code just a simple C++ program to ask your name but having a problem the variable name is only storing the first character of your name i know char does this but what is the alternative so that it will store a whole name.

Thanks
 

·
In the Heat of the Moment
Joined
·
33,794 Posts
Its because you used char variable, that does one letter I think. I'll try to fix it for you and put it up.

EDIT- Took C++ last year, so rusty at it right now, I'll still try to find out though. :eek:

Double edit- you can do:

#include <iostream>
#include <cmath>
# include <iomanip>

using namespace std;

int main()
{
char name[5];

cout<<"Please enter your name: ";
cin>> name;

cout<<"Welcome, "<< name <<"\n";

}However this will only do the number of letters already defined, which in this case is 5
 

·
Registered
Joined
·
391 Posts
Discussion Starter #3
Thanks mate this does work with error. I played around with it and the number doesnt seem to matter if u have 0 5 or 5000 it will show as many characters as typed until a space. C++ is seeming a little confusing as to how results are shown. Also the 2 #includes dont seem to be needed works either way with error.
 

·
Registered
Joined
·
391 Posts
Discussion Starter #5
yeh thats not a bad idea im new to C++ and not if sure of what commands are with in each of these includes. But thats for your help ill keep playing with it till i get it right.

Thanks
 
1 - 6 of 6 Posts
Top