切换案例后如何阻止变量复位? 我是C++新手。我正在尝试编写一个程序,要求用户选择:
[a] 输入数字[b]读取数字[q]退出程序 如果用户选择了一个数字,则会要求他输入一个数字,程序将保存该数字。切换案例后如何阻止变量复位? 我是C++新手。我正在尝试编写一个程序,要求用户选择:,c++,class,variables,initialization,switch-statement,C++,Class,Variables,Initialization,Switch Statement,[a] 输入数字[b]读取数字[q]退出程序 如果用户选择了一个数字,则会要求他输入一个数字,程序将保存该数字。 如果用户选择b,程序将显示输入的号码。 如果用户选择q,程序将退出 但是,在我的程序中,我无法确定用户输入数字并选择读取后,“存储”变量将被重置,无法显示输入的数字 #include <iostream> #include <string> using namespace std; class cNumber //declare the clas
如果用户选择b,程序将显示输入的号码。
如果用户选择q,程序将退出 但是,在我的程序中,我无法确定用户输入数字并选择读取后,“存储”变量将被重置,无法显示输入的数字
#include <iostream>
#include <string>
using namespace std;
class cNumber //declare the class
{
public: void WriteRead(char cmd); //a for write; b for read
bool Quit();
private: int storage;
int input;
};
void cNumber::WriteRead(char cmd) {
if (cmd == 'a') {
cout << "input:\n";
cin >> input;
storage = input;
}
if (cmd == 'b') {
cout << "Here is your number:\n" << storage << endl;
}
}
bool cNumber::Quit() {
return true;
}
bool menu();
int main() {
bool exitflag = false;
while (exitflag == false) {
exitflag = menu();
}
return 0;
}
bool menu() {
cNumber Test;
char choice; // To store the command of user
cout << "[a] Write number\n" << "[b] Read number\n" << "[q] Quit\n";
cout << "Please enter your choice: ";
cin >> choice;
switch (choice) {
case'a':
Test.WriteRead('a');
cout << "Write thx\n"; break;
case 'b':
cout << "Test Read\n";
Test.WriteRead('b');
cout << "\nRead thx\n"; break;
case 'q':
cout << "\nCloseeeee";
return Test.Quit();
}
return false;
}
#包括
#包括
使用名称空间std;
类cNumber//声明该类
{
public:void WriteRead(char cmd);//a表示写入;b表示读取
bool-Quit();
私有:int存储;
int输入;
};
void cNumber::WriteRead(char cmd){
如果(cmd='a'){
cout>输入;
存储=输入;
}
如果(cmd='b'){
在您的bool菜单()
函数中
cNumber Test;
是一个本地cNumber
对象,它将从函数作用域的端的堆栈中销毁。这意味着存储到私有存储
变量的内容也将随之消失
下次调用bool menu()时
函数再次执行,您正在创建另一个cNumber
的对象,该对象与您以前所做的工作无关。因此,存储区
只包含一些垃圾值,因为它尚未在此函数作用域中初始化。尝试访问未初始化的变量将失败
解决方案是在main()
程序中有一个cNumber
实例,每次通过引用将其传递给函数
也就是说,将代码更改为:
/* other code */
bool menu(cNumber& Test);
// ^^^^^^^^^^^^
int main() {
bool exitflag = false;
cNumber Test; // create a single instance for entire program
while (exitflag == false)
{
exitflag = menu(Test); // and pass to the function by ref
}
return 0;
}
bool menu(cNumber& Test) // and pass to the function by ref
{
char choice;
// remaing code
}
编辑:当OP要求查看解决方案而不使用构造函数时,也可以在类内部初始化成员变量,前提是应使用C++11或更高版本
下面是一个示例代码,它在bool menu()
函数的中编译,如下所示
cNumber Test;
是一个本地cNumber
对象,它将从函数作用域的端的堆栈中销毁。这意味着存储到私有存储
变量的内容也将随之消失
下次调用bool menu()时
函数再次执行,您正在创建另一个cNumber
的对象,该对象与您以前所做的工作无关。因此,存储区
只包含一些垃圾值,因为它尚未在此函数作用域中初始化。尝试访问未初始化的变量将失败
解决方案是在main()
程序中有一个cNumber
实例,每次通过引用将其传递给函数
也就是说,将代码更改为:
/* other code */
bool menu(cNumber& Test);
// ^^^^^^^^^^^^
int main() {
bool exitflag = false;
cNumber Test; // create a single instance for entire program
while (exitflag == false)
{
exitflag = menu(Test); // and pass to the function by ref
}
return 0;
}
bool menu(cNumber& Test) // and pass to the function by ref
{
char choice;
// remaing code
}
编辑:当OP要求查看解决方案而不使用构造函数时,也可以在类内部初始化成员变量,前提是应使用C++11或更高版本
下面是一个在中编译的示例代码。int存储是我遇到问题的全局变量。我使用的是Visual Studio 2017。如果用户在输入后读取数字,则会看到-858993460。@AustinL抱歉,在编辑您的代码时,我只是没有发现中断;
,因为我希望他们在单独的行。你的代码格式化非常不寻常。哦,真的很抱歉我的格式很差,因为我对C++非常陌生。我将尝试使它看起来更好。下一次,It存储是我遇到的全局变量。我使用Visual Studio 2017。如果用户在输入后读取数字,用户将看到-858993460。@ AustinL Sorry,I。当我编辑了你的代码时,没有发现“<代码>”,因为我期望它们在一个单独的行中。你的代码格式化非常不寻常。哦,真的很抱歉我的格式很差,因为我是C++的新手。我会试着让它看起来更好。但是,有一个错误显示关于抱歉,我编辑了以前的评论WRon。gly.我试过了,但是关于这行代码有一个错误:exitflag=menu(Test);它说使用了代码C4700未初始化的局部变量“Test”。C4700未初始化的局部变量“Test”used@AustinL然后我建议提供如下显式构造函数:explicit cNumber(ints,inti):存储(s),输入(I){
并在创建时使用存储和输入的一些初始值初始化对象,如cNumber Test(1,2);
但是,出现了一个错误显示,抱歉,我错误地编辑了前面的注释。我尝试了,但这行代码有一个错误:exitflag=menu(Test);它说使用了代码C4700未初始化局部变量“Test”。C4700未初始化局部变量“Test”used@AustinL然后我建议提供如下显式构造函数:explicit cNumber(ints,inti):存储(s),输入(I){
并在创建时使用一些用于存储和输入的初始值初始化对象,如cNumber Test(1,2);