如何通过Enter(c ++)打破循环?

时间:2015-08-12 21:53:20

标签: c++ loops

我想在用户不想再添加时打破循环:

#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;
}

3 个答案:

答案 0 :(得分:2)

使用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;
}

输出:

Enter numbers to be added:

0 + 1 2 3 4 5 6 7 8 9 

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

请注意,这允许每行多个条目并且看起来非常难看,因此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;
}

输出

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 + 

答案 1 :(得分:0)

使用可以检查的标记值更容易,如下所示:

#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;
}

答案 2 :(得分:0)

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;
}