Jump to content

I've seen that using

system("PAUSE")

is a bad thing.

Next solution was

cin.get()

Though i does not work for me.

So i ask, what am i doing wrong or what are alternative ways of doing so?

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
https://linustechtips.com/topic/542940-how-to-hold-the-program-c/
Share on other sites

Link to post
Share on other sites

10 minutes ago, MisterWhite said:

I've seen that using


system("PAUSE")

is a bad thing.

Next solution was


cin.get()

Though i does not work for me.

So i ask, what am i doing wrong or what are alternative ways of doing so?

in school we use cin.get(), that makes us press any button on the keyboard so it will continue. No idea of any alternative ways.

Link to post
Share on other sites

Just now, IoWii said:

in school we use cin.get(), that makes us press any button on the keyboard so it will continue. No idea of any alternative ways.

About cin.get() , it does not work

 

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to post
Share on other sites

Just now, PampaZiya said:

You're not doing anything wrong. What exactly do you want? You can use the sleep function if you want it to halt for a bit then continue.

I don't want for the console to exit as soon as everything is done. I need it to stay (so that i can see the output)

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to post
Share on other sites

Could you give us an example of code where it doesn't work? 

#include<iostream>

int main()
{
    std::cout << "Hello World! Press any key to continue" << std::endl;
    std::cin.get();

    return 0;
}

 

This example works fine for me. Is it a compiler error? Or your program doesn't work as expected?

Link to post
Share on other sites

14 minutes ago, casualgamer said:

This example works fine for me. Is it a compiler error? Or your program doesn't work as expected?

#include <iostream> // binary to decimal and decimal to binary converter
#include <stdlib.h>

using namespace std;

int main()
{
    int select;
    string binary;// for decimal conversion
    int Decimal;// for binary conversion
    unsigned int toDecimal=0; // converted binary
    char toBinary[32]; // converted decimal

    cout<<"Convert from binary (0) or decimal(1): \n";

    while(select!=0 && select!=1)
    {
        cin>>select;
        switch (select)
        {
            case 0:
            {
                cout<<"Enter a binary number: ";
                cin>>binary;

                int powTwo;
                for(int i=binary.length()-1; i!=-1; i--)
                {
                    int temp=binary[i]- '0'; // taking last value of binary string

                        if(i==binary.length()-2)
                            powTwo=2;
                        if(i==binary.length()-1)
                            toDecimal+=temp*1; // first number must be max 1
                        else
                            toDecimal+=temp*powTwo; // other numbers are multiplied with pow 2

                     powTwo*=2; // increasing "power"

                }
                cout<<toDecimal;
            }
            break;

            case 1:
            {
                cout<<"Enter a decimal number: ";
                cin>>Decimal;
                int i=0;
                while(Decimal!=0)
                {
                    int sk=Decimal%2;
                    toBinary[i]=sk + '0';
                    Decimal/=2;
                    i++;

                }
                for(int j=i; j!=-1; j--)
                {
                    cout<<toBinary[j-1];
                }

                cout<<endl;
            }
            break;

            default:
                cout<<"Enter 0 or 1 only\n";
            }

    }

     cin.get(); // when the program is run through main.exe it does what it's supposed to, but quits instantly.
    return 0;
}

Long post though.

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to post
Share on other sites

I believe that cin will leave stuff in the buffer like \n and then when you do a cin.get(); it automatically takes the stuff that's in the buffer.

 

One way of doing it is

cin >> someVariable;
cin.ignore();

However if you're on Windows you can use

system("PAUSE");

But use it only for testing. Remove it before deploying anything to production :)

Link to post
Share on other sites

If you're on Windows you can use getch() ( or _getch() ) from conio.h, or even better , use kbhit() from the Microsoft run-time library. If you're on Linux (I think even OSX) you can implement kbhit() yourself like this :

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>

int kbhit(void)
{
  struct termios oldt, newt;
  int ch;
  int oldf;

  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);

  ch = getchar();

  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  fcntl(STDIN_FILENO, F_SETFL, oldf);

  if(ch != EOF)
  {
    ungetc(ch, stdin);
    return 1;
  }

  return 0;
}

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×