473,387 Members | 1,440 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,387 software developers and data experts.

help with input

254 100+
what is wrong with this code:
Expand|Select|Wrap|Line Numbers
  1. char Invalid() {
  2.     char ignore[1];
  3.  
  4.     printf("\t\tInvalid choice! Please press any key to try again.\n");
  5.     gets(ignore);
  6.  
  7.     return atoi(ignore);
  8. }
  9.  
actually i wanted it to be wait until user press any key before return anything.

but it seems that it didn't stop after printf() is output for user to press any key, and it just return.

how it happens?
i want it to stop and wait for user to press any key, then return.
I have to write it in C language(no C++).

please help
Jan 28 '07 #1
4 1576
horace1
1,510 Expert 1GB
the newline character which you entered on the end of your last input is probably left in the input stream (so gets() returns immediatly)- you need to remove it. try
Expand|Select|Wrap|Line Numbers
  1. char Invalid() {
  2.     char ignore[1];
  3.        cin.ignore(1000,'\n');        // extract characters up to and including \n
  4.     printf("\t\tInvalid choice! Please press any key to try again.\n");
  5.     gets(ignore);
  6.  
  7.     return atoi(ignore);
  8. }
  9.  
Jan 28 '07 #2
nickyeng
254 100+
the newline character which you entered on the end of your last input is probably left in the input stream (so gets() returns immediatly)- you need to remove it. try
Expand|Select|Wrap|Line Numbers
  1. char Invalid() {
  2.     char ignore[1];
  3.        cin.ignore(1000,'\n');        // extract characters up to and including \n
  4.     printf("\t\tInvalid choice! Please press any key to try again.\n");
  5.     gets(ignore);
  6.  
  7.     return atoi(ignore);
  8. }
  9.  
cin is c++ syntaz rite?

i dont want cin for input stream...
can you change that line to C language based ?
Jan 28 '07 #3
horace1
1,510 Expert 1GB
cin is c++ syntaz rite?

i dont want cin for input stream...
can you change that line to C language based ?
sorry - tend to think in C++ all the time!
try calling gets() twice, once to remove previous input and again to wait for the newline key to be hit
Expand|Select|Wrap|Line Numbers
  1. char Invalid() {
  2.     char ignore[10];
  3.        gets(ignore);       // extract characters up to and including \n
  4.     printf("\t\tInvalid choice! Please press any key to try again.\n");
  5.     gets(ignore);
  6.     return atoi(ignore);
  7. }
  8.  
make your ignore[] array larger than 1 in case there are a number of characters in the input stream
Jan 28 '07 #4
nickyeng
254 100+
thanks alot horace1.
:)

nick
Jan 28 '07 #5

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

Similar topics

3
by: laurie | last post by:
Hi all, I'm trying to help out a friend who has inherited a client with a PHP shopping cart application. Neither of us know PHP, but I've been muddling my way through, trying to get these old...
6
by: Kurt A. Kaylor | last post by:
Hey, I am trying to get some code I have written to work. Runs well until I make a request. The I get some problems with PHP related to an SQL statement. Here are the errors :Warning:...
2
by: Richard | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** HI, I am working on a project where I need to input data to a (local) HTML page using multiple form elements, such as text,...
6
by: -D- | last post by:
I'm trying to accomplish the following. I'm trying to get the values for the table rows that are dynamically created to persist through a redirect. Referring URL:...
5
by: TrvlOrm | last post by:
Can any one please help me...I am new to JavaScript and I have been struggling with this code for days now and can't figure it out. I would like to get the Buttons to correspond with the action...
1
by: rainbowii7 | last post by:
Calling all programmers for helllllllllllllllppppp!!! i am currently doing a uni degree and our lecturers have set us the task of making a game in JavaScript. i chose to do a hangman game and...
5
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
7
by: Papelotte | last post by:
Hi all, I'm new to this forum and I am hoping that there is someone here who can help me. I have an ASP page that has javascript that works perfectly in IE, but not in Firefox. Can anybody tell...
1
by: TopherB | last post by:
Hi, First let me say that my knowledge of HTML and Javascript is fairly limited. But I am stuck in a situation of trying to adapt a website's shopping cart to a new one. Here's the problem, the...
1
by: deepaks85 | last post by:
Dear All, I want to send some data through a form with Multiple attachment in an HTML Format. I have tried it but it is not working for me. I am able to send data without attachment but with the...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.