getch() in C++

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
neoak
Psychotic DCEmu
Psychotic DCEmu
Posts: 690
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Wed Dec 18, 2002 5:48 pm
Location: Mayagüez, PR | Houston, TX
Has thanked: 0
Been thanked: 0

getch() in C++

Post by neoak »

I don't know what to do next. I write all my programs, and they run fine. However, my teacher asked for one condition: That all programs must ask at the end if you want them to repeat (like a reload without closing the window).

A normal code:

Code: Select all

#include "iostream.h"
#include "math.h"
#include <stdio.h>
#include <conio.h>

int main()
{

double A,Xold,Xnew;
double d=1;

cout << "Enter the number which square root will be determined";
cin >> A;

Xold=A;

while (d>0.000001;)
{
Xnew=(Xold+(A/Xold))/2;
d=fabs(Xnew-Xold);
Xold=Xnew;
}

cout << "The Square Root of the number is: " << Xnew << "\n";

return 0;
}
This code works. However, my teacher wants the code like this:

Code: Select all

#include "iostream.h"
#include "math.h"
#include <stdio.h>
#include <conio.h>

int main()
{

double A,Xold,Xnew;
double d=1;
char key='y';

do
{
cout << "Enter the number which square root will be determined";
cin >> A;

Xold=A;

while (d>0.000001;)
{
Xnew=(Xold+(A/Xold))/2;
d=fabs(Xnew-Xold);
Xold=Xnew;
}

cout << "The Square Root of the number is: " << Xnew << "\n";

cout << "\nDo you wish to run this program again? Press Y for yes, N for no:";
key=getch();
cout << "\n";
}

while(key=='s'||key=='S');

return 0;
}
The problem is the "getch()". Anyone here knows how to use getch() in C++?, because if i use plain C, it would work (but i like more C++). However, i want to now.
Image
q_006
Mental DCEmu
Mental DCEmu
Posts: 415
Joined: Thu Oct 10, 2002 7:18 pm
Has thanked: 0
Been thanked: 0
Contact:

Post by q_006 »

you don't need a getch()

you can easily use cin and char.

example

Code: Select all

char choice;
choice = 'y';
while (choice == 'Y' || choice 'y') {
     cout << "Loop still runs << endl;
     cin >> choice;
}
also since you're using C++, it'll be better to use:

Code: Select all

<iostream>
<cmath>
<cconio>   // you may have to use conio.h depending on compiler
using namespace std;
hope this helps.
(and hope i didn't break a rule...)
User avatar
toastman
Iron Fist of Justice
Iron Fist of Justice
Posts: 4933
Joined: Sat Nov 10, 2001 3:08 am
Location: New Orleans
Has thanked: 0
Been thanked: 0
Contact:

Post by toastman »

Actually there is a significant difference. cin will wait for the 'Enter' key to be pressed, while getch doesn't.
getch should still work the same. C++ pretty much includes all of C.
No signature.
User avatar
neoak
Psychotic DCEmu
Psychotic DCEmu
Posts: 690
Joined: Wed Dec 18, 2002 5:48 pm
Location: Mayagüez, PR | Houston, TX
Has thanked: 0
Been thanked: 0

Post by neoak »

Actually, cin was one of the solutions. However, a condition was that you mustn't press Enter to get the program to run again.

toastman, the problem is that it compiles in VC++ 6.0, but it goes wrong after it begins to do the operations after asking the values. Could you please try it?
Image
Mask of Destiny
Mental DCEmu
Mental DCEmu
Posts: 330
Joined: Sun Mar 23, 2003 10:52 pm
Has thanked: 0
Been thanked: 0

Post by Mask of Destiny »

The standard C IO doesn't get allong with C++ IOStream. If you're going to use getch, you need to use printf and scanf instead of cin and cout.
ATani
DCGen Creator
DCGen Creator
Posts: 66
Joined: Sat Jan 19, 2002 12:54 am
Location: Near Yosemite, CA
Has thanked: 0
Been thanked: 0
Contact:

Post by ATani »

neoak wrote:Actually, cin was one of the solutions. However, a condition was that you mustn't press Enter to get the program to run again.
try:

Code: Select all

char foo = cin.get();
Based on the book "C++ How To Program" by "H.M. Deitel & P.J. Deitel" this should read one character at a time. You can also use:

Note that cin.get() does not block, it will return EOF when there is no input.

Code: Select all

char charArray = new char[10];
cin.get(charArray, 10);
This example will read up to 10 characters but not more than 10.
User avatar
neoak
Psychotic DCEmu
Psychotic DCEmu
Posts: 690
Joined: Wed Dec 18, 2002 5:48 pm
Location: Mayagüez, PR | Houston, TX
Has thanked: 0
Been thanked: 0

Post by neoak »

See next post.
Last edited by neoak on Mon Apr 26, 2004 10:05 am, edited 2 times in total.
User avatar
neoak
Psychotic DCEmu
Psychotic DCEmu
Posts: 690
Joined: Wed Dec 18, 2002 5:48 pm
Location: Mayagüez, PR | Houston, TX
Has thanked: 0
Been thanked: 0

Post by neoak »

The problem was using \n for new lines. Just use endl and it will work like a charm.
Image
Post Reply