Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error handling 1

Status
Not open for further replies.

oaquao

Programmer
May 16, 2001
12
US
I'am a student who has just started C++ and for practice I'am writing a simple game.
I have checked the archives and found nothing pertaining to this.
If a user enters improper data such as a number or nothing I want to correct it.

struct player
{
char firstName[ArSize];
int score;
};
player* plyr=new player[numPlyrs];
while (cin.get(plyr.firstName,ArSize))

Here is what I tried.
Any type of input is accepted.
When I use that test condition with an array it works so I would imagine it has something to do with the array of structures.

Any help would be appreciated.
Thank you.
 
Hi.

Take a look at the following function. I found the original
code a few years ago in a book from... David Vandevoorde I
think. I modified it a bit and use it to read strings
from stdin. As you can see each character is read
separately.

void ReadLn(std::string &s)
{
using namespace std;

const cnMax = 0x80; // max string length

int i=0, c;
char buffer[cnMax] = {0};

// skip leading whitespaces
while((c = cin.get()) && cin && isspace(c));

do // read line
buffer[i++] = char(c);
while((c = cin.get()) && cin && i != cnMax && c != '\n');

buffer= '\0'; // terminate char string
s.assign(buffer, i);

return;
}

-- ralf
 
Thanks rpet,

The functions works perfectly.
I appreciate your help.

oaquao
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top