943,905 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 856
  • C RSS
Dec 6th, 2008
0

problem with the main()

Expand Post »
Hello guys, I'm trying to collect user input which combines integers with letters, but whenever it reaches the scanf for char it skips it and goes to next integer, could anybody explain why??? is it because main returns an integer,? I tried setting void main(void), and it sais that main has to be an integer during compilation.. here comes the code:
Could anybody explain why when it goes to scanf it doesnt let user to enter the letter for char and skipps it?
thank you!

  1. #include<stdio.h>
  2.  
  3. void GetTime(void);
  4. void GetLetter(void);
  5. void ShowTime(void);
  6. int MinuteAdd(int *p_hour, int *p_minute, char *p_mode);
  7.  
  8. // here comes the block of main
  9. main()
  10. {
  11. int time=0;
  12. int hour=0;
  13. int minute=0;
  14. char mode;
  15.  
  16. GetTime();
  17. GetLetter();
  18. ShowTime();
  19.  
  20. }
  21.  
  22. //here comes the function to get users input
  23. void GetTime(void)
  24. {
  25.  
  26. int hour=0;
  27. int minute=0;
  28.  
  29. printf("Enter the hour:");
  30. scanf("%d", &hour);
  31. printf("\nEnter the minute:");
  32. scanf("%d", &minute);
  33.  
  34. }
  35.  
  36.  
  37. // here is the function to get a character since main returns an integer
  38. void GetLetter(void)
  39. {
  40. char mode;
  41. printf("\nEnter A (for AM) or P (for PM):");
  42. scanf("%c", &mode);
  43. }
  44.  
  45.  
  46. //here comes the functio for getting amount of repetition times
  47. void ShowTime(void)
  48. {
  49. int amount=0;
  50. printf("\nEnter how many minutes to display:");
  51. scanf("%d", &amount);
  52. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atman is offline Offline
50 posts
since Oct 2008
Dec 6th, 2008
0

Re: problem with the main()

It's because scanf doesn't read a whole line. It only reads what it needs.

So if you type
123\n

and you remove 123 with your scanf("%d"), then that leaves the \n for something else to deal with.

That something else (in your case) is scanf("%c") which reads the next char (ie, the \n).

A quick hacky fix would be to use %1s to read the next non-whitespace character.

The better answer is to use fgets() to read ALL input, then extract what you need from the buffer in memory.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 6th, 2008
0

Re: problem with the main()

If I remember right, chars in scanf() don't need the & before them.
Yes, main() should always return zero, and shouldn't you be passing the variables to be modify through the parameters of the functions?
edit:
Or was it for strings(%s)...
Last edited by MosaicFuneral; Dec 6th, 2008 at 3:02 am.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Dec 6th, 2008
0

Re: problem with the main()

wow thats awesome i still dont understand why does it skip to the next line, im reall new, but how to extract from fgets buffer into memory?
thanx alot!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atman is offline Offline
50 posts
since Oct 2008
Dec 6th, 2008
0

Re: problem with the main()

could anybody please help imbeding fgets() to read 1 letter from stdin? do i have to create array for this? do i have to include a pointer to char in fgets() argument? once its read i have to extract the value from the buffer to memory? could anyone please help with an exmple possibly using the my code???
any help would be greatly appreciated.


thank you!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atman is offline Offline
50 posts
since Oct 2008
Dec 6th, 2008
0

Re: problem with the main()

Each scanf would be fgets() + sscanf()
  1. char buff[BUFSIZ];
  2. fgets( buff, sizeof buff, stdin );
  3. sscanf( buff, "%d", &hour);
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 6th, 2008
0

Re: problem with the main()

Click to Expand / Collapse  Quote originally posted by atman ...
could anybody please help imbeding fgets() to read 1 letter from stdin? do i have to create array for this? do i have to include a pointer to char in fgets() argument? once its read i have to extract the value from the buffer to memory? could anyone please help with an exmple possibly using the my code???
  1. #define LSZ 128
  2. char line[LSZ];
  3. ...
  4. if (fgets(line,sizeof line,stdin)) {
  5. /* You have line chars in char array now */
  6. ... line[0]... /* that's the 1st char of this line! */
  7. } else {
  8. /* No data (end of file or i/o error) */
  9. }
What's a problem?
Last edited by ArkM; Dec 6th, 2008 at 5:09 am.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Dec 7th, 2008
0

Re: problem with the main()

This series might give you some insight on scanf() .

And why would you want to use fgets() on stdin to read a character? That's what getchar() is for.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,729 posts
since May 2006
Dec 8th, 2008
0

Re: problem with the main()

> And why would you want to use fgets() on stdin to read a character? That's what getchar() is for.
But then you're back in the same situation as using scanf(), in that you've got no idea what state the input stream is in.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 9th, 2008
0

Re: problem with the main()

So true. So you really need
  1. char buff[BUFSIZ];
  2. fgets( buff, sizeof buff, stdin );
  3. if (buff[0] == xval)
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,729 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: hacking and virus
Next Thread in C Forum Timeline: Problem using multiple forks





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC