[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 175
  • Last Modified:

Order Correction

#include <iostream.h>
#include <conio.h>

void CPost();

int main()
{
   CPost();
   getch();
   return 0;
}

void CPost()
{
   cout<< "Hello\n";
}

I have the following code. I have put CPost to its
own function in attempt to answer my own
question, it did not work, but I post it this way
anyway. My question is how do I force it to
Cout then GetCH. Currently it does GetCH then
Cout, which ... messes things up, and prevents
me from continuing on.
0
List244
Asked:
List244
2 Solutions
 
Jaime OlivaresCommented:
Your sample code doesn't work with my compilers, I have tried this succesfully:

/* don't use iostream.h header */
#include <iostream>
#include <conio.h>

using namespace std;  /* needs this namespace in order to use cout */

void CPost();

int main()
{
   CPost();
   getch();
   return 0;
}

void CPost()
{
   cout<< "Hello\n";
}

0
 
SirHandoCommented:
Use cin to read the characters.

cout & cin are tied together, so that things will work in order.

If you mix c-library methods with c++ streams, there there is guarantee on the order.

Ciao

SirHando
0
 
twobitadderCommented:
It works as:

This deals with the namespace problem pointed out by jaimie for the cout and changes your getch to getChar() from the C standard library


 #include <iostream>
 #include <stdio.h>

void CPost();

int main()
{
   CPost();
   getchar();
   return 0;
}

void CPost()
{
   std::cout<< "Hello\n";
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
twobitadderCommented:
sorry getchar() not getChar()
0
 
List244Author Commented:
I know I can use Cin but, I wanted to avoid that.
However, adding the namespace worked out
great, thanks jaime.
0
 
rstaveleyCommented:
twobitadder,

> #include <iostream>
> #include <stdio.h>

With <iostream> you should really use:

#include <cstdio>
0
 
itsmeandnobodyelseCommented:
I would recommend to use

void CPost()
{
   std::cout<< "Hello\n";
   cout.flush();
}

That definitively flushes the output stream.  

Regards, Alex
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now