Jump to content

n00b C++ question : Shuting out withought displaying all of program

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
17 replies to this topic

#1
zeroradius

zeroradius

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,406 posts
Hey guys, I am starting C++ and am having a small problem. I know it is somthing simple but can't figure out what. The program closes out after you enter the width instead of displaying the area.

heres the code:

#include <cstdlib>

#include <iostream>


using namespace std;


int main(int argc, char *argv[])

{

    int length, width;

    cout<<"Enter the length of the object:";

    cin>>length;

    cout<<"Enter the width of the object:";

    cin>>width;

    cout<<"The area of the object is ";

    int area;

    area= length*width;

    cout<<area;

    cin.get();

    

    return EXIT_SUCCESS;

}



Also I am using Bloodshed C++ Beta 5. I would like to use the previous full version so that I know there are no bugs in the compiler. If any one could point me to it I would be thankfull
Posted Image

#2
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
Oh, one of the most annoying things in the world, that sh*t..

cin.get(); 

I have also very much struggled/been annoyed by this. It seems to work for some people and not for others, I'm not sure why. What I have figured is that there are characters "waiting" in the input buffer, which are read by cin, and then -> goodbye. I might be wrong, but this seems to be the problem. I solved this by creating a "FlushInput" function which... flushes the input buffer! :D

I could post the code and some examples to how I use it, if you want, assuming you are using Windows.

A much simpler way of achieving basically the same result (wait for user input) is the

system("PAUSE");


Try it.
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#3
zeroradius

zeroradius

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,406 posts
The system pause worked. Thanks. Man thats going to get annoying fast :D
Posted Image

#4
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Bloodshed isn't a compiler.

#5
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
Right MeTh0dz. Bloodshed makes the IDE, and it comes with MinGW (GCC) as a compiler. I'm not sure if this annoying little problem is related to the compiler.... Is anone using e.g. borland? Same problem there? On borland I think you can also use "getch()" - which is supposed to work... IDK..
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#6
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Borland is ****.

Just include conio.h, and you can use getch().

#7
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
I've never tried borland, so I can't really comment on that one. But if anybody uses it, it would be interesting to know if they had the same problem.

I don't think I have conio.h on my computer.. But I doubt I'm going to need it anyway..
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#8
zeroradius

zeroradius

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,406 posts
Methodz way works great. Let me rephrase my other question, where can I get a non beta version of bloodshed C++. I have visual C++ but don't want to use it.
Posted Image

#9
Dren

Dren

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 448 posts
I used to open the command prompt and then run the compiled program by typing its location so the window wouldn't close, lol :p

#10
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Bloodshed DevCpp is no longer in development.

#11
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
But I guess you could "downgrade" to an earlier version, not beta, if you want. Check their website.. I have the latest (beta), though, and it works fine. So far.
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#12
LogicKills

LogicKills

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts
You should stay away from system specific calls like system("pause");
Instead just try a pair of...
cin.ignore();
cin.get();
http://logickills.org
Science - Math - Hacking - Tech