| | |
problem with the main()
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2008
Posts: 48
Reputation:
Solved Threads: 0
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!
Could anybody explain why when it goes to scanf it doesnt let user to enter the letter for char and skipps it?
thank you!
C Syntax (Toggle Plain Text)
#include<stdio.h> void GetTime(void); void GetLetter(void); void ShowTime(void); int MinuteAdd(int *p_hour, int *p_minute, char *p_mode); // here comes the block of main main() { int time=0; int hour=0; int minute=0; char mode; GetTime(); GetLetter(); ShowTime(); } //here comes the function to get users input void GetTime(void) { int hour=0; int minute=0; printf("Enter the hour:"); scanf("%d", &hour); printf("\nEnter the minute:"); scanf("%d", &minute); } // here is the function to get a character since main returns an integer void GetLetter(void) { char mode; printf("\nEnter A (for AM) or P (for PM):"); scanf("%c", &mode); } //here comes the functio for getting amount of repetition times void ShowTime(void) { int amount=0; printf("\nEnter how many minutes to display:"); scanf("%d", &amount); }
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.
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.
If I remember right, chars in scanf() don't need the
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)...
& 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
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
•
•
Join Date: Oct 2008
Posts: 48
Reputation:
Solved Threads: 0
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!
any help would be greatly appreciated.
thank you!
Each scanf would be fgets() + sscanf()
C Syntax (Toggle Plain Text)
char buff[BUFSIZ]; fgets( buff, sizeof buff, stdin ); sscanf( buff, "%d", &hour);
•
•
•
•
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???
c Syntax (Toggle Plain Text)
#define LSZ 128 char line[LSZ]; ... if (fgets(line,sizeof line,stdin)) { /* You have line chars in char array now */ ... line[0]... /* that's the 1st char of this line! */ } else { /* No data (end of file or i/o error) */ }
Last edited by ArkM; Dec 6th, 2008 at 5:09 am.
This series might give you some insight on
And why would you want to use
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
So true. So you really need
c Syntax (Toggle Plain Text)
char buff[BUFSIZ]; fgets( buff, sizeof buff, stdin ); 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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Help with my main method (Java)
- How to implement a POSIX threaded program to solve producer/consumer problem (C++)
- Problem placing Tkinter root window on the screen (Python)
- Problem Displaying Items (VB.NET)
- error at return point in main (C++)
- Template Header Problem (HTML and CSS)
- Problem with internet I dont understand.. (Web Browsers)
- Crazy Folder Type Problem (Windows NT / 2000 / XP)
- problem with main (Java)
- "cannot resolve symbol"problem (Java)
Other Threads in the C Forum
- Previous Thread: hacking and virus
- Next Thread: Problem using multiple forks
| Thread Tools | Search this Thread |
* ansi api append array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft multi mysql oddnumber open opendocumentformat openwebfoundation overwrite pdf pointer pointers posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation threads unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






