gpt4 book ai didi

c++ - 获取标识符选择未定义

转载 作者:行者123 更新时间:2023-11-28 05:54:42 33 4
gpt4 key购买 nike

我正在开发一个计算器控制台应用程序。我是 C++ 的初学者,我希望程序在打印完之前的结果后提出问题。我收到以下错误,但我不知道如何修复它。

identifier "choice" is undefined

这是我的源代码:

#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
}

有人可以告诉我如何解决吗?我在 main 函数中得到错误只是为了让大家知道。

最佳答案

choice 没有在 main 中声明,它只在 getUserInput2 中声明,它返回。 getUserChoice 甚至不需要两个参数,所以只需执行以下操作:

getUserChoice(input2);

您可以开始稍微链接您的函数调用,例如:getUserChoice(getUserInput2());,从而消除一些局部变量。

关于c++ - 获取标识符选择未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34444755/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com