C++ 试图声明函数的局部变量,但获取作用域错误
下面是我的一小段代码。您在这里看到的代码片段有两个目的:1)显示一个菜单——这是menu()函数;2)提示用户进行选择并存储它——selection() selection()从第18行开始 我试图声明一些变量,这些变量将是我的menu()和selection()函数的局部变量,但我会遇到如下错误,对于我试图声明的所有内容: 警告:未使用的变量's'(它为我的所有'char'变量提供了此选项) 或 错误:“snack_selection”未在此范围内声明(它为我试图声明的两个“int”变量提供了此选项) 但这怎么可能呢?我不知道我错过了什么 感谢患者的反馈C++ 试图声明函数的局部变量,但获取作用域错误,c++,c++11,scope,C++,C++11,Scope,下面是我的一小段代码。您在这里看到的代码片段有两个目的:1)显示一个菜单——这是menu()函数;2)提示用户进行选择并存储它——selection() selection()从第18行开始 我试图声明一些变量,这些变量将是我的menu()和selection()函数的局部变量,但我会遇到如下错误,对于我试图声明的所有内容: 警告:未使用的变量's'(它为我的所有'char'变量提供了此选项) 或 错误:“snack_selection”未在此范围内声明(它为我试图声明的两个“int”变量提供了
int menu(void)
{
cout << "Available snacks to select from: " << endl //presents menu
<< " " << setw(5) << "P - Potato Chips" << setw(9) << "$1.25"
<< endl //prompts user to make a selection (until it is valid)
<< " " << setw(5) << "S - Snickers Bar" << setw(9) << "$1.35"
<< endl
<< " " << setw(5) << "T - Pop Tart" << setw(13) << "$0.95"
<< endl
<< " " << setw(5) << "C - Cookies" << setw(14) << "$1.50"
<< endl
<< " " << setw(5) << "B - Brownie" << setw(14) << "$1.75"
<< endl
<< " " << setw(5) << "N - Nuts" << setw(17) << "$1.40"
<< endl;
selection();
return 0;
}
int selection(void) //FUNCTION: welcomes, presents menu, and prompts user for selection
{
int potato_chip_price = 125, snickers_bar_price = 135, pop_tart_price = 95, cookies_price = 150, brownie_price = 175, nuts_price = 140;
char snack_selection = 0; //condition for snack_selection switch case statement
char P, S, T, C, B, N; //the char variables needed for the snack_selection
cout << "Please enter the letter labeling your snack selection: ";
cin >> snack_selection;
cout << endl;
switch(std::toupper(snack_selection))
{ //beginning of switch statement (goes through all the "legal" options so there is an "illegal" option
case 'P': price = potato_chip_price; //setting it up this way stores the value of price with the value of potato_chip_price. if the other way around, pop_tart_price would become 0 since that is what price is set to.
break;
case 'S': price = snickers_bar_price; //stores the snack selection price in CENTS to the int price variable needed for the next function's argument
break;
case 'T': price = pop_tart_price;
break;
case 'C': price = cookies_price;
break;
case 'B': price = brownie_price;
break;
case 'N': price = nuts_price;
break;
default:
cout << "Invalid selection!";
menu();
} //end of switch statement
return(price); //needs to return some kind of value bc it isn't a void function, and compiler will give a warning
}
int菜单(无效)
{
cout代码编译得很好,但包含未使用变量的警告。在3个位置声明未使用的char
变量。删除这些声明可以消除警告。这里我发布了修改后的代码
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;
//Variable Declarations
int price = 0, total_paid = 0, total_price = 0; //arguments for the three functions in the program.need to be global
//Function Declarations:
int menu(void);
// presents menu
int selection(void);
//prompts user to make selection and stores it
int accept_money(int price);
//tells user what money it accepts and the label for each type
//1. repeats back what cost is
//2. tells what has been paid
//3. allows user to input money by payment selection here
int compute_change(int total_paid, int total_price);
//1. tells user what total amt was paid
//2. computes change
//3. tells user what change will be
//4. ask user if he or she would like another purchase (if statement. if y, send back to int menu(). if no, thank for purchase message.)
int main()
{
cout << "Welcome to the snack vending machine" << endl << endl;
menu();
accept_money(price);
compute_change(total_paid, total_price);
return 0;
}
//Function Definitions
int menu(void)
{
cout << "Available snacks to select from: " << endl //presents menu
<< " " << setw(5) << "P - Potato Chips" << setw(9) << "$1.25"
<< endl //prompts user to make a selection (until it is valid)
<< " " << setw(5) << "S - Snickers Bar" << setw(9) << "$1.35"
<< endl
<< " " << setw(5) << "T - Pop Tart" << setw(13) << "$0.95" <<
endl
<< " " << setw(5) << "C - Cookies" << setw(14) << "$1.50" <<
endl
<< " " << setw(5) << "B - Brownie" << setw(14) << "$1.75" <<
endl
<< " " << setw(5) << "N - Nuts" << setw(17) << "$1.40" <<
endl;
selection();
return 0;
}
int selection(void) //FUNCTION: welcomes, presents menu, and prompts user for selection
{
int potato_chip_price = 125, snickers_bar_price = 135, pop_tart_price = 95, cookies_price = 150, brownie_price = 175, nuts_price = 140;
char snack_selection = 0; //condition for snack_selection switch case statement
cout << "Please enter the letter labeling your snack selection: ";
cin >> snack_selection;
cout << endl;
switch ( std::toupper(snack_selection) )
{ //beginning of switch statement (goes through all the "legal" options so there is an "illegal" option
case 'P': price = potato_chip_price; //setting it up this way stores the value of price with the value of potato_chip_price. if the other way around, pop_tart_price would become 0 since that is what price is set to.
break;
case 'S': price = snickers_bar_price; //stores the snack selection price in CENTS to the int price variable needed for the next function's argument
break;
case 'T': price = pop_tart_price;
break;
case 'C': price = cookies_price;
break;
case 'B': price = brownie_price;
break;
case 'N': price = nuts_price;
break;
default:
cout << "Invalid selection!";
menu();
} //end of switch statement
return(price); //needs to return some kind of value bc it isn't a void function, and compiler will give a warning
}
int accept_money(int price)
{
int nickel = 5, quarter = 25, dollar = 100;
char money_selection = 0; //condition for money_selection switch case statement
cout << "Money accepted by the machine:\n"
<< " " << setw(5) << "N - Nickel" << endl
<< " " << setw(5) << "Q - Quarter" << endl
<< " " << setw(5) << "D - Dollar" << endl << endl;
do
{
cout << "Your selected snack item cost: " << price << " CENTS" << endl
<< "Your total inserted: " << total_paid << " CENTS" << endl
<< "Insert amount (enter letter of choice): ";
cin >> money_selection;
cout << endl;
switch ( std::tolower(money_selection) )
{
case 'n': total_paid = nickel + total_paid;
cout << endl;
break;
case 'q': total_paid = quarter + total_paid;
cout << endl;
break;
case 'd': total_paid = dollar + total_paid;
cout << endl;
break;
default:
cout << money_selection << " is not recognized as a coin." << endl << endl;
}
} while ( total_paid < price );
return total_paid; //this function needs to return updated value, or any changes made to it will remain local to the function
}
int compute_change(int total_paid, int total_price)
{
char continue_purchase = 0;
int change = total_paid - price; //has to have somewhere to be stored in this function to work
int amt_inserted = total_paid; //was not using updated value of total_paid from previous fnction, so had to save it to a variable local to this one from the global variable
cout << "Your total inserted: " << amt_inserted << " CENTS" << endl
<< "Dispensing change: " << change << setw(5) << " CENTS" << endl
<< "Would you care to make another purchase (Y/N): ";
cin >> continue_purchase;
switch ( std::tolower(continue_purchase) )
{
case 'y': total_paid = 0;
price = 0;
total_price = 0;
cout << endl << endl;
menu();
break;
case 'n': cout << "Thank you and enjoy your purchase!";
break;
default:
cout << "Invalid Selection" << endl << endl;
compute_change(total_paid, total_price);
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
//变量声明
int price=0,total_paid=0,total_price=0;//程序中三个函数的参数。必须是全局参数
//函数声明:
int菜单(无效);
//赠送菜单
int选择(void);
//提示用户进行选择并将其存储
国际货币(国际价格);
//告诉用户它接受多少钱以及每种类型的标签
//1.重复成本是多少
//2.说明已支付的金额
//3.允许用户在此处通过付款选择输入资金
int计算变更(支付的int总额,支付的int总价);
//1.告诉用户支付的总金额
//2.计算更改
//3.告诉用户将进行哪些更改
//4.询问用户是否希望再次购买(如果是语句,则返回int menu()。如果不是,则感谢购买消息。)
int main()
{
cout除了大家在评论中提到的不使用全局变量和其他良好编程实践之外,代码本身编译得很好(除非您在编译器标志中像我一样激活了-Werror开关),但有警告
您似乎误解了,如果要在case语句中使用字符串文字“p”,就像在函数int selection(void)中所做的那样
,您必须首先声明一个变量char P
。使用这些字符串文字不必声明变量。变量char P和字符串“P”之间没有任何联系
警告来自以下几行:
在函数int compute\u change(int total\u paid,int total\u price)
中删除以下行:
char y;
char n, q, d; //the char variables needed for the money_selection switch case statement
然后在函数int accept\u money(int price)
中删除以下行:
char y;
char n, q, d; //the char variables needed for the money_selection switch case statement
最后在函数int selection(void)//函数中:欢迎、显示菜单并提示用户选择
拆下以下行
char P, S, T, C, B, N; //the char variables needed for the snack_selection switch case statement (can't be inside function, needs to be global
在那之后你会没事的
如果你真的想使用这些变量,p,S等等,那么你应该给它们赋值并声明它们为常量。我在这里做了一个例子,让你看看。你可以按照类似的思路做其余的事情
int compute_change(int total_paid, int total_price)
{
char continue_purchase = 0;
const char y = 'y';
int change = total_paid - price; //has to have somewhere to be stored in this function to work
int amt_inserted = total_paid; //was not using updated value of total_paid from previous fnction, so had to save it to a variable local to this one from the global variable
cout << "Your total inserted: " << amt_inserted << " CENTS" << endl
<< "Dispensing change: " << change << setw(5) << " CENTS" <<
endl
<< "Would you care to make another purchase (Y/N): ";
cin >> continue_purchase;
switch(std::tolower(continue_purchase))
{
case y:
total_paid = 0;
price = 0;
total_price = 0;
cout << endl << endl;
menu();
break;
case 'n': cout << "Thank you and enjoy your purchase!";
break;
default:
cout << "Invalid Selection" << endl << endl;
compute_change(total_paid, total_price);
}
return 0;
}
编译器会警告您未使用的变量/参数,因为它会显示您的疏忽/遗漏等真正错误(例如,在本例中,您似乎误解了声明char变量需要使用与该char变量名称包含相同文本的字符串文字)。有关此问题的详细信息,请参阅此问题:
希望对您有所帮助。您显示的代码从未声明名为d
、未使用或其他的变量。您引用的错误消息似乎与未显示的某些代码有关。您从未声明price
?在菜单()
之前添加选择()的原型或移动选择()的定义
在菜单()之前
字符P、S、T、C、B、N;
这些变量都没有被使用。这些是名为P
、S
等的char
类型的变量,与literalchar
值'P'
、'S'
等明显不同。删除这一行将消除一些关于未使用变量的警告。您需要发布错误消息的完整来源(包括任何#include
指令)和完整文本,与屏幕上显示的内容完全一致。使用链接可为您的问题添加必要的信息。