否则会陷入循环困境
else if looping quandry
我在一个项目的第二阶段,我需要扩展我的程序到一个菜单驱动的应用程序来查询数据库,我有一个。txt文件。我的问题是我不能让循环是永久的。它总是在从一个选项初始化到下一个选项时终止。下面是我的int main代码片段:
int main ()
{
char Q,q;
char S,s;
char task;
string pathname;
string z;
int count=0;
cout << "Welcome to Jason Rodriguez's Library Database." << endl;
cout << "Please enter the name of the backup file: ";
cin >> pathname;
ifstream inFile(pathname.c_str());
while(!inFile.eof())
{
getline(inFile,z);
count++;
}
while (task != 'Q' || task != 'q') {
cout << count << " records loaded successfully." << endl;
cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: ";
cin >> task;
if ((task == 'Q')||(task =='q'))
{
cout << "Program will now terminate";
break;
}
else if ((task == 'S')||(task =='s'))
{
showAll (loadData (pathname));
cout << endl;
cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: ";
cin >> task;
}
}
}
我需要在这两个选项之上再添加两个选项到循环中,但我认为我应该先让前两个正确工作。另外两个应该是插头&然后再喝。基本上我想做的是,如果用户输入Q或Q,终止程序。否则,如果用户点击S或S,则激活showall功能,然后返回到原始查询。但这行不通。欢迎协助并感谢。
菜单几乎总是需要循环 - 特别是需要用户输入正确的选择输入的菜单。在这种情况下,最适用的循环是while循环——但本质上,任何其他循环变体都可以使用。
更新:int main ()
{
char task;//this is the only char needed. Your other chars were redundant
string pathname;
string temp;//I changed z to temp to better reflect its purpose
int count=0;
cout << "Welcome to Jason Rodriguez's Library Database." << endl;
cout << "Please enter the name of the backup file: ";
cin >> pathname;
ifstream inFile(pathname.c_str());//this is potentially a problem in that you aren't verifying that the pathname is a valid one
//you did not check to see that your file was open, otherwise there is no way to tell that you successfully opened the file
if (inFile.is_open()) {
//while(!inFile.eof()) is a character by character read and comparison
//made your life easier by shortening it down to this - which ensures
//that a line is read. (Much faster and more readable)
while(getline(inFile,temp))
{
count++;
}
inFile.close();//always close a file after you've used it
//At this point the entire file has been read. So, this is where this message SHOULD be
cout << count << " records loaded successfully." << endl;
}
else {
//if there was an error opening the file (i.e. wrong path, or it simply does not exist), this will be displayed
cout << "There was a problem opening your file" << endl;
exit(0);//and the program will terminate
}
while (task != 'Q' || task != 'q') {
cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: ";
cin >> task;
if ((task == 'Q')||(task =='q'))
{
cout << "Program will now terminate";
break;
}
else if ((task == 'S')||(task =='s'))
{
string author;
//showAll (loadData (pathname));
cout << endl;
cout << "Search an Author" << endl;
cin >> author;//get the author name to search from the user
//write code to search an author here
}
}
}
你发布的代码有很多问题,为了简洁起见,我将放弃。因此,请注意以下内容:
你的代码对每个选项打印相同的消息(除了quit)。当然,它看起来不工作。每个选项都是一个不同的任务。打印每个任务所做的(类似于我所做的)。
您希望在文件中搜索作者,但是您还没有存储它。寻找一种能让你的老师满意的存储方法。
考虑到代码日益增加的复杂性,在这种情况下使用
switch
是最理想的。尝试将每个任务分解为函数,并调用它们以使主函数可读。实际上,main函数越小越好,这是一个很好的编程习惯。
并且,正如juanchopanza非常正确地指出的那样:c++存在一些基本问题。试着从一本好的c++书中多做一些练习和例子。
相关文章:
- 如何循环打印顶点结构
- 如何在C++中从两个单独的for循环中添加两个数组
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 正在尝试了解输入验证循环
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 循环后如何继续阅读
- Ardunio UNO解决了多个重叠的定时器循环
- Eigen如何在容器循环中干净地附加矩阵
- 在某些循环内使用vector.push_back时出现分段错误
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- while循环中while循环的时间复杂度是多少
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 为什么在这个代码结束循环中没有得到结束
- 在基于范围的for循环中使用结构化绑定声明
- 用于C++中带有数组和指针的循环
- 循环中的随机函数
- 循环时我如何摆脱困境
- 否则会陷入循环困境