Code0
Code0

Reputation: 74

c++ strange execution logic

int main(int argc, const char * argv[])
{
    std::cout << "How many turns do you want to do?" << std::endl << std::endl;
    std::string turnsDefined;
    std::cin >> turnsDefined;
    std::cout << turnsDefined;
    bool hasPassed = false;
    int objectSize;
    for(char c : turnsDefined){
        objectSize++;
    }
    std::cout << objectSize;
    while(hasPassed != true){
        for(char c : turnsDefined){
            if(isdigit(c) != true){
                hasPassed = false;
                break;  
            }
        }
    }

    std::cout << "Successfully passed all if conditions.";
    return 0;
}

So, all that this short program is doing, is that it's getting a users input, tests if it contains any letters, and then sets a bool to either false or true (I know that that part won't work, but that's not what I'm asking for). My problem with this is: As soon as I input something, I'm expecting it to return it right back (std::cin >> turnsDefined; "NEWLINE" std::cout << turnsDefined;) however, it's just pausing and not returning anything.

Upvotes: 0

Views: 106

Answers (1)

icabod
icabod

Reputation: 7074

The reason you're not getting any output is that stdout (as used by default by cout) is not getting flushed. The stdout stream is buffered, and this buffer will only be flushed in certain circumstances: reaching a newline, when it is instructed to flush (for example with cout.flush()), or on exiting the process.

In your code you're not outputting any newlines, you're not manually flushing the buffer, and (because hasPassed is never set to true, resulting in an infinite loop) you're never exiting the process. For these reasons, stdout is never being flushed, so any output is not being shown on the display.

Upvotes: 3

Related Questions