-4

I am in midway of implementing fairplay algorithm, but I noticed that cout statements are not working

Below is my code. Note that all required header files are included and there are no compilation errors (by far my knowledge). The program runs without errors and I get terminated command (again, with no errors).

But there is no output at the same time. Even the cout test 2 is not reflected in output.

NOTE: I get output if I use endl at the end of each cout statement, but thats inconvenient here as I want to print the matrix. Besides there must be a easy way rather than writing enld every time

int main()
{

    int i,j,k,x,y,z;
    bool status[24]={false};
    char map[5][5]={0};
    string key;
    cout<<"Enter the key"<<endl;

    cout<<"Test 1";
    getline(cin,key);
    cout<<"Test 2";
    x=key.length();

    for(i=0;i<x;i++)
        key[i]=toupper(key[i]);

    for(i=0;i<x;i++)
    {
        y=key[i]-65;
        if(status[y]==0)
            status[y]=1;
    }

    cout<<"Adding the main key string";

    i=j=k=z=0;
    while(k<x)
    {
        if(j==5)
        {
            i++;
            j=0;
        }
        map[i][j++]=key[k++];
    }
    cout<<"Adding the remaining alphabets";
    while(k++<25)
    {
        if(j==5)
                {
                    i++;
                    j=0;
                }
        while(status[z++]==false)
            map[i][j++]=z+64;
    }

    cout<<"Output matrix";
    for(i=0;i<5;i++)
        for(j=0;j<5;j++)
        {
            cout<<map[i][j];
        }


    return 0;
}
das-g
  • 9,718
  • 4
  • 38
  • 80
  • Please list your include files also in question – Steephen Feb 21 '15 at 17:25
  • I said there are no errors, all the header files are included – Sagar Patel Feb 21 '15 at 17:29
  • @πάνταῥεῖ thanks for guidance, im just a novice, but wouldn't the world be a better place if the "good" people had been a little less arrogant and showed a helping nature to their inferiors. Rather that being bully/bossy I would be pleased if you could answer to my problem. "In vain have you acquired knowledge if you have not imparted it to others." – Sagar Patel Feb 21 '15 at 17:52
  • Looks like you just need to use `cout.flush();` after your outputting lines to see the values appearing immediately after writing them. `cout` uses buffered output with most implementations. – πάντα ῥεῖ Feb 21 '15 at 17:55
  • @πάνταῥεῖ Thank You very much. I apologise if I offended you, its just that I was a bit upset on my question being downvoted. You see its very difficult for a new members like us to increase our reputation, if people kept on downvoting our questions. – Sagar Patel Feb 21 '15 at 18:02
  • @πάνταῥεῖ BTW wasn't my question worth ?? – Sagar Patel Feb 21 '15 at 18:03
  • 1
    @SagarPatel _"BTW wasn't my question worth ??"_ Well, you should have at least put in some efforts to make a [MCVE](http://stackoverflow.com/help/mcve), instead of providing just a plain code dump. The 1st of the downvotes were probably about that yelling text. That's not well appreciated here. – πάντα ῥεῖ Feb 21 '15 at 18:06
  • Possible duplicate of [std::cout not working inside a for-loop](https://stackoverflow.com/questions/6451198/stdcout-not-working-inside-a-for-loop) – jww Mar 28 '18 at 18:01

2 Answers2

1

Add cout.flush() (or std::endl which implicitly flushes) after each cout statement.

cout uses buffered output, using cout.flush() will clear the buffer and force the contents being written out.

Thanks πάντα ῥεῖ for guidance

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

try using

fflush(stdin);

after cout<<"Test 1"; if not working try after getline(cin,key);

cout<<"Test 1";
getline(cin,key);
cout<<"Test 2";
Azad Hussain
  • 141
  • 1
  • 6
  • 19
  • thanks bro, but I already tried that before and it doesn't work for me – Sagar Patel Feb 21 '15 at 17:34
  • fflush does not always work. See alternative solution here http://stackoverflow.com/questions/28382962/wait-for-press-enter-in-c-inside-a-while-loop/28393414#28393414 – VolAnd Feb 21 '15 at 17:46
  • 1
    _@Azad_ _`fflush(stdin);`_ You probably meant `cout.flush()`. It's not a good idea to mix c-style IO with c++ IO, and it's not guaranteed to work with all implementations of the language. Also input buffering has nothing to do with the behavior described in the OP. – πάντα ῥεῖ Feb 21 '15 at 18:13