Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
January 6th, 2013, 02:05 AM
 gsolorio3
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("\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;```

#2
January 6th, 2013, 03:13 AM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,141
Time spent in forums: 2 Months 2 Weeks 3 Days 23 h 52 m 21 sec
Reputation Power: 1974
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);

#3
January 6th, 2013, 03:27 AM
 clifford
Contributing User

Join Date: Aug 2003
Location: UK
Posts: 4,808
Time spent in forums: 1 Month 2 Days 17 h 43 m 11 sec
Reputation Power: 1800
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("\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;```

#4
January 6th, 2013, 01:11 PM
 gsolorio3
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!!

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Need Help with looping in C Language