The program runs correctly on Ideone, but not in Xcode

I recently started retraining C ++ from the time I was in high school in my free time (using C ++ Primer, 5th Ed.). When I went through the basic exercises, I noticed that the following program would not execute correctly in Xcode, but would execute correctly on Ideone: http://ideone.com/6BEqPN

#include <iostream>

int main() {
    // currVal is the number we're counting; we'll read new values into val
    int currVal = 0, val = 0;

    // read first number and ensure that we have data to process
    if (std::cin >> currVal) {
        int cnt = 1;
        while (std::cin >> val) {
            if (val == currVal)
                ++cnt;
            else {
                std::cout << currVal << " occurs " << cnt << " times." << std::endl;
                currVal = val;
                cnt = 1;
            }
        }
        std::cout << currVal << " occurs " << cnt << " times." << std::endl;
    }

    return 0;
}

In Xcode, the program does not end. It stops at the last execution of the body of the while loop. In the debug console, I see that there is a SIGSTOP signal. Screenshot

This is my first time using Xcode for any IDE. I suspect this could be due to my build settings? I configured it for GNU ++ 11 and used libstdc ++.

, Ideone, Xcode. , , IDE , Xcode ++ 11. !

+4
1

cin . while std::cin >> val, , , . , (42 42 42 42 42 55 55 62 100 100 100) , cin , . -, , (, 42 42 42 42 42 55 55 62 100 100 100 x).

, std::getline stringstream:

#include <iostream>
#include <sstream>

int main() {
    // currVal is the number we're counting; we'll read new values into val
    int currVal = 0, val = 0;

    string str;
    //read the string
    std::getline(std::cin, str);
    //load it to the stream
    std::stringstream ss(str);

    //now we're working with the stream that contains user input
    if (ss >> currVal) {
        int cnt = 1;
        while (ss >> val) {
            if (val == currVal)
                ++cnt;
            else {
                std::cout << currVal << " occurs " << cnt << " times." << std::endl;
                currVal = val;
                cnt = 1;
            }
        }
        std::cout << currVal << " occurs " << cnt << " times." << std::endl;
    }

    return 0;
}
+1

Source: https://habr.com/ru/post/1612201/


All Articles