尝试声明函数的局部变量,但得到范围错误

trying to declare a variable local to a function, but get scope error

本文关键字:范围 错误 局部变量 声明 函数      更新时间:2023-10-16

这是我的一小段代码。你在这里看到的片段的目的是双重的: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
}

此处的完整代码删除了表面信息:

#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
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
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
char n, q, d; //the char variables needed for the 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;
char 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;
}

除了每个人都在评论中所说的不使用全局变量和其他良好的编程实践之外,代码本身编译得很好(除非您的编译器标志中有 -Werror 开关像我一样处于活动状态。但有警告。

您似乎误解了,如果您要在 case 语句中使用字符串文字"P",就像您在函数int selection(void)中所做的那样,您必须首先为此声明一个变量char P。您不必声明用于使用这些字符串文本的变量。变量 char P 和字符串"P"之间没有任何联系。

警告来自以下行:

在函数int compute_change(int total_paid, int total_price)中删除以下行:

char y;

然后在函数int accept_money(int price)中删除以下行:

char n, q, d; //the char variables needed for the money_selection switch case statement

最后在功能int selection(void) //FUNCTION: welcomes, presents menu, and prompts user for selection

删除以下行。

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

但为了清楚起见,变量 y 可以命名为任何东西——它不必被命名为 y 来存储"y"。如果我将该行替换为const char hakunamatata = 'y',代码会正常工作

int compute_change(int total_paid, int total_price)
{
char continue_purchase = 0;
const char hakunamatata = '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 hakunamatata:
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 变量名称相同的文本的字符串文字(。有关此问题的更多信息,请参阅此问题: 忽略的后果是什么: 警告: 未使用的参数

希望有帮助。

代码编译良好,但包含未使用变量的警告。在 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;
}