English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

The problem is that their name can be of any length, so I can't just use a finite length string.

2007-01-07 08:34:25 · 3 answers · asked by sarciness 3 in Computers & Internet Programming & Design

3 answers

"so I can't just use a finite length string"
Except computer memory is finite. Which means that you _do_ need to use a finite length string. Just choose a length you think unlikely to be exceeded.

In C, it would be something like
char name[80];
fgets(name, sizeof name, stdin);

In C++, use string and iostreams.
std::string name;
std::cin >> name;

2007-01-07 10:16:38 · answer #1 · answered by csanon 6 · 0 0

You can arrange it so that first n charachers are accepted (just use scanf), the rest of them are ignored. Better yet, write a function GetUsername that would include all the different options.

2007-01-07 08:42:04 · answer #2 · answered by Snowflake 7 · 0 1

#include
#include
#include

void main()
{
char name;
cout << "Please enter your name \n\n";
cin >> name;
etc etc....
}


or if you want to input the length of the name and use that to initiliaze a char array;

void main()
{
int length = 0;
cout << "Please enter the length of your name \n\n";
cin>>length;
name = new char[length];
cout << "Please enter your name \n\n";
cin >> name;
etc etc....
}

2007-01-07 13:07:41 · answer #3 · answered by Anonymous · 0 0

fedest.com, questions and answers