0

So I just started to use Visual Code for Mac and started to play around with it. I got the c/cpp and code run extensions and also downloaded HomeBrew. When I created a "for loop" there was no output in the terminal.

#include <iostream>

int main()
{
    std::cout << "Hey" << std::endl;

    /*for(int i = 0; i > 30; i++)
    {
        std::cout << i << " - " << std::endl;
    }*/

    for (int i = 1; i <= 5; ++i) {
        std::cout << i << " ";
    }

    return 0;
}

-------------------------------------------------
TERMINAL:
Hey

Im just confused on why the "for loop" isn't outputting. Is there another extension or setting that I have to download/enable?

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 1
    You may need to add a `std::cout << std::endl` to flush. Doing a test locally on macOS I see your complete output, but there's no end of line on the `5` line. `zsh` shows this as `1 2 3 4 5 %`. – tadman Aug 30 '23 at 14:39
  • @tadman I don't think that `std::endl` is required at all if you call [`std::flush`](https://en.cppreference.com/w/cpp/io/manip/flush)? – darclander Aug 30 '23 at 14:46
  • It's not necessary, but it's polite to do it so your shell doesn't get janked up. – tadman Aug 30 '23 at 14:47
  • 1
    @tadman sure thing! I was just thinking that if they do not want to add a newline then the reason to why we don't have an output is because we aren't flushing the output buffer? – darclander Aug 30 '23 at 14:50
  • 1
    @darclander Having options is always a good thing, and sometimes you need to flush without a newline. – tadman Aug 30 '23 at 14:50
  • 2
    For @OP I think that this stackoverflow post: https://stackoverflow.com/questions/4591915/no-console-output-on-cout is similar to what you are having an issue with if you want to read previous answers. – darclander Aug 30 '23 at 14:54
  • ***Is there another extension or setting that I have to download/enable?*** No – drescherjm Aug 30 '23 at 15:33
  • I'll my bets on either this user is using a debugger and hasn't run the program to completion yet (https://stackoverflow.com/q/14858262/11107541), or they didn't save their changes to the file (https://stackoverflow.com/q/76984829/11107541) – starball Aug 30 '23 at 20:07

0 Answers0