我正在尝试做的这个任务只是有一些问题。基本上我需要一个菜单,4个选项,其中两个接受用户的输入,作为基数和指数的形式。第三个输出base raise 的答案,然后第四个退出程序。
我无法通过 getNum() 获取用户输入;我不太确定如何正确使用它。只是看一些关于如何让我的代码更好地工作的技巧。
寻求帮助:
- 接受来自两个不同功能的用户输入并使用它来输出答案
- 解决选择菜单选项时的无限循环问题
每个功能完成后循环程序回到主菜单,只有在选择菜单选项 4 时才退出程序
int main(void) { int option = 0; do { loadMenu(); while (option<1 || option>4) { printf("\nChoose an option between 1 and 4:"); option = getNum(); while (getNum() != '\n'); } switch (option) { case 1: baseChange(); //Gets base number break; case 2: powerChange(); //Gets exponent break; case 3: calcMath(); //Calculates the answer break; default: break; } } while (option != 4); printf("Goodbye!\n"); } void loadMenu() //Menu choices { printf("Power Menu:\n" ); printf(" 1. Change base\n"); printf(" 2. Change exponent\n"); printf(" 3. Calculate\n"); printf(" 4. Exit\n"); printf("Option?\n"); } int baseChange(int base) { printf("What is your base?: "); base = getNum(); while (getNum() != '\n'); return base; } int powerChange(int power) { printf("What is the power?: "); power = getNum(); while (getNum() != '\n'); return power; } int calcMath(int base, int power) { int index = 0; long answer = 1.00; for(index = 1; index <= power; index++) answer = answer * base; { printf("%d raised to the power of %d is %ld.\n\n", base, power, answer); } return answer; }