473,379 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Bizarre cin.get() problems (I suspect)

Hi, I'm trying to write a simple program (or portion of a program), that will re-prompt a user for input each time the user enters invalid input, and then exit once the user has entered valid input. Here's what I have so far in main():

int main()
{
char input[SIZE];

cout << "Enter a string: ";

while (!InputString(input))
{
cout << "Invalid string entered. Try again: " << endl;
}

system("pause");
return 0;
}

And here's the body of the InputString() function:

bool InputString(char str[SIZE])
{
char ch;
int x = 0;

while ((ch = cin.get()) != '\n')
{
if (x != (SIZE - 1))
{
str[x++] = ch;
}
else
{
str[x] = '\0';
return false;
}
}

str[x] = '\0';

return true;
}

The odd thing that ends up happening is, "Invalid string entered. Try again: " is displayed three times, and then the program exits as if valid input was entered. Anyone know what's going wrong? Thanks.
Nov 26 '06 #1
3 1588
horace1
1,510 Expert 1GB
what is the value of SIZE ? the program will work OK so long as less than SIZE characters are entered on a line. If more are entered it will keep reading the rest of the input stream until it finds less than SIZE characters on a line. Try using cin.ignore() to remove any characters on a line if input fails, e.g. also added a statement to print input if it is OK
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. char input[SIZE];
  4.  
  5. cout << "Enter a string: ";
  6.  
  7. while (!InputString(input))
  8. {
  9. cout << "Invalid string entered. Try again: " << endl;
  10. cin.ignore(1000,'\n');  // ignore rest of characters on the line
  11. }
  12. cout << "input " << input << endl;
  13. system("pause");
  14. return 0;
  15. }
  16.  
Nov 26 '06 #2
Thanks a lot for your reply. I figured I might have to use cin.ignore(), I just wasn't sure where or how. The value of the SIZE constant is 101. The maximum number of characters allowed in order for a string to be valid is 100, plus a terminating null character. Should I use the SIZE constant in the cin.ignore() call, instead of 1000, which seems to be generic?
Nov 26 '06 #3
horace1
1,510 Expert 1GB
[quote=horace1]ignore() extracts and discard characters
Expand|Select|Wrap|Line Numbers
  1. cin.ignore(int length, char t = EOF)
  2.  
Extracts and discards up to length characters from cin or until the termination character t or EOF is found (the termination character is extracted).

I usually just put a large number for length (to make sure all excess characters are removed) but the recommended way is
Expand|Select|Wrap|Line Numbers
  1.   cin.ignore(numeric_limits<int>::max(), '\n');
  2.  
where numeric_limits<int>::max() returns the maximum finite value of an int and the termination character is newline
Nov 26 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Francesco Moi | last post by:
Hello. I'm trying to build a RSS feed for my website. It starts: ----------------//--------------------- <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE rss PUBLIC "-//Netscape...
1
by: Jim F | last post by:
Hi, Once the values I am passing between scripts gets too large, the submit button for the form stops working. Any work arounds other than changing GET to PUT? Thank you, Jim
2
by: J.Marsch | last post by:
Ok, so here's a problem you probably don't see every day: We are building an application that must run in a browser, but we need to do some things client-side that would be rather difficult to...
5
by: Arjen | last post by:
Hi, Is there a utility in C# or in visual web developer to automaticly create get and set code lines for a given variable? Thanks!
0
by: Billie Boy | last post by:
Hi to all. I’m new here and am coming to you from Melbourne Australia. So a big HELLO 2 ALL. Now I am encountering an annoying problem in the SQL builder of the copy of VB.6 that I am using at...
3
by: ilikenwf | last post by:
I am done and this compiles and everything (except for one of the provided files, which seems to have errors itself...I exclude that from the build until I need it, but will post it too). The only...
2
by: patrickdepinguin | last post by:
Hi, I use zlib to write data structures to a compressed file, using the gzwrite function. Afterwards I read the data back with gzread. I notice that this works well when the data written is not...
0
by: Claire | last post by:
I'm writing an email module and Im having lots of strange errors with threads. The form has a similar look to outlook express. I've several buttons that I use to switch between...
15
by: Bexm | last post by:
Hello I have searched through this forum and it seems some people are having similar problems to me but none of the fixes are fixing mine..! :( I have a table in my database that has two xml...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.