C++待办事项列表重复输入.如何避免这种情况?
C++ To-Do List repeats inputs. How to avoid that?
我有以下代码,我正在尝试创建一个非常简单(不那么简单(的待办事项列表。 期望的结果如下:
1(醒来 2(感谢上帝,我还活着。
。C++
#include <iostream>
#include <string>
using namespace std;
int main() {
string list_entry;
cout << "What would you like to add to your to-do list?" << endl;
int count = 1;
while (true) {
getline(cin,list_entry)
cout << count << ")" << list_entry << endl;
count++;
}
return 0;
}
。C++
。输出
我得到以下输出,这不是预期的结果:
What would you like to add to your to-do list?
Wake up
1)Wake up
Thank God I'm alive
2)Thank God I'm alive
Make the bed
3)Make the bed
。输出
期望的结果如下:
您想在待办事项列表中添加什么? 1(醒来 2(感谢上帝我还活着 3(铺床 等。
// Simple ToDo List C++.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h" edited out
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
//function prototypes required in C++
int printvalues(string str1);
int main()
{
string list_entry = "";
std::cout << "What would you like to add to your to-do list?" << std::endl;
// while(true) is an infinite loop. It exits when exit is sent to
// printvalues and it evaluates to the exit(0); which means exit with an
// error code of 0. Other way to exit a loop is with the break keyword.
while (true) {
getline(std::cin, list_entry);
if (list_entry != "print") { printvalues(list_entry); }
if (list_entry == "print") { printvalues("print"); }
}
// return required by most compilers is an int. main is an int in this
// program so return returns an int value.
return 0;
};
// function "definition" for the function we put at the top. defines the
// body of the function
int printvalues(string str1) {
// static variables in C++ will retain their set values next time this
// function is called withe printvalues(list_entry) or
// printvalues("print") or printvalues("exit")
static int i = 0;
static string all[30];
// if list_entry is not != the word print, add the value at index i and
// increment i using i++;
if (str1 != "print") { all[i] = str1; i++ }
//iterator i2
int i2;
// if we typed print inside the while (true) loop in main then print all
// the values in a for loop starting at all[i2].
if (str1 == "print") {
for (i2 = 0; i2 < i; i2++) {
//print i2 + 1 makes the value start at 1 so we don't print out
// 0) Make the bed , we print out 1)
cout << i2 + 1 << ")" << all[i2] << endl;
}
}
// if exit was typed then the values are stored but it doesnt matter
// because they aren't printed and the program exits with a error code
// of 0 which is success.
if (str1 == "exit") { exit(0); }
return 0;
}
我在一个新项目中的Visual Studio中运行了这个确切的代码,它起作用了..键入每个项目并按回车键..完成后,在新行上键入打印并按回车键,它将打印值。
引自https://en.cppreference.com/w/cpp/string/basic_string/getline
b( 下一个可用的输入字符是 delim,由 Traits::eq(c, delim( 测试,在这种情况下,分隔符是从输入中提取的,但不附加到 str。
没有必要调用 ignore(( 来跳过带有 std::getline(( 的 ''。
int printvalues(string);
main() {
while (true) {
getline(cin, list_entry); //cin.ignore();
if (list_entry != "print") {
printvalues(list_entry);
}
if (list_entry == "print") {
printvalues("print");
}
}
}
int printvalues(string str1) {
static int i = 0;
static string all[3];
all[i] = str1;
i++;
int i2;
if (str1 == "print") {
for (i2 = 0; i2 <= i; i2++) {
cout << i2 << ")" << all[i2] << endl;
}
exit(0);
}
}
当您使用 printvalues(list_entry( 读取任何字符串时调用此函数,当您完成使用 printvalues("print"( 调用此函数时,它将打印所有值。
相关文章:
- 在没有太多条件句的情况下,我如何避免被零除
- 我们应该如何使用枚举类进行索引(或者我们应该更好地避免这种情况)?
- 使用 std::istream_iterator 时,它似乎跳过了空文件行 - 如果可能的话,如何避免这种情况?
- C++待办事项列表重复输入.如何避免这种情况?
- C++ 模板类:运行时给出的模板参数,如何避免重复大开关情况?
- 在这种情况下,我可以避免使用带有主体的纯虚函数吗?
- lower_bound 在 C++ STL 中返回迭代器,即使元素不存在也是如此.如何避免这种情况?
- 为什么我可以在不链接任何额外库的情况下包含 sys/*.h
- 应由于计算费用而避免对STD的多个调用::发现,否则这是可以接受的情况
- 如何避免字符串到整数转换情况下的无效参数异常
- 在没有互斥锁的情况下重新计数时如何避免竞争条件?
- 在这种情况下是否可以避免使用虚拟方法调用?
- 具有空参数包的递归可变参数模板(以避免基本情况的重复)
- 我可以在不使用原始指针的情况下避免在std :: pinitializer_list的Intialization过程中复
- 随机洗牌似乎会产生相同的结果,但仅在前两个实例中产生相同的结果.我如何避免这种情况
- POSIX pthread_create对结构中的变量值进行打乱,如何避免这种情况
- Qt:避免在字符串更改的情况下重建应用程序
- 是否有应该避免使用constexpr的情况,即使它可以被使用?
- 如何在不创建空类的情况下避免"big"模板类的专用化?
- 字体在Windows 8上变小 - 在C++/MFC程序中.如何避免这种情况