【问题标题】:C++ Loop recieves integers until given QC++ 循环接收整数,直到给定 Q
【发布时间】:2016-12-15 17:37:27
【问题描述】:

在给出 q 或 Q 之前,循环应该接收输入。尽管循环需要一个整数,而 char q 或 Q 会中断循环并且无法识别。如何让循环识别 q 或 Q 来打破循环?

int ReadInput;      // Remembers input elements given
string ReadInputString = to_string(ReadInput); // String version of the ReadInput 

        do
        {
            bool NoErrors = true;   // Make sure there were no errors while reading input

            std::cout << "Enter the next element (Enter 'q' to stop): "; // Prompt user for input
            cin >> ReadInput;

            if (ReadInputString == quit || ReadInputString == Quit) // Enter Q or q to quit
            {
                break;
            }

            /* Validation */
            if (cin.fail()) 
            {
                /* Pass by the bad input */
                NoErrors = false; // Note there was a problem
                cin.clear(); // Clear the bad input
                cin.ignore(); // Ignore the bad input
                std::cout << "Invalid number" << endl; // Error message
            }

            /* Add the input to the list (if there were no problems) */
            if (NoErrors)
            {
                NumbersList.push_back(ReadInput); // Put the given number onto the end of the list
            }

        } while (ReadInput >= 0);

【问题讨论】:

  • quitQuit 又是什么?
  • 您的代码中还有一些更荒谬的东西:: cin &gt;&gt; ReadInput; 读取 int 值,但您想将其与 "q""Q" 进行比较。
  • 这与您要求用户输入的内容完全不同。
  • 字符串退出 = "q";字符串退出 = "Q";
  • 你还在黑暗和迷雾中徘徊。

标签: c++ loops int


【解决方案1】:

最终将数字存储在哪里并不重要。您正在输入整数变量,并希望将 char 存储在那里。所以只需在那里取一个 char,检查它是否包含数字,如果是,将其转换为 num 并继续,如果是 char 检查它是 q 或 Q,如果是则中断

int ReadInput;

char temp;

string ReadInputString = to_string(ReadInput); // String version of the ReadInput 

        do
        {
            bool NoErrors = true;   // Make sure there were no errors while reading input

            std::cout << "Enter the next element (Enter 'q' to stop): "; // Prompt user for input

            cin >> temp;                        //store input as char 
            if (isdigit(temp))                  //check input is number or not
                ReadInput = temp;               //convert input to num if true
            else if (temp == 'q' | temp == 'Q') //if not number check for q or Q
                break;                          //break if q

            if (ReadInputString == quit || ReadInputString == Quit) // Enter Q or q to quit
            {
                break;
            }

            /* Validation */
            if (cin.fail()) 
            {
                /* Pass by the bad input */
                NoErrors = false; // Note there was a problem
                cin.clear(); // Clear the bad input
                cin.ignore(); // Ignore the bad input
                std::cout << "Invalid number" << endl; // Error message
            }

            /* Add the input to the list (if there were no problems) */
            if (NoErrors)
            {
                NumbersList.push_back(ReadInput); // Put the given number onto the end of the list
            }

        } while (ReadInput >= 0);

【讨论】:

  • 不鼓励您仅发布代码答案是有原因的!阅读std::string 值排在第一位!
  • 添加了适当的 cmets 以使更改完全可以理解
  • 它们存储在 vetor NumbersList 中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多