Sam Bh
Sam Bh

Reputation: 21

Why is the loop not stopping?

#include <stdio.h>
#include <stdlib.h>

#define NEWGEAR 15.0
#define USEDGEAR 5.0

int main() {
    int type;
    int num_items;
    float total;

    printf("Welcome to the market\n");
    printf("What would you like to do\n");
    printf("\t1-Buy New Gear\n");
    printf("\t2-Buy used gear\n");
    printf("\t3-Quit\n");

    scanf("%d", &type);

    while (type != 3) {
        switch (type) {
        case 1:
            printf("How many new items would you like?\n");
            scanf("%d", &num_items);
            total = num_items * 15.0;
            break;

This is where the code keeps on asking how many new items would you like?

        case 2:
            printf("How many old items would you like?\n");
            scanf("%d", &num_items);
            total = num_items * USEDGEAR;
            break;

This is where the code keeps on asking how many old items would you like? case 3: break;

        default:
            printf("Not a valid option\n");
            break;
        }
    }
    printf("Your total cost is %f\n",total);

    return 0;
}

Both my choices are looping

Upvotes: 2

Views: 275

Answers (3)

chqrlie
chqrlie

Reputation: 144685

Your loop logic is flawed:

  • you should move the prompt code inside the loop.
  • you should update total for each answer.
  • you should test if scanf() is successful at converting user input.

Here is a modified version:

#include <stdio.h>

#define NEWGEAR  15.0
#define USEDGEAR  5.0

int main() {
    int type;
    int num_items;
    double total = 0;

    printf("Welcome to the market\n");
    for (;;) {
        printf("What would you like to do\n");
        printf("\t1-Buy New Gear\n");
        printf("\t2-Buy used gear\n");
        printf("\t3-Quit\n");

        if (scanf("%d", &type) != 1 || type == 3)
            break;

        switch (type) {
        case 1:
            printf("How many new items would you like?\n");
            if (scanf("%d", &num_items) == 1)
                total += num_items * 15.0;
            break;

        case 2:
            printf("How many old items would you like?\n");
            if (scanf("%d", &num_items) == 1)
                total += num_items * USEDGEAR;
            break;

        default:
            printf("Not a valid option\n");
            break;
        }
    }
    printf("Your total cost is %f\n", total);

    return 0;
}

Upvotes: 2

Whooper
Whooper

Reputation: 625

I think this will handle what you want to achieve.

#include <stdio.h>
#include <stdlib.h>

#define NEWGEAR 15.0
#define USEDGEAR 5.0

int main() {
    int type = 0;
    int num_items;
    float total;

    printf("Welcome to the market\n");
    printf("What would you like to do\n");
    printf("\t1-Buy New Gear\n");
    printf("\t2-Buy used gear\n");
    printf("\t3-Quit\n");

    while (type != 3) {
      printf("Enter an option: ");
      scanf("%d", &type);

      if(type == 3)
        break;

      switch (type) {
        case 1:
            printf("How many new items would you like?\n");
            scanf("%d", &num_items);
            total = num_items * 15.0;
            break;
        case 2:
            printf("How many old items would you like?\n");
            scanf("%d", &num_items);
            total = num_items * USEDGEAR;
            break;
        default:
            printf("Not a valid option\n");
            break;
        }
    }
    printf("Your total cost is %f\n",total);

    return 0;
}

Basically, after your user finished doing cases 1-2 or default, your program would prompt your user for an option again if he wants to quit or perform another case 1 or 2 operation.

Upvotes: 2

Tillson
Tillson

Reputation: 530

You never update the type variable to 3, so the while loop never terminates. Although you do have a break statement, it is affecting the switch and not the while loop that surrounds it.

Upvotes: 2

Related Questions