Tried to stop my console closing...dosnt work?

I'm a noob, go easy.

Trying to stop my program from closing, but no matter what I try nothing seems to work.

Read this thread, (http://www.cplusplus.com/forum/beginner/1988/) but ALL solutions I've tried sill has the console closing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <limits>
#include <iostream>
using namespace std;

/*
 * This is a quiz.
 */
int main(){
    
    int option;

    cout << "Please select an option: \n 1) Wipe Drive \n 2) Wipe while preserving installation \n 3) Do nothing" << endl;
    cin >> option;
          
       if (option == 1) 
            cout << "Wipe progressing.." << endl;
       
       else if (option == 2) 
            cout << "Preserving Install" << endl;
       
       else 
            cout << "Aborted" << endl;
                 
 
  std::cout << "Press ENTER to continue...";
  std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

  return 0;
  }


I've even tried the apparent sin system ("PAUSE");, and for some reason that dosnt work either.

Any ideas?

Thank you :)

Tom
If you want system("PAUSE"); You need to #include <windows.h> .

What compiler/IDE/OS are you using? If you're using Visual Studio, press CTRL+F5 to run the program from a console with an automatic PAUSE in there.

If you're on windows without VS, press WIN+R, then type "cmd". use the cd command to get to the output path of your compiler/linker. Then run the executable from there. The console will not pause.

I'm using NetBeans on windows 8 (don't hate).

I'd really like to learn just a simple bit of code that I can use just to keep the program open.

Ty

Tom
closed account (28poGNh0)
How about now

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# include <conio.h>
# include <iostream>
using namespace std;

int main()
{
    int option;

    cout << "Please select an option: \n 1) Wipe Drive \n 2) Wipe while preserving installation \n 3) Do nothing" << endl;
    cin >> option;

    if (option == 1)
        cout << "Wipe progressing.." << endl;

    else if (option == 2)
        cout << "Preserving Install" << endl;

    else
        cout << "Aborted" << endl;


    cout << "Press ENTER to continue...";
    getch();

    return 0;
}
I get an error with the '# include <conio.h>' 'cannot find included file'
closed account (28poGNh0)
It works well ,I think you got this error because of your compiler netbeans

I advise you using the compiler "Code::blocks " It's free and good for beginers
this is the link "http://www.codeblocks.org/downloads/26#linux"
is Code::blocks an IDE or a Compiler? I use Netbeans as the IDE and Cygwin as the compiler?

Also is there no way to get this to work on NetBeans? I'm kinda getting used to the layout and dont want to start afresh.

Ty

Tom
Last edited on
closed account (28poGNh0)
It's both IDE and compiler ,I never use netbeans,I use MSVC but most Code::block because it has the bets IDE I beleive
No way to try and get this working on NetBeans?
closed account (28poGNh0)
No
sorry
The problem is that on line 13, your cin >> operation will leave extra input (the non integer information) in the buffer, so later on line 26 the ignore will obtain this input and ignore it.

The solution is to place another std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); after the lines where are you using formatted input (like line 13).
Just started using codeblocks and I'll think I'll stick with that for now.

Thank you for your help guys, really starting to get into programming, since I'm more of an electronic design guy :)

Tom
Topic archived. No new replies allowed.