problem with the main()

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 48
Reputation: atman is an unknown quantity at this point 
Solved Threads: 0
atman atman is offline Offline
Light Poster

problem with the main()

 
0
  #1
Dec 6th, 2008
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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: problem with the main()

 
0
  #2
Dec 6th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 963
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: problem with the main()

 
0
  #3
Dec 6th, 2008
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.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 48
Reputation: atman is an unknown quantity at this point 
Solved Threads: 0
atman atman is offline Offline
Light Poster

Re: problem with the main()

 
0
  #4
Dec 6th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 48
Reputation: atman is an unknown quantity at this point 
Solved Threads: 0
atman atman is offline Offline
Light Poster

Re: problem with the main()

 
0
  #5
Dec 6th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: problem with the main()

 
0
  #6
Dec 6th, 2008
Each scanf would be fgets() + sscanf()
  1. char buff[BUFSIZ];
  2. fgets( buff, sizeof buff, stdin );
  3. sscanf( buff, "%d", &hour);
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: problem with the main()

 
0
  #7
Dec 6th, 2008
Originally Posted by atman View Post
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: problem with the main()

 
0
  #8
Dec 7th, 2008
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: problem with the main()

 
0
  #9
Dec 8th, 2008
> 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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: problem with the main()

 
0
  #10
Dec 9th, 2008
So true. So you really need
  1. char buff[BUFSIZ];
  2. fgets( buff, sizeof buff, stdin );
  3. if (buff[0] == xval)
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC