Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 03-11-2007, 09:40 AM   #1 (permalink)
 
True Techie

Join Date: Aug 2006

Posts: 202

JinkX

Default c programming

heres the probliem ..
if i enter an alpha in this program all i will have is an endless loop
what can i do to stop this ?? and print the statement "what ever"
here the program

#include <stdio.h>
int main ()
{
// here u enter the 3 integers of time
int hrs , min , sec ;

printf("please enter the hrs now\n");
scanf("%d",&hrs);
// if the time was wrong this will make u inter the number again
while (hrs <0 || hrs >=24 )

{
printf("this time is invalid please enter a valid number\n");
scanf("%d",&hrs);

}
printf("please enter min now\n");
scanf ("%d",&min);
while (min < 0 || min > 60)
{
printf("this time is invalid please enter a valid number\n");
scanf("%d",&min);
}
printf("please enter sec now\n");
scanf("%d",&sec);
while (sec < 0|| sec >= 60 )
{
printf("this time is invalid please enter a valid number\n");

scanf("%d",&sec);
}
// if evrything is going ok , then the output will be as the following
printf("the time is %02d : %02d : %02d ",hrs,min,sec);


return 0;
}
__________________
plz god .. if u cant make me thin ..
make all my friends very fat !
JinkX is offline  
Old 03-18-2007, 08:06 AM   #2 (permalink)
 
True Techie

Join Date: Aug 2006

Posts: 202

JinkX

Default Re: c programming

plz help me !!!
__________________
plz god .. if u cant make me thin ..
make all my friends very fat !
JinkX is offline  
Old 03-19-2007, 06:12 PM   #3 (permalink)
 
Ultra Techie

Join Date: Jul 2006

Posts: 714

Meithan is on a distinguished road

Send a message via AIM to Meithan Send a message via Yahoo to Meithan
Default Re: c programming

What do you mean "If I enter an alpha"?
__________________


SuperPi 1M: 29.6s - 3DMark06: 8616
The Geek Test v3.1 Score: 36.3% - Major Geek
Meithan is offline  
Old 03-19-2007, 11:54 PM   #4 (permalink)
 
Ultra Techie

Join Date: Jul 2006

Posts: 714

Meithan is on a distinguished road

Send a message via AIM to Meithan Send a message via Yahoo to Meithan
Default Re: c programming

I compiled your program and I see what you mean. The problem is that scanf doesn't handle invalid inputs very well, such as trying to read in a string into an integer variable. When you try to do that, scanf gets confused, and simply continues the execution of the program, but it does not flush the input buffer first. So if the scanf is in a loop, the next time it it executed the original string you had entered will still be in the input buffer, and scanf will fail again.

I can see two solutions to this problem:

1) Manually flush the input buffer whenever an invalid input is provided.
2) Utilize the cin instruction instead of scanf. This is in general a better practice since printf / scanf are old instructions with a lot of issues.

However, if you still want to use scanf, there's a way to solve the problem. As I said, we gotta make sure the input buffer is flushed when scanf fails. The code is below. This is what it does:

1) Define the variable check to know whether scanf fails.
2) When we read an input with scanf, we use the syntax "check = scanf(...)". With this, the result of the "read" operation is saved in the variable check. The result itself is the number of read variables (one in your case), or an "EOF" if scanf fails.
3) After the scanf, we just test the "check" variable: if it has any value other than 1 (remember 1 also means "logical true"), then the input was incorrect and we have to make the user enter it again ...
4) ... but before we can do that, we must make sure the input buffer is cleared. We do this by invoking scanf with the string "%*[^\n]", which reads anything in the input buffer until the end of line. The important thing here is that the input is ignored; scanf never tries to assign it to a variable, so we avoid any problem with the input.

Code:
#include <stdio.h>
int main (){

// here u enter the 3 integers of time
int hrs, min, sec;
int check, exit;

printf("please enter the hrs now\n");
check = scanf("%d",&hrs);
// if the time was wrong this will make u inter the number again
while (!check) {
      scanf("%*[^\n]");
      printf("this time is invalid please enter a valid number\n");
      check = scanf("%d",&hrs);
}   


printf("please enter min now\n");
check = scanf("%d",&min);
while (!check) {
      scanf("%*[^\n]");
      printf("this time is invalid please enter a valid number\n");
      check = scanf("%d",&min);
}

printf("please enter sec now\n");
check = scanf("%d",&sec);
while (!check) {
      scanf("%*[^\n]");
      printf("this time is invalid please enter a valid number\n");
      check = scanf("%d",&sec);
}

// if evrything is going ok , then the output will be as the following
printf("the time is %02d:%02d:%02d\nPress any key to exit...",hrs,min,sec);
scanf("%i",&exit);
return 0;
}

__________________


SuperPi 1M: 29.6s - 3DMark06: 8616
The Geek Test v3.1 Score: 36.3% - Major Geek
Meithan is offline  
Old 04-04-2007, 12:21 PM   #5 (permalink)
 
Newb Techie

Join Date: Jul 2006

Posts: 28

whoosh88

Default Re: c programming

i don't think i understand your question.

first of all, can you explain why you would want to enter a letter (alpha?) in a program where you input time?

and instead of:

while (hrs <0 || hrs >=24 )

just make an if statement:

if(hrs < 0 || hrs >= 24)
printf("This time is invalid. Enter another number\n");

that could also be a problem. hope that helps.
whoosh88 is offline  
Old 04-04-2007, 06:33 PM   #6 (permalink)
 
Ultra Techie

Join Date: Jul 2006

Posts: 714

Meithan is on a distinguished road

Send a message via AIM to Meithan Send a message via Yahoo to Meithan
Default Re: c programming

Quote:
Originally Posted by whoosh88 View Post
i don't think i understand your question.

first of all, can you explain why you would want to enter a letter (alpha?) in a program where you input time?
That's the point, he wants the program to make sure a valid time is input. So when letters are entered, the program must catch the error and ask for the time again.

Quote:
Originally Posted by whoosh88 View Post
and instead of:

while (hrs <0 || hrs >=24 )

just make an if statement:

if(hrs < 0 || hrs >= 24)
printf("This time is invalid. Enter another number\n");

that could also be a problem. hope that helps.
The purpose of the WHILE is to keep asking for the time until the user enters a valid number. With a simple IF statement, the user would be asked for input just one more time, and after that, the program would continue.
__________________


SuperPi 1M: 29.6s - 3DMark06: 8616
The Geek Test v3.1 Score: 36.3% - Major Geek
Meithan is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On