Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

C code, what am I missing for this control statement program?

I am working on a program. This is what I am attempting to achieve; The program should present a menu of pay rates from which to choose. Use a switch to select the pay rate. The beginning of a run should look something like this: enter image description here

If choices 1 through 4 are selected, the program should request the hours worked. The program should recycle until 5 is entered. If something other than choices 1 through 5 is entered, the program should remind the user what the proper choices are and then recycle. Use #defined constants for the various earning rates and tax rates.

This is the code I have so far, what am I missing?;

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

int main()
{
int choice, hour;
float taxe, total;

printf("****************************************************************
");
printf("
Enter the number corresponding to the desired pay rate or action");
printf("
1)$8.75/hr");
printf("
2)$9.33/hr");
printf("
3)$10.00/hr");
printf("
4)$11.20hr");
printf("
5)Quit");
printf("**********************************************************
");

scanf("%d", &choice);

printf("Please enter number of hours: ");
scanf("%d", &hour);

switch(choice){
case 1:
    total = 8.75* hour;
    break;
case 2:
    total = 9.33*hour;
    break;
case 3:
    total = 10.00*hour;
    break;
case 4:
    total = 11.20*hour;
    break;
case 5:
     break;
return 0;
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

what am I missing?

What you are missing is the actual iteration.

The program should recycle until 5 is entered.

This means that you have to do all your work inside a loop, which will check each time if the value read is 5, which you need in order to stop iterating. You can use one of the following ways :

  1. while loop:

    scanf("%d", &choice);
    while (choice != 5)
    {
        ....
        switch(choice){
            ....
        }
        ....
     scanf("%d", &choice);
    }
    
  2. for loop:

    for (scanf("%d", &choice); choice != 5; scanf("%d", &choice);)
    {
        ....
        switch(choice){
            ....
        }
        ....
    }
    
  3. do-while loop:

    do{
        scanf("%d", &choice);
        ....
        switch(choice){
            ....
        }
        ....
    }while(choice != 5);
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...