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!

#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.

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)...

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!

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!

Each scanf would be fgets() + sscanf()

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???

#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) */
}

What's a problem?

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.

> 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.

So true. So you really need

char buff[BUFSIZ];
fgets( buff, sizeof buff, stdin );
if (buff[0] == xval)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.