issue with using a do while with a switch

new to working with loops, I had an original program made to be able to chose a few classes from different CRN's and then be charged for the various credit hours the represented.

Now Im pretty new to do while loops but I figured I could have an if else inside the do while and just keep looping it. My aim was to let the loop exit on every option chosen BESIDES the option where you were choosing above the allotted amounts of classes (in this case the max amount of classes are 3).

It seems it does not want to loop though, is this a poor way of doing a do while loop and is there a more elegant way of approaching these?

do{

printf("Please enter Student ID:\n");
scanf("%i", &studentId);
printf("Enter how may courses-up to 3:\n");
scanf("%f", &amount);


    if(amount == 1){
        printf("Enter the course number:\n\n");
        scanf("%d", &course);
        switch(course)
           {
             case 4587:
                credit1 = 4;
                break;
            case 4599:
                credit2 = 3;
                break;
            case 8997:
                credit3 = 1;
                break;
            case 9696:
                credit4 = 3;
                break;
            default:
                printf("Sorry invalid entry!\n\n");

                return 0;
          }
        a = 1;
    }
    else if(amount == 2){
        printf("Enter the 2 course numbers\n");
        scanf("%d %d", &course, &course1);
            switch(course)
             {
             case 4587:
                credit1 = 4;
                break;
            case 4599:
                credit1 = 3;
                break;
            case 8997:
                credit1 = 1;
                break;
            case 9696:
                credit1 = 3;
                break;
            default:
                printf("Sorry invalid entry!\n\n");
                return 0;
            }
          switch(course1)
            {
             case 4587:
                credit2 = 4;
                break;
            case 4599:
                credit2 = 3;
                break;
            case 8997:
                credit2 = 1;
                break;
            case 9696:
                credit2 = 3;
                break;
            default:
                printf("Sorry invalid entry!\n\n");
                return 0;
            }
           a = 1;
        }

    else if(amount == 0){
        printf("Thank you!\n\n");
        a = 1;
    }
    else if(amount > 3){
        printf("Invalid number of courses (up to 3)\n");
        a = 0;

    }
    else{
        printf("Sorry, we cant process your request this time-invalid number of courses.\n\n");
        printf("\t\t\tTry again. Goodbye!\n");
        return 0;
    }

}while(a = 0);

Upvotes: 0

Views: 61

Answers (1)

Loc Tran
Loc Tran

Reputation: 1180

Replace the line

}while(a = 0);

by

}while(a == 0);

Please do that to allow the loop can execute. The expression (a=0) is always evaluated to true value, so the loop can not execute

Upvotes: 2

Related Questions