?
Solved

How to get user input for a console application?

Posted on 2003-02-26
16
Medium Priority
?
561 Views
Last Modified: 2012-05-04
Hi,

I have a simple console application in C++ that does some data archiving for Access database. I need to get user's input for a date, which I coded as follows:

printf("Please enter a date for archiving, in the format of yyyymmdd: ");
scanf ("%s", date);  
date[8] = '\0';

It worked fine if a date is entered (such as 20030226). The problem with the above is that if a user does not input anything, instead, just presses [Enter], the program will keep on waiting for the input, meaning, it stays at statement 'scanf ("%s", date);'. Please advise, or let me know if there is an alternate way to do so. Note that I'm not using STL.  
Thanks.
0
Comment
Question by:minjiezen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 3
  • 3
  • +1
16 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 8025796
Use 'gets()' instead of 'scanf()':

printf("Please enter a date for archiving, in the format of yyyymmdd: ");
gets ( date);  


0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8026180
>>>>The problem with the above is that if a user does not input anything, instead, just presses [Enter], the program will keep on waiting for the input

this is for ur benefit only.. what if u have empty string in the date .. are u prepared to handle such situation ?
btw another way to get the user input using scanf wud be

scanf("%[^\n]",date);
this will read the input till the newline
0
 

Author Comment

by:minjiezen
ID: 8026230
jkr, thanks for the prompt reply. Your method works if I put a break point on line 'gets( date )'. However, it goes straight through if I don't put a break point on that line, meaning, it does not wait for my input at all, and goes directly to the next statement or break point. How do I solve that? Thanks.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:minjiezen
ID: 8026327
akshayxx, with your solution, the app also went straight through the scanf statement without waiting for the input. You had a good point about preparing for handling the empty string. I need to check the code about that. Thanks.
0
 
LVL 86

Expert Comment

by:jkr
ID: 8026351
>>it does not wait for my input at all

It does. Try it without a debugger :o)
0
 

Author Comment

by:minjiezen
ID: 8027128
akshayxx, with your solution, the app also went straight through the scanf statement without waiting for the input. You had a good point about preparing for handling the empty string. I need to check the code about that. Thanks.
0
 

Author Comment

by:minjiezen
ID: 8027679
It may sound crazy but I just cannot make the app stop at 'gets' (I launched the app from the DOS prompt, and from the Windows Explorer, without going through the debugger). The funny thing is that there is a 2nd 'gets' after this 1st one and that one worked. Is there anything I did wrong for the 1st 'gets'? See the code below:

char date[9];
printf("\nMask IDs last loaded ON and BEFORE a certain date will be archived.\n");
printf("Please enter a date for archiving, in the format of yyyymmdd:\n");
gets( date );

char maskIDs[256];
printf("\n\nEnter the mask IDs that are in the date range you entered,\n");
printf("but you do not want them archived. Separated them by comma, \n");
printf("without spaces around the commas, e.g., mask-1,mask-2,...,mask_n:\n");
gets( maskIDs );

Thanks.
0
 

Author Comment

by:minjiezen
ID: 8027815
It may sound crazy but I just cannot make the app stop at 'gets' (I launched the app from the DOS prompt, and from the Windows Explorer, without going through the debugger). The funny thing is that there is a 2nd 'gets' after this 1st one and that one worked. Is there anything I did wrong for the 1st 'gets'? See the code below:

char date[9];
printf("\nMask IDs last loaded ON and BEFORE a certain date will be archived.\n");
printf("Please enter a date for archiving, in the format of yyyymmdd:\n");
gets( date );

char maskIDs[256];
printf("\n\nEnter the mask IDs that are in the date range you entered,\n");
printf("but you do not want them archived. Separated them by comma, \n");
printf("without spaces around the commas, e.g., mask-1,mask-2,...,mask_n:\n");
gets( maskIDs );

Thanks.
0
 
LVL 86

Expert Comment

by:jkr
ID: 8028013
What compiler are you using? I tried

/* GETS.C */

#include <stdio.h>

void main( void )
{
   char line[81];

   printf( "Input a string: " );
   gets( line );
   printf( "The line entered was: %s\n", line );
}

Tird it with VC++6 and gcc2.96 and it works with both...
0
 

Author Comment

by:minjiezen
ID: 8028665
I used VC++ 6.0 to compile the app.

I finally found the cause, although I'm still not sure why it is so: I had another 'scanf' before the two that are shown here, and I forgot to change that 'scanf' to 'gets' (i.e., I have altogether 3 inputs from users). Once I changed that scanf to gets, the problem is gone.

Thank you very much for the help.
0
 

Expert Comment

by:purnendughosh
ID: 8031523
See i tell u this will

when u r using c++ ...
then go for

cin.getline()

When cin is used with strings of characters it is usually used with its getline method, that can be called following this prototype:

cin.getline ( char buffer[], int length, char delimiter = ' \n');

where buffer is the address where to store the input (like an array, for example), length is the maximum length of the buffer (the size of the array) and delimiter is the character used to determine the end of the user input, which by default - if we do not include that parameter - will be the newline character ('\n').


#include<iostream>
using namespace std;
void main()
{
char date[20];
cin.getline(date,20)//no problems at all
}

hope u got it;;;
0
 

Expert Comment

by:purnendughosh
ID: 8031535
it would be better than gets()..
0
 

Author Comment

by:minjiezen
ID: 8034020
Hi purnendughosh, cin.getline() is one of the first things I tried but I got the the same problem: at least one of the getline() was skipped. Now I think it might also be because of the mixing of methods from different libraries. I'll try getline() again. It's a pure C++ function and should be better, as you said. Thanks a lot for the info.
0
 

Expert Comment

by:purnendughosh
ID: 8041113
!!!!
well if u donot enter char....but if just..press enter
then it comes out and doesn't wait....
i suppose that was the remedy that u waanted
0
 

Author Comment

by:minjiezen
ID: 8042727
The problem was that the app did not even give me a chance to press [Enter] for the 2nd input, as if cin.getline(...) wasn't there at all, and it went straight to the 3rd input. As I described before, there are 3 inputs altogether, and I somehow forgot to change the 1st input to cin.getline() (or to gets()), which was buried in the code somewhere. Anyway, Now I tried it out again (with all 3 of them using cin.getline()), and it worked beautifully. The cause of the app ignoring the cin.getline() for the 2nd input was, again, due to the different input method used for the 1st input.

I'd like to give you points as a way of saying thanks. How should I do it? Put another question out? Thanks.

0
 

Author Comment

by:minjiezen
ID: 8043068
The problem was that the app did not even give me a chance to press [Enter] for the 2nd input, as if cin.getline(...) wasn't there at all, and it went straight to the 3rd input. As I described before, there are 3 inputs altogether, and I somehow forgot to change the 1st input to cin.getline() (or to gets()), which was buried in the code somewhere. Anyway, Now I tried it out again (with all 3 of them using cin.getline()), and it worked beautifully. The cause of the app ignoring the cin.getline() for the 2nd input was, again, due to the different input method used for the 1st input.

I'd like to give you points as a way of saying thanks. How should I do it? Put another question out? Thanks.

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question