我正在编写一个程序,该程序输入三个数字,然后计算一些不同的事物(每个事物必须具有其自身的功能)。该程序从告诉用户他们的选项并等待他们的输入开始。在执行任何一种情况后,程序将再次打印菜单,但它将使用默认情况,然后将打印菜单并要求输入。任何帮助将不胜感激。
#include <stdio.h>
#include <stdlib.h>
void greeting() {
printf("Welcome to Dr. Computer's Mathatorium \n"); // this is a seperate function just because
printf("Remember to use capital letters when selecting \n");
}
//This getNum function is used to the get the number
int getNum ()
{
int a;
printf("Enter your first integer:"); //tells user to input number
scanf("%i", &a); //input
return a;
}
// gets the sum of the numbers
int getSum (int f, int g, int h)
{
return (f + g + h);
}
// gets the sum of the numbers
int getPro (int f, int g, int h)
{
return (f * g * h);
}
// gets the sum of the numbers
int getAvg (int f, int g, int h)
{
return (f * g * h)/3;
}
// gets the sum of the numbers
int getLow (int f, int g, int h)
{
return (f + g + h); //NEEDS ADJUSTING
}
main()
{
int first, second, third, sum, pro, avg, low;
char choice;
greeting ();
do {
printf("Main Menu\n");
printf("A) Get Three Integers\n");
printf("B) Display the Sum\n");
printf("C) Display the Product\n");
printf("D) Display the Average\n");
printf("E) Display the lowest\n");
printf("F) Quit\n");
scanf("%c", &choice);
//here comes the switches to route the choices
switch(choice){
case 'A':
first = getNum ();
second = getNum ();
third = getNum ();
printf("first is: %i\n", first);
printf("second is: %i\n", second);
printf("third is: %i\n", third);
break;
case 'B':
sum = getSum (first, second, third);
printf("sum is: %i\n", sum);
break;
case 'C':
pro = getPro (first, second, third);
printf("product is: %i\n", pro);
break;
case 'D':
avg = getAvg (first, second, third);
printf("average is: %i\n", avg);
break;
case 'E':
avg = getAvg (first, second, third); //NOT DONE YET
printf("average is: %i\n", avg); //REMEMBER TO FIX
break;
default:
printf("INVALID CHOICE!\n");
break;
}
} while (choice != 'F');
return 0;
}
最佳答案
尝试以下代码,这是您的程序,但有一些更改:
#include <stdio.h>
void greeting() {
printf("Welcome to Dr. Computer's Mathatorium \n"); // this is a seperate function just because
printf("Remember to use capital letters when selecting \n");
}
int getNum () //This getNum function is used to the get the number
{
int a;
printf("Enter your first integer:"); //tells user to input number
scanf("%i", &a); //input
return a;
}
int getSum (int f, int g, int h) // gets the sum of the numbers
{ return (f + g + h);
}
int getPro (int f, int g, int h) // gets the sum of the numbers
{ return (f * g * h);
}
int getAvg (int f, int g, int h) // gets the sum of the numbers
{ return (f * g * h)/3;
}
int getLow (int f, int g, int h) // gets the sum of the numbers
{ return (f + g + h); //NEEDS ADJUSTING
}
int main(void)
{
int first, second, third, sum, pro, avg;
char choice;
greeting ();
do {
printf("Main Menu\n");
printf("A) Get Three Integers\n");
printf("B) Display the Sum\n");
printf("C) Display the Product\n");
printf("D) Display the Average\n");
printf("E) Display the lowest\n");
printf("F) Quit\n");
fseek(stdin,0,SEEK_END); // change
choice = getc(stdin); // change
//scanf("%c", &choice);
//here comes the switches to route the choices
switch(choice){
case 'A':
case 'a': // change
first = getNum ();
second = getNum ();
third = getNum ();
printf("first is: %i\n", first);
printf("second is: %i\n", second);
printf("third is: %i\n", third);
break;
case 'B':
case 'b': // change
sum = getSum (first, second, third);
printf("sum is: %i\n", sum);
break;
case 'C':
case 'c': // change
pro = getPro (first, second, third);
printf("product is: %i\n", pro);
break;
case 'D':
case 'd': // change
avg = getAvg (first, second, third);
printf("average is: %i\n", avg);
break;
case 'E':
case 'e': // change
avg = getAvg (first, second, third); //NOT DONE YET
printf("average is: %i\n", avg); //REMEMBER TO FIX
break;
default:
printf("INVALID CHOICE!\n");
break;
}
} while (choice != 'F' && choice != 'f'); // change
return 0;
}
关于c - C编程:开关运行两次,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25831913/