LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-06-2003, 08:59 PM   #1
pazvant
Member
 
Registered: Jul 2003
Location: Istanbul
Distribution: slack
Posts: 43

Rep: Reputation: 15
Angry Exiting program with CTRL-Z C++


Here is the simple code for a calculator, however i want to exit when pressing CTRL-z on dos shell..In while loop something wrong i think
can you help me???


#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <iterator>
using std::cout;
using namespace std;
int main()
{
float numberone;
float numbertwo;
float result;
char command_character;

do {


cout<<"Enter operation:";
cin >>numberone>>command_character>>numbertwo;
if ((numberone == 0) && (command_character== '^')) {
cout<<"error: Let's go to sleep"<<endl;
}
else
if ((command_character == '/' ) && (numbertwo == 0))
cout<<" Division by zero error"<<endl;
else
if ((numberone < 0) && (command_character =='^'))
cout<<"???????"<<endl;
else
if ((((command_character != '-' ) && (command_character != '+') && (command_character !='/' ) && (command_character != '^'))))
cout<<"error:undifined operator"<<endl;
else {
switch (command_character) {
case '+':
result = numberone + numbertwo;
break;
case '/':
result = numberone / numbertwo;
break;
case '-':
result = numberone - numbertwo;
break;
case '*':
result = numberone * numbertwo;
break;
case '^':
result = pow(numberone,numbertwo);
break;
}
cout<<"The result is:"<<result<<endl;

}

} while ( != EOF); //// Here i try everyting but cannot managee.......!!!!


return 0;


}
 
Old 11-06-2003, 09:24 PM   #2
megaspaz
Senior Member
 
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
well in dos, i think you need to add the <conio.h> header file and use getch() to get the characters individually. basically when you get the control key then do a test in that test branch and check if the next character is "z". if it is issue exit (0);

i'm not in windows at all and i can't look at the msdn documentation on using conio.h, but take a look at it.
 
Old 11-07-2003, 10:40 AM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
ctrl-z is ASCII 26. getc() would return 26, for example.

If this is windows programming one way is to call GetAsynchKeyState() to find when ctrl-z has been pressed. cin will not work in this case, AFAIK.
 
Old 11-09-2003, 12:41 AM   #4
ludwig
Member
 
Registered: Jun 2002
Location: Orange County, CA
Distribution: Debian (squeeze), kernel 2.6.30-2-amd64
Posts: 32

Rep: Reputation: 15
In a DOS session the Ctrl-Z key combination generates the end-of-file signal. In other words, "end-of-input", there's nothing more to be read. Although you see ^Z on the screen when you type Ctrl-Z, it doesn't actually generate those two characters. In C++ the way to see if end-of-file has been detected is to test for it; since you're using standard stream objects, that means using the "eof" member function.

Immediately after your extraction from stdin, try testing for EOF:


Code:
do {
cout<<"Enter operation:";
cin >>numberone>>command_character>>numbertwo;
if (cin.eof())  {
// we just read EOF, break out of the loop
break;
}
BTW, I've noticed different behaviors with different WIndows platforms (e.g., Win95 vs. Win98 vs. Win2000 vs. XP, etc.). On some you can enter the Ctrl-Z after some input on the same line and don't have to press Enter, on others you can enter Ctrl-Z after some input on the same line but have to press Enter, but on all of them if you enter Ctrl-Z as the first character on a new line of input and press Enter it should be detected. (Why on earth MS keeps dinking around with the behavior of their consoles is beyond me...)

Have fun,
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ctrl+c & ctrl+v equivalent for Gnome terminal window? halfpower Linux - General 2 11-29-2005 03:57 AM
IPC Memory Share - C Program - Why not exiting for(;;) ?? brunnopessoa Programming 4 09-05-2004 10:27 PM
Exiting program w/ pthreads from sig handler LogicG8 Programming 1 07-29-2004 06:56 PM
libxml++: segmentation fault when exiting program. isl01jbe Programming 0 06-20-2004 06:47 AM
exiting program loops epoo Linux - Newbie 1 12-14-2003 03:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration