C++待办事项列表重复输入.如何避免这种情况?

C++ To-Do List repeats inputs. How to avoid that?

本文关键字:何避免 情况 输入 列表 C++      更新时间:2023-10-16

我有以下代码,我正在尝试创建一个非常简单(不那么简单(的待办事项列表。 期望的结果如下:

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"( 调用此函数时,它将打印所有值。

相关文章: