The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Need Help with looping in C Language
Discuss Need Help with looping in C Language in the C Programming forum on Dev Shed. Need Help with looping in C Language C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 6th, 2013, 02:05 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 2
Time spent in forums: 13 m 51 sec
Reputation Power: 0
|
|
|
Need Help with looping in C Language
I'm new to programming.
If user selects "4", the following code will be executed (shown below).
When user enters a value for myNumber1 (non zero) and then enters a value for myNumber2 (this value being zero), it displays:
Please enter two numbers to divide (separate by space): 4 0
Invalid - Cannot divide by zero!
Would you like to continue (y/n)? <--I do not want this to display yet until two numeric values are entered correctly
If the division is undefined (e.g. 4/0), it should only display "Invalid - Cannot divide by zero!" and loop back to display the line where it ask to enter two numbers to divide. This is what I want, shown below):
Please enter two numbers to divide (separate by space): 4 0
Invalid - Cannot divide by zero!
Please enter two numbers to divide (separate by space): 4 4
Result of dividing 4.00 and 4.00 is 1.00
Would you like to continue (y/n)?
How can I fix the code so that it does what I want?
Providing code (of course, I am using the switch statement but the problem lies in case 4):
Code:
case 4:
printf("\nPlease enter two numbers to divide (separate by space): ");
while ((scanf("%f %f", &myNumber1, &myNumber2)) != 2)
{
while (getchar() != '\n')
continue;
printf("\nError reading your input. Please try again!");
printf("\nPlease enter two numbers to divide (separate by space): ");
}
if (myNumber2 == 0)
printf("Invalid - Cannot divide by zero");
else
printf("\nResult of dividing %.2f and %.2f is %.2f", myNumber1, myNumber2, myNumber1/myNumber2);
printf("\nWould you like to continue (y/n)? ");
scanf("%c", &resp);
break;
|

January 6th, 2013, 03:13 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
This:
scanf("%c", &resp);
tells the computer to read in the next character from the input buffer. Since the next character is already in the buffer, the newline char from the Enter key for the previous scanf, it doesn't wait for you to enter the response. Rather, you want to tell scanf to ignore all white space (eg, blanks, tabs, newlines) before reading in the character that you will enter. To do that, you need to add a space before the "%c" thus:
scanf(" %c", &resp);
|

January 6th, 2013, 03:27 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
More consistent formatting might make it easier for you to fathom:
Code:
case 4:
{
printf("\nPlease enter two numbers to divide (separate by space): ");
do
{
while ((scanf("%f %f", &myNumber1, &myNumber2)) != 2)
{
while (getchar() != '\n')
continue;
printf("\nError reading your input. Please try again!");
printf("\nPlease enter two numbers to divide (separate by space): ");
}
if( myNumber2 == 0 )
{
printf("Invalid - Cannot divide by zero");
}
} while( myNumber2 == 0 ) ;
printf("\nResult of dividing %.2f and %.2f is %.2f", myNumber1, myNumber2, myNumber1/myNumber2);
printf("\nWould you like to continue (y/n)? ");
scanf("%c", &resp);
}
break;
|

January 6th, 2013, 01:11 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 2
Time spent in forums: 13 m 51 sec
Reputation Power: 0
|
|
|
Thanks, it's fixed now
Thank you Clifford!!! it worked!!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|