C++ quirks (or likely my own stupity). (Solved)

Code junkies hangout here

Moderators: ChriThor, LXF moderators

C++ quirks (or likely my own stupity). (Solved)

Postby GMorgan » Sat Mar 04, 2006 11:18 pm

I've had trouble with entering a value into a 20 character array using the
cin.get command with three arguments. The problem that occurs is if somebody simply presses enter with no value entered the program seemingly goes into an infinite loop rather than entering the '\0' character into the 0 element of the array. The code I've been using for entry is below

Code: Select all
     cout << "Second line:";
     cin.get(incr->secondadd, 20, '\n');

The cin.ignore is there incase somebody enters more than 19 characters into the array. It works in Dev-Cpp (which uses a GNU compiler, I thought they'd have the same quirks even across platforms ) in windows but on trying to run it in Ubuntu I get the problem above. The cin.getline command solves the one problem (that being entering '\0' on an empty entry) but leaves me with an infinite loop even with the cin.ignore command if more than 19 characters are entered (the cin.ignore also requires a enter each time since getline flushes the '\n' character from the buffer).

If anyone has a solution that enters '\0' into secondadd[0] on an empty entry while also truncating the entry should it exceed 19 characters I will be grateful. The '\0' is necessary since I use it later to skip an empty field during output.

//edit - nevermind I found a solution //
LXF regular
Posts: 684
Joined: Thu Jan 12, 2006 6:58 pm
Location: South Wales, UK

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests