Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

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

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
GMorgan
LXF regular


Joined: Thu Jan 12, 2006 6:58 pm
Posts: 684
Location: South Wales, UK

PostPosted: Sat Mar 04, 2006 11:18 pm    Post subject: C++ quirks (or likely my own stupity). (Solved) Reply with quote

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:
 
     cout << "Second line:";
     cin.get(incr->secondadd, 20, '\n');
     cin.ignore(255,'\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 //
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast