Thread: for loop/counter error

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    40

    for loop/counter error

    Hey everyone.

    Writing some code and i'm having a little problem with my for loop. the program is only supposed to output 3 lines of code (all different random prices). However, the first price is always $0.00. I'm sure this is the result of a careless mistake, but i'm not seeing it.

    Code:
            for (counter=0; counter < num_games; counter++){
                    printf("The cost of game %d is $%.2lf\n", counter +1, cost_new);
                    cost_new = 25 + rand()%26;}
                    if(cost_new != 50) {
                    cost_new += rand()%100 * 0.01;}
                    break;}
    is more code needed?

    as always, any help is appreciated.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Does cost_new have a value before you enter the loop? Because in the code you posted you print cost_new before you assign any value to it which could explain why it's 0 in the first round.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    40
    no, that makes sense.. i'll just throw all the code down. The assignment is to have the program assign a random cost of $5-$20 for used games and $25-50 for new games.

    Code:
    int main() {
        int new_games, used_games, num_games, selection, counter;
        double cost_new, cost_used;
        srand(time(0));
    
        printf("Welcome to UCF Games!\n");
        printf("What would you like to do?\n");
        printf("\t 1. Buy New Games.\n");
        printf("\t 2. Buy Used Games.\n");
        printf("\t 3. Quit.\n");
    
        scanf("%d", &selection);
    
        while (selection == 1){
            switch (selection) {
                case 1:
                    printf("How many New Games would you like to buy today?\n");
                    scanf("%d", &num_games);
                    }
    
                for (counter=0; counter < num_games; counter++){
                    printf("The cost of game %d is $%.2lf\n", counter +1, cost_new);
                    cost_new = 25 + rand()%26;}
                    if(cost_new != 50) {
                    cost_new += rand()%100 * 0.01;}
                    break;}

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Just move your printf() statement to a point in your loop body AFTER calculating cost_new.

    The break statement in the for loop will cause it to stop after the first iteration.

    The thread is mistitled too. The problem is not a for loop/counter error. It is a "broken programmer logic" error.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    So just switch place of line #22 and #23.

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    40
    fair enough. thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For Loop Counter
    By RondallEngleII in forum C Programming
    Replies: 2
    Last Post: 02-22-2012, 06:50 PM
  2. need help about counter in for loop
    By hasib ahmed in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2011, 01:40 PM
  3. for loop counter problem
    By karipap in forum C Programming
    Replies: 4
    Last Post: 05-05-2009, 07:05 AM
  4. Continuous loop or counter problem or both?
    By carolsue2 in forum C++ Programming
    Replies: 4
    Last Post: 04-09-2006, 02:59 AM
  5. cant find where my error is in the line counter
    By Led Zeppelin in forum C Programming
    Replies: 5
    Last Post: 03-23-2002, 04:06 PM