简体   繁体   English

如何通过Enter(C ++)中断循环?

[英]How to break loop by Enter (c++)?

I wanted to break the loop when the user doesn't want to add anymore: 当用户不想再添加时,我想打破循环:

#include<iostream>

using namespace std;

int main() {
    int i = 0, a = 0, h = 0;
    cout << "Enter numbers to be added:\n ";
    for(i=0; ??; i++) {
        cout << "\n" << h << " + ";
        cin >> a;
        h = h+a;
    }
    return 0;
}

Use std::getline to read an input line and exit the loop when the line is empty. 使用std :: getline读取输入行,并在该行为空时退出循环。

#include<iostream>
#include <sstream>

int main() {
    int a = 0, h = 0;
    std::cout << "Enter numbers to be added:\n ";
    std::string line;

    std::cout << "\n" << h << " + ";
    while (std::getline(std::cin, line) && // input is good
           line.length() > 0) // line not empty
    {
        std::stringstream linestr(line);
        while (linestr >> a)// recommend better checking here. Look up std::strtol
        {
            h = h+a;
            std::cout << "\n" << h << " + ";
        }
    }
    return 0;
}

And output: 并输出:

Enter numbers to be added:

0 + 1 2 3 4 5 6 7 8 9 

1 + 
3 + 
6 + 
10 + 
15 + 
21 + 
28 + 
36 + 
45 + 

Note that this allows multiple entries per line and looks pretty ugly, so OP is probably more interested in: 请注意,这允许每行多个条目,并且看起来很丑陋,因此OP可能更感兴趣:

#include<iostream>

int main() {
    long a = 0, h = 0;
    std::cout << "Enter numbers to be added:\n ";
    std::string line;

    std::cout << "\n" << h << " + ";

    while (std::getline(std::cin, line) && // input is good
           line.length() > 0) // line not empty
    {
        char * endp; // will be updated with the character in line that wasn't a digit
        a = std::strtol(line.c_str(), &endp, 10);
        if (*endp == '\0') // if last character inspected was the end of the string
                           // warning: Does not catch ridiculously large numbers
        {
            h = h+a;
        }
        else
        {
            std::cout << "Very funny, wise guy. Try again." << std::endl;
        }
        std::cout << "\n" << h << " + ";
    }
    return 0;
}

Output 产量

Enter numbers to be added:

0 + 1

1 + 1 2 3 4
Very funny, wise guy. Try again.

1 + 2

3 + 44444

44447 + jsdf;jasdklfjasdklf
Very funny, wise guy. Try again.

44447 + 9999999999999999999999

-2147439202 + 

It is easier to use a sentinel value that you can check against, something like this: 使用您可以检查的哨兵值更容易,例如:

#include<iostream>
#include<string>

using namespace std;

int main()
{
    int sum = 0;
    string userInput;

    while(true)
    {
        cout<<"Enter number to be added ('q' to quit): ";
        cin >> userInput;

        if( (userInput == "q") || (userInput == "Q") )
        {
            break;
        }

        try
        {
            sum += stoi( userInput );
        }
        catch( const std::invalid_argument& e )
        {
            cerr << "Invalid input \"" << userInput << "\" received!" << endl;
            return EXIT_FAILURE;
        }
    }

    cout << "Sum: " << sum << endl;

    return EXIT_SUCCESS;
}

std::getline(std::istream&, std::string&) happily gives all lines including empty lines: std::getline(std::istream&, std::string&)愉快地给出所有行,包括空行:

#include <iostream>
#include <string>

int main() {
    long long accumulator = 0;
    while (true) {
        // read a (possibly empty) line:
        std::string buf;
        if (!std::getline(std::cin, buf)) {
            std::cerr << "The input stream is broken." << std::endl;
            break;
        }

        // was the entered line empty?
        if (buf.empty()) {
            std::cerr << "You entered a blank line" << std::endl;
            break;
        }

        // convert string to integer
        std::size_t pos;
        long long summand;
        try {
            summand = std::stoll(buf, &pos, 10);
        } catch (std::invalid_argument &) {
            std::cerr << "Not an integer: " << buf << std::endl;
            continue;
        } catch (std::out_of_range &) {
            std::cerr << "Out of range: " << buf << std::endl;
            continue;
        }
        if (pos != buf.size()) {
            std::cerr << "Not an integer on its own: " << buf << std::endl;
            continue;
        }

        // do something with the data:
        accumulator += summand;
    }
    std::cout << "accumulator = " << accumulator << std::endl;
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM