11 Replies - 17892 Views - Last Post: 14 January 2011 - 11:00 PM Rate Topic: -----

#1 Guest_Brad*


Reputation:

How to press "Enter" to continue

Posted 14 January 2011 - 05:10 PM

I am doing an program where you have to find the hypotenuse of a right triangle. Everything works fine. The question I have is "Press Enter To Continue." When this pops up, I can still press any key besides Enter and it will continue to the next problem, which I do not want. I want them to press the "ENTER" key. Is their a way to do that? Also, if you look at my program, I am using getch() 4 times. Is their a way to limit that to only using that once?

Thanks,

- Brad

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


using namespace std;

int main()
{

    double x, y;
    
    cout << "1. To calculate the hypotenuse of a right triangle\n " << endl;
    cout << "Enter length for first side: " << endl;
    cin >> x;
    cout << "Enter length for the second side: " << endl;
    cin >> y;
    
    double hypotenuse, V;
    
    hypotenuse = sqrt(pow(x,2) + pow(y,2));
    
    cout << "\n\nx = " << x << endl
         << "y = " << y << endl
         << "\n\nThe Hypotenuse of a right triangle = " << hypotenuse << endl;
    
         
    cout << "\nPress Enter to continue\n " << endl;
    getch();
    
    double volume;
    double radius;
    const double PI = 3.14159;
    
    cout << "2. Find the volume of a sphere\n " << endl;
    cout << "Enter the radius: " << endl;
    cin >> radius;
    
    cout << "\nThe radius = " << radius << endl;
    
    volume = (4.0/3.0) * PI * pow(radius,3);
    
    cout << " \nThe Volume of a Sphere = " << volume << endl;
    cout << "\nPress Enter to continue\n " << endl;
    getch();
    
    double Area;
    double height;
    
    cout << "3. To calculate the surface area of a Cylinder\n " << endl;
    cout << "Enter the radius: " << endl;
    cin >> radius;
    cout << "Enter the Height: " << endl;
    cin >> height;
    
    cout << "\nThe radius = " << radius << endl;
    cout << " \nThe height = " << height << endl;
    
    Area = 2 * PI * pow(radius,2) + 2 * PI * height; 
    
    cout << "\nThe Area of a cylinder = " << Area << endl;
    cout << "\nPress Enter to continue\n " << endl;
    getch();
    
    double peak;
    double degree;
    double voltage;
    
    cout << "4. To calculate the instantaneous value of an AC voltage " << endl;
    cout << "\nEnter the peak value: " << endl;
    cin >> peak;
    cout << "Enter angle: " << endl;
    cin >> degree;
    
    cout << "\nThe peak voltage = " << peak << endl;
    cout << "\nThe degree angle = " << degree << endl;
    
    voltage = peak * sin (degree/180 * PI);
    

    
    getch();
    
    return (0);
}





Is This A Good Question/Topic? 0

Replies To: How to press "Enter" to continue

#2 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to press "Enter" to continue

Posted 14 January 2011 - 05:16 PM

Replace your calls to the non-standard "getch()" with calls to the standard "getchar()".

Does it work as you want now?
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Brad*


Reputation:

Re: How to press "Enter" to continue

Posted 14 January 2011 - 05:26 PM

View Postjanotte, on 14 January 2011 - 04:16 PM, said:

Replace your calls to the non-standard "getch()" with calls to the standard "getchar()".

Does it work as you want now?


It still does the same thing. When I run the program, and it give the out come of the hypotenuse and tells me "Press Enter To Continue," I pressed the letter n on the keyboard and it continued. I am trying to make it where you have to press the "Enter" key to continue, and if you press any other key besides "Enter" it will not continue.
Was This Post Helpful? 0

#4 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: How to press "Enter" to continue

Posted 14 January 2011 - 05:32 PM

you will want to use your OS's key functions. look's like your using Turbo C++ so GetAsyncKeyState() should work.
Was This Post Helpful? 0
  • +
  • -

#5 Ancient Dragon   User is offline

  • D.I.C Addict
  • member icon

Reputation: 82
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: How to press "Enter" to continue

Posted 14 January 2011 - 05:36 PM

If all you want is the Enter key then you need to write your own function that ignores all other keys. For example, this uses non-standard conio.h only because that's what you tried to use in your original post. We don't recommend it because not all compilers support it.
#include <iostream>
#include <conio.h>
#include <limits>

void pause(const char* message)
{
    // clear keyboard buffer
    int c;
    while( _kbhit() )
        _getch();
    std::cout << message << '\n';
    while( (c = _getch()) != '\r' )
        ;
}

int main()
{
    pause("Press Enter to continue\n");
    std::cout << "done\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get();
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 Guest_Brad*


Reputation:

Re: How to press "Enter" to continue

Posted 14 January 2011 - 05:39 PM

View Postishkabible, on 14 January 2011 - 04:32 PM, said:

you will want to use your OS's key functions. look's like your using Turbo C++ so GetAsyncKeyState() should work.


I am using De++ 4.9.92 Beta. Not sure if its Turbo. I never even heard of that keystat() function but I will try to see if I can get that to work.
Was This Post Helpful? 0

#7 Guest_Brad*


Reputation:

Re: How to press "Enter" to continue

Posted 14 January 2011 - 05:42 PM

View PostAncient Dragon, on 14 January 2011 - 04:36 PM, said:

If all you want is the Enter key then you need to write your own function that ignores all other keys. For example, this uses non-standard conio.h only because that's what you tried to use in your original post. We don't recommend it because not all compilers support it.
#include <iostream>
#include <conio.h>
#include <limits>

void pause(const char* message)
{
    // clear keyboard buffer
    int c;
    while( _kbhit() )
        _getch();
    std::cout << message << '\n';
    while( (c = _getch()) != '\r' )
        ;
}

int main()
{
    pause("Press Enter to continue\n");
    std::cout << "done\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get();
	return 0;
}



reason for the conio.h was because so I can use the getchar() or getch() because I was getting an error before.
Was This Post Helpful? 0

#8 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: How to press "Enter" to continue

Posted 14 January 2011 - 05:44 PM

if your using window's then GetAsyncKeyState() should work. you will need to include windows.h. why are you using conio.h? it's not stranded and no one uses it that i know of. don't use conio.h ever. if you have an error that if fixes, fix it another way becuase it's not right. if you have the error again post it and your code and we can help

This post has been edited by ishkabible: 14 January 2011 - 05:52 PM

Was This Post Helpful? 0
  • +
  • -

#9 Guest_Brad*


Reputation:

Re: How to press "Enter" to continue

Posted 14 January 2011 - 06:02 PM

View Postishkabible, on 14 January 2011 - 04:44 PM, said:

if your using window's then GetAsyncKeyState() should work. you will need to include windows.h. why are you using conio.h? it's not stranded and no one uses it that i know of. don't use conio.h ever. if you have an error that if fixes, fix it another way becuase it's not right. if you have the error again post it and your code and we can help


so what header allows you to use getch or getchar because without conio.h you can not use getch or getchar. Since you are saying it is wrong to use conio.h, what can I use that is similar to getch or getchar where I do not need conio.h? (I am glad you guys are telling me this, I wont make this mistake again.)
Was This Post Helpful? 0

#10 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: How to press "Enter" to continue

Posted 14 January 2011 - 06:13 PM

cstdio should have getchar, im surprised that iostream doesn't include cstdio.
Was This Post Helpful? 0
  • +
  • -

#11 Guest_Brad*


Reputation:

Re: How to press "Enter" to continue

Posted 14 January 2011 - 06:26 PM

View Postishkabible, on 14 January 2011 - 05:13 PM, said:

cstdio should have getchar, im surprised that iostream doesn't include cstdio.


Thank you.....now I just have to figure out how to stop it at "Press Enter To Continue" with out adding part 2 to it.
Was This Post Helpful? 0

#12 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to press "Enter" to continue

Posted 14 January 2011 - 11:00 PM

The answers here will get you the solution you are looking for
http://www.dreaminco...on-window-open/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1