【发布时间】:2017-03-13 05:35:37
【问题描述】:
我为此使用 Ubuntu 终端 g++ 编译器,我很难调试
这是我的代码: 嘘
class S
{
protected:
string name;
bool space;
public:
S();
S(string,bool);
void setName(string name);
string getName();
};
在我的 S.cpp 中
string S::getName()
{
return (name);
}
void S::setName(string name) // I tried to change to other variable names but gave me segmentation errors
{
name = name;
}
S::S() // default constructor
{
name = "";
space = true;
}
S::S(string name, bool space) // non-default constructor
{
name = name;
space= space;
}
我有另一个类,我要实现流程 在我的 Main.cpp 中
void Main::mainmenu()
{
//cout displays//
cout<<"Please input your choice: ";
cin>>choice;
option(choice);
}
void Main::option(int choice)
{
static string shapename;
static bool space;
const int size = 100;
static S s[size];
static int number =0;
static int count = 0;
while(choice !=999)
{
switch(choice)
{
case 1:
{
cout<<"Please enter name of Shape:";
cin>>shapename;
s[size].setName(shapename);
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
count++;
mainmenu();
cin>>number;
option(number);
}
break;
case 2:
{
for(int x = 0;x<count;x++)
{
cout<<count<<endl;
cout<<shapename<<endl; // this is to test if the string reaches case 2 and it displays the correct input
cout<<s[x].getName()<<endl; // however this method prints a blank.
}
mainmenu();
cin>>number;
option(number);
}
break;
default:break;
} // end of switch
} // end of while
}
我只是在我的int main() 中运行mainmenu()
我正在尝试在我的 main.cpp 上做一个切换案例,其中案例 1 我让用户输入他们想要的名称,我使用类 S 中的 set 方法来获取名称,在我的案例 2 中,我想要使用 get 方法检索并打印出用户输入的名称。
我将 static 放在我的声明前面,因为我认为调用可能会影响值。
如何使用 switch case 语句检索我的 get/set 方法?我正在使用 Ubuntu 终端来执行此操作,这对我来说很难看到传入的值。
【问题讨论】:
-
运行多次后你会得到啊,呃,堆栈溢出。再想想代码的逻辑
标签: c++ while-loop switch-statement accessor mutators