Problem with loop

This is a discussion on Problem with loop within the C Programming forums, part of the General Programming Boards category; Hello I wrote this program it's meant to ask the user to enter the amout of either toy or food ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Problem with loop

    Hello I wrote this program it's meant to ask the user to enter the amout of either toy or food but there is a problem can some one please point out where i'am making the mistake.

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int x=1000;
    	int toy=10;
    	int food=30;
    	int amount;
    	int totals;
    	int totalm;
    	int i;
    	
    printf("what do you wish to buy 1 for toy or any other number for food: ");
    scanf("%d", &i);
    
    if(i==1)
    {
    
    printf("How many individual units of toy do you wish to buy: ");
    scanf("%d", &amount);
    
    while (amount != -1) {
    
                 totals = toy*amount;
                 totalm = x-totals;
                 printf("You have %d money left\n", totalm);
                 }
    
    }
    
    else
    
    printf("How many individual units of food do you wish to buy: ");
    scanf("%d", &amount);
    
    while (amount != -1) {
    
                 totals = toy*amount;
                 totalm = x-totals;
                 printf("You have %d money left\n", totalm);
                 }
    
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    23
    try putting else in brackets?

    Code:
    else
    {
    
    printf("How many individual units of food do you wish to buy: ");
    scanf("%d", &amount);
    
    while (amount != -1) {
    
                 totals = toy*amount;
                 totalm = x-totals;
                 printf("You have %d money left\n", totalm);
                 }
    }
    return 0;
    }
    This would be a good situation to use switch statement also instead of if else blocks.
    Last edited by eurus; 01-19-2006 at 05:47 AM.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Does'nt work

    I put braces around the else statement but it still had the same problem the loop was infinite what could I do to overcome this problem.

  4. #4
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    Quote Originally Posted by HAssan
    I put braces around the else statement but it still had the same problem the loop was infinite what could I do to overcome this problem.
    you never change amount in the loop so it is always != -1

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    23
    Ah yes, create some sort of a flag inside the loop to exit out or modify your while statement.

  6. #6
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    well now that i look at the whole code..

    why do you need a loop.. why not an if..
    Code:
    int amount = 0;
    if(amount) {
        // do the calculations here..
    }
    also do not use i as a variable outside of a loop..
    and you need to initialize amount before you test it..

  7. #7
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469

    differences between using "while" and "if"

    i get the feeling that you are not clear of the difference between if statements and while loops, so i've written the following with that in mind, hope it clears the issue up for you. the following is obviously not real code, don't try to compile it!

    I assume that you are trying to use loops for running the program over and over again at will. if not, then use if statements like the previous post said, or remove the while loop from the code below. here what i think you were trying to do, and i know its not a very eloquent solution, but the aim of writing the code like this is to show how to apply if statements to the situation correctly.

    just for some clarity for you, i will describe what happens here. when the program starts, it enters a while loop. the purpose of initializing choice = 1 is for this - any non zero number will do. then the user is asked to choose what they want to do by entering 1 for toys, 2 for food or 0 to quit.

    assuming they choose 1, then choice is set to 1. the first thing the computer sees after that is

    if (choice == 1)

    since choice is 1, the computer knows to execute the following code block (for the toys). after it has finished this, the computer then sees

    if (choice == 2)

    but you haven't changed the value of choice, so the if statement is false - the computer won't execute that block of code - it skips on. there is nothing after that block of code, so the end of the loop is reached. this causes the computer to "jump" back up to where is says

    while (choice != 0)

    but like i said before, choice is still 1. this means that the looping condition is true, so the loop begins again. then you are asked to make a choice again - 1, 2 or 0. so long as you dont pick 0, you can loop over as many times as you like. this is not an infinite loop though, because you can easily enter 0 and break the loop.

    try removing the while loop - the program will run once (because if statemnts only run once on their own) and then exit. try changing the while loop to something like:

    while (2 > 1)

    you get an infinite loop - no matter what you choose, the code loops over! That is essentially what you had done in your 2 loops, there was no way of breaking the looping conditions.

    i hope that this provides some insight and helps you out, and i apologise if i made it overly long and detailed, just trying to cover all the angles!


    Code:
    #include <a bunch of header files you need.h>
    
    int main ()
    {
        int declare all the variables;
        int choice = 1; /*this is to allow the program to enter the loop*/
        
    
              while (choice != 0)
             {
    
    
              printf ("Make your choice - toy or food (or quit): ");
              scanf ("%d", &choice);
              
              if (choice == 1)
              {
                 /*do the stuff relating to toys*/
                 ...
              }
              
              if (choice == 2)
              {
                 /*do stuff relating to food*/
                 ...          
              }
              
        } /*end of the while loop*/
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM

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