C++

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

jacy

Thread Starter
Joined
Jul 19, 2004
Messages
120
Hi Happy New Year everyone,
I am using Dev-C++ 4.9.8.0. In my program, in order to display the output i have to use a code which i have underlined. I dont know what it is. If i dont use that code, then the output is not displayed.Instead of this code can i use system(pause). If i can use that, then what header file should i have to use. I found that code in the CD that came with the book. Also instead of iostream if i use iostream.h i get a whole bunch of warnings. Could anyone please tell me the difference between the two.Thanks for the help.

#include <iostream>
using namespace std;
int main()
{
cout << "Hello";
cin.ignore(std::cin.rdbuf()->in_avail() + 1);

}
 

IMM

Joined
Feb 1, 2002
Messages
3,257
Regarding the difference between the new and old iostream (iostream.h) libraries
http://c.ittoolbox.com/documents/popular-q-and-a/iostream-vs-iostreamh-1726

If find myself still using the old one for some things.

pause invokes the shell -- avoid it
use something like
Code:
  /* eat up characters until a newline or eof */
  do
  {
    ch = getchar();
    if(ch == EOF) break;
  } while(ch != '\n');
which is similar to what your code is trying to do

You can see http://cpp.codenewbie.com/articles/cpp/1437/systemPAUSE-Page_1.html for ideas
 
Joined
Apr 30, 2001
Messages
2,636
Its' a command line program you are dealing with. It is not meant to be double-clicked, ran and then paused at the end. If you run it from the command line (command.com or cmd.exe for example), it will display fine.

Anyway, you can also do it like this:

Code:
#include <iostream>
#include <cstdio> // for fflush();

using namespace std;

void pause() {
    cout << "Press ENTER to continue . . .";
    fflush(stdin);
    cin.get();
}

int main() {
    cout << "Hello, world!" << endl;
    pause();
}
 

jacy

Thread Starter
Joined
Jul 19, 2004
Messages
120
Shadow2531 said:
Its' a command line program you are dealing with. It is not meant to be double-clicked, ran and then paused at the end. If you run it from the command line (command.com or cmd.exe for example), it will display fine.

Anyway, you can also do it like this:

Code:
#include <iostream>
#include <cstdio> // for fflush();

using namespace std;

void pause() {
    cout << "Press ENTER to continue . . .";
    fflush(stdin);
    cin.get();
}

int main() {
    cout << "Hello, world!" << endl;
    pause();
}
Thanks for the reply. Sorry i still have some doubt. Am using Dev C++ and it has a GUI, so am not compiling the program from command line, so why i still have to use that code in order to display the output. I have borland C++ in my college. When am using that i dont have to use any code within the program to display the output. Thanks for your help.
 
Joined
Apr 30, 2001
Messages
2,636
Am using Dev C++ and it has a GUI, so am not compiling the program from command line
It's not where you compile it from, it's the type of program, which in this case, is a command line program. If another compiler automatically adds some type of pause without you specifying it in the code, it's got to be a feature that you can turn off. If you can't turn it off, that's a problem. Automatically adding a pause is not required by a conforming c++ compiler. DevC++ uses mingw as it's compiler btw. DevC++ is just an editor.
 
Joined
Oct 18, 2004
Messages
355
Hello there.

I used Dev-C++ (as did my uncle) for quite awhile. I recently stumbled on a newer IDE called Code::Blocks Studio that may be worth looking into. We both had a lot of problems doing some things in Dev-C++ (I still use it a lot, however, because I have gotten so used to it), but for some reason things work perfectly fine in Code::Blocks. It's worth looking into, and it is free.

Take care.
 
Joined
Nov 22, 2005
Messages
869
Not sure if I'm repeating what another person has already said. But first up, try not to use system("pause"). First up, you cannot use it on another OS, and secondly, you need another include <stdlib.h> for it. A better alternative would be:

cout << "Press anykey to continue...";
getchar();

you don't need any other includes for this, and its completely portable to other OSes.
 

jacy

Thread Starter
Joined
Jul 19, 2004
Messages
120
Shadow2531 said:
It's not where you compile it from, it's the type of program, which in this case, is a command line program. If another compiler automatically adds some type of pause without you specifying it in the code, it's got to be a feature that you can turn off. If you can't turn it off, that's a problem. Automatically adding a pause is not required by a conforming c++ compiler. DevC++ uses mingw as it's compiler btw. DevC++ is just an editor.
Thanks shadow2531
 

jacy

Thread Starter
Joined
Jul 19, 2004
Messages
120
InterKnight said:
Hello there.

I used Dev-C++ (as did my uncle) for quite awhile. I recently stumbled on a newer IDE called Code::Blocks Studio that may be worth looking into. We both had a lot of problems doing some things in Dev-C++ (I still use it a lot, however, because I have gotten so used to it), but for some reason things work perfectly fine in Code::Blocks. It's worth looking into, and it is free.

Take care.
Thanks Interknight for the link. I appreciate it. Take care.
 

jacy

Thread Starter
Joined
Jul 19, 2004
Messages
120
blaqDeaph said:
Not sure if I'm repeating what another person has already said. But first up, try not to use system("pause"). First up, you cannot use it on another OS, and secondly, you need another include <stdlib.h> for it. A better alternative would be:

cout << "Press anykey to continue...";
getchar();

you don't need any other includes for this, and its completely portable to other OSes.
Thanks blaqdeaph, i will definitely keep ur suggestion in mind.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top