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

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

I have the following code, and I'm trying to create a very simple (not so simple) to-do list. The desired outcome is as follows:

1) Wake up 2) Thank God I'm alive.

...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

...输出

The desired outcome is as follows:

What would you like to add to your to-do list? 1) Wake up 2) Thank God I'm Alive 3) Make the bed etc.

引自https://en.cppreference.com/w/cpp/string/basic_string/getline

b) the next available input character is delim, as tested by Traits::eq(c, delim), in which case the delimiter character is extracted from input, but is not appended to str.

不需要调用 ignore() 来使用 std::getline() 跳过 '\n'。

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

// 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 中的一个新项目中并且它有效.. 输入每个项目并按回车键.. 当你完成后在新行上输入打印并点击输入,它将打印值。