C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 6th, 2013, 02:05 AM
gsolorio3 gsolorio3 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 gsolorio3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;

Reply With Quote
  #2  
Old January 6th, 2013, 03:13 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,141 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 5 m 50 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);

Reply With Quote
  #3  
Old January 6th, 2013, 03:27 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,808 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
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("\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;

Reply With Quote
  #4  
Old January 6th, 2013, 01:11 PM
gsolorio3 gsolorio3 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 gsolorio3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 m 51 sec
Reputation Power: 0
Thanks, it's fixed now

Thank you Clifford!!! it worked!!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Need Help with looping in C Language

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap