我正在研究计算器控制台应用程序。我是C ++的初学者我希望程序在打印完之前的结果后提出问题。我收到以下错误,我不知道如何解决它。
标识符“choice”未定义
这是我的源代码:
#include "stdafx.h"
#include <iostream>
int getUserInput() {
std::cout << "Please enter an integer: ";
int value;
std::cin >> value;
return value;
}
int getConversionFormula() {
std::cout << "What do you want to convert? (1 for Meters to feet, 2 for kilometers to miles, 3 for kilagrams to pounds.): ";
int op;
std::cin >> op;
//user might select an invalid operation that isnt there
//Implement a way to avoid this.
return op;
}
int getUserInput2() {
std::cout << "Do you want to convert anything else? (hit 1 for yes, hit 2 for no): ";
int choice; //for the user if they hit yes or no to the question above.
std::cin >> choice;
return choice;
}
int getUserChoice(int choice) {
//If user selects 1 show the converstion formula screen
if (choice == 1)
getConversionFormula();
if (choice == 2)
std::exit;
return -1;
}
int calculateResult(int x, int op) {
//we will use == to compare two variables to see if they are true or not
if (op == 1)
return x * 3.280839895; //meters to feet
if (op == 2)
return x / 1.609344; //km to miles
if (op == 3)
return x * 2.2046; //kg to pounds
return -1; //If the user entered an invalid operation
}
void printResult(int result) {
std::cout << "Your result is: " << result << std::endl;
}
int main()
{
int input1 = getUserInput(); //Gets the users input
int op = getConversionFormula();
int result = calculateResult(input1, op);
printResult(result);
int input2 = getUserInput2(); //Asks the user if they want to convert anything else
int input3 = getUserChoice(input2, choice);
std::cin.clear(); // reset any error flags
std::cin.ignore(32767, '\n'); // ignore any characters in the input buffer until we find an enter character
std::cin.get(); // get one more char from the user
}
有人可以告诉我如何解决它吗?我在主函数中得到错误只是为了让每个人都知道。
答案 0 :(得分:1)
choice
未在main
中声明,它仅在getUserInput2
中返回。 getUserChoice
甚至没有两个参数,所以就这样做:
getUserChoice(input2);
你可以开始链接你的函数调用,例如:getUserChoice(getUserInput2());
,从而消除一些局部变量。