Looping/Repetition help

This is a discussion on Looping/Repetition help within the C Programming forums, part of the General Programming Boards category; I've been trying to figure this out for the past couple days, but it seems to be defeating me. I'm ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    37

    Looping/Repetition help

    I've been trying to figure this out for the past couple days, but it seems to be defeating me. I'm just at the very beginning of learning C Programming and this is one of my assignments. I'm just having trouble with the looping part. In the assignment hand out it said to not use any relational operator (==, !=, >, >=, <, <=) in the condition test for the loop. I can't seem to do it any other way. Even when I do use it, it doesn't seem to stop right away. I put my "while" below where it displays the code numbers so it won't keep repeating every time.

    I didn't want to post my actual code, so I just shortened it and changed some stuff.

    this is the output I get (more or less). It doesn't want to stop when I put a 0 in at Enter amount or zero to stop. It always ask to enter the code as well.

    1 - Code 1
    2 - Code 2

    Enter amount or zero to stop: x
    Enter code: x
    xx dollars = x


    Enter amount or zero to stop: x
    Enter code: x
    xx dollars = x


    Enter amount or zero to stop: 0
    Enter code: 0
    00 dollars = 0

    Code:
    #include <stdio.h>
    
    
    const float num_1 = 1.234;
    const float num_2 = 0.123;
    
    
    int main()
    
    {
            
    // Declare Variables
            
            int code;
            float dollars,total;
            
                
    // Input
    
            printf("\n");
            printf("1 - Code 1 (num_1)");
            printf("\n");
            printf("2 - code 2 (num_2)");
            printf("\n");
            printf("\n");
    while (amount != 0) {   
            printf("Enter amount or a zero to stop: ");
            scanf("%f", &dollars);
            printf("Enter code: ");
            scanf("%d", &code);
    
    // Calculations  
                    
             if (code>0 && code<3)
            {
                    if (code == 1)
                    {
                    total = amount * num_1;
                    }
                    else if (code == 2)
                    {
                    total = amount * num_2;
                    }
                    
            } // End if-else
                    
    // Print output
    
            switch (code)
            {
                    case 1:
                            printf("%.2f dollars = %.2lf", USD, total);
                            break;
                    case 2:
                            printf("%.2f USD = %.2lf ", USD, total);
                            break;
                    
                    default:
                            printf("Code %d is invalid.", code);
            } // End Switch
                    
    } // End While
    
    return 0;
                    
    }

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409

    .

    you could use while(myVariable) this evaluates to while true, so if you get a zero the loop will break, i think minus numbers would be an issue though
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    37
    Okay, the my variable thing worked, but it is still asking to enter a code after I put in a 0 for the amount. I know it has to do something with putting a printf somewhere else right? I just can't seem to figure out where and still have the program run smoothly.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    You could try to reorder the program, I guess that's what you mean by "putting a printf somewhere else". You'd have to have reading the 0 to stop the last thing in the loop.


    A simpler way is to just test for 0 and break out of the loop:


    Code:
    while (amount != 0) {   
            printf("Enter amount or a zero to stop: ");
            scanf("%f", &dollars);
            if (!dollars) //equiv to if dollars != 0.0
                  break;             
            printf("Enter code: ");
            scanf("%d", &code);
    
    // Calculations  
                    
             if (code>0 && code<3)
            {
                    if (code == 1)
                    {
                    total = amount * num_1;
                    }
                    else if (code == 2)
                    {
                    total = amount * num_2;
                    }
                    
            } // End if-else
                    
    // Print output
    
            switch (code)
            {
                    case 1:
                            printf("%.2f dollars = %.2lf", USD, total);
                            break;
                    case 2:
                            printf("%.2f USD = %.2lf ", USD, total);
                            break;
                    
                    default:
                            printf("Code %d is invalid.", code);
            } // End Switch
                    
    } // End While
    // execution will resume here after break 
    return 0;
                    
    }
    Your code snippet doesn't set the amount variable anywhere -- maybe missed because you didn't post your full code, but thought I'd point it out.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    37
    Yes, I've noticed that I did leave amount out in the code I posted. But in my actual full code everything works like it should now! thank you so much!

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21