【问题标题】:C++ To-Do List repeats inputs. How to avoid that?C++ 待办事项列表重复输入。如何避免这种情况?
【发布时间】:2018-08-16 00:36:10
【问题描述】:

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

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) 铺床 等等

【问题讨论】:

  • 摆脱cin.ignore();

标签: c++ string input counter cin


【解决方案1】:
// 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 中运行了这个确切的代码,它工作了.. 键入每个项目并按 Enter.. 完成后在新行上键入 print 并按 Enter,它将打印值。

【讨论】:

  • J.A.R.,我知道这要求很多,但您能否包含更多 cmets 来解释您正在使用代码做什么。
  • static string all[30] 是我们存储字符串的字符串数组。您应该识别的所有其他内容。 for 循环,if 块,顶部的函数原型(在 C++ 中是必需的),exit(0) 退出程序,大多数编译器都需要 return,静态,我解释了保存多个函数调用的值,所以静态 i,和静态全部[30]。比较运算符不等于 != ... 比较运算符等于 ==。这是一个有趣的想法,因为您可以继续输入和打印。直到你输入 exit。
  • 您应该尝试使用字符串类型的数组。有些容器对我来说是新的。列表和地图。类似于 VB 中的 ArrayList 和 VB 中的 HashTable。还有一种叫做向量。我从来没有使用过他们中的任何一个,在 Visual Basic 中非常出色。
  • 谢谢杰森。我感谢您的澄清和建议。我会带着它跑。再次感谢!顺便说一句,我这次成功了。
【解决方案2】:

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

b) 下一个可用的输入字符是 delim,由 Traits::eq(c, delim) 测试,在这种情况下,分隔符从输入中提取,但不附加到 str。

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

【讨论】:

    【解决方案3】:
    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") 调用这个函数,它会打印所有的值。

    【讨论】:

    • 函数内的静态变量在对该函数的所有调用期间保持其值。如果设置 i = 1,则下次调用该函数时,它将为 1。因此,创建一个 all[3] 的静态数组,并将 list_entry 字符串添加到函数中。
    • 没关系。你遇到了什么错误。我更改了代码。 all[i]
    猜你喜欢
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    相关资源
    最近更新 更多