• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 569
  • Last Modified:

How to get user input for a console application?

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
minjiezen
Asked:
minjiezen
  • 9
  • 3
  • 3
  • +1
1 Solution
 
jkrCommented:
Use 'gets()' instead of 'scanf()':

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


0
 
akshayxxCommented:
>>>>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
 
minjiezenAuthor Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
minjiezenAuthor Commented:
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
 
jkrCommented:
>>it does not wait for my input at all

It does. Try it without a debugger :o)
0
 
minjiezenAuthor Commented:
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
 
minjiezenAuthor Commented:
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
 
minjiezenAuthor Commented:
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
 
jkrCommented:
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
 
minjiezenAuthor Commented:
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
 
purnendughoshCommented:
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
 
purnendughoshCommented:
it would be better than gets()..
0
 
minjiezenAuthor Commented:
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
 
purnendughoshCommented:
!!!!
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
 
minjiezenAuthor Commented:
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
 
minjiezenAuthor Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 9
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now