Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,557 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,919 people online right now. Registration is fast and FREE... Join Now!




Problem with break/end of file statement

 
Reply to this topicStart new topic

Problem with break/end of file statement, Calculator Program

rancor
11 Apr, 2008 - 10:44 AM
Post #1

New D.I.C Head
*

Joined: 30 Nov, 2007
Posts: 22



Thanked: 3 times
My Contributions
This calculator program is supposed to exit specifically when the string "^D" is entered and output an error message (and continuing the loop afterward) if anything that isn't a number or a valid operator is entered. I've only figured out how to end the program with anything that isn't a number. I've tried using cin.eof() and cin.fail().... If any of you know the command to do this or have any other suggestions, that would be great!

Heres my code so far:
CODE

#include <math.h>
#include <iostream>
using namespace std;

double  firstOperand;
char    operation;      
double  secondOperand;  
double  result;      

double add(double,double);
double sub(double,double);
double mult(double,double);
double div(double,double);
double exp(double,double);

int main()
{
    cout << "Enter expression (^D to quit): ";
    
    while (cin >> firstOperand)
    {
        cin >> operation;
        cin >> secondOperand;
              
        if (operation == '+') {
            result = add(firstOperand,secondOperand);
        } else if (operation == '-') {
            result = sub(firstOperand,secondOperand);
        } else if (operation == '*') {
            result = mult(firstOperand,secondOperand;
        } else if (operation == '/') {
            if (secondOperand == 0) {
                cout << "Error: Cannot divide by zero" << endl;
                cout << "Enter expression (^D to quit): ";              
                continue;
            } result = div(firstOperand,secondOperand);
        } else if (operation == '^') {
            result = exp(firstOperand,secondOperand);
        } else {
            cout << "Invalid operator: " << operation << endl;
            continue;
        }
      
        cout << "The result is " << result << endl;
        cout << "Enter expression (^D to quit): ";
    }
    cout << "Thank you for using the calculator program.\n";
    return 0;
}

   double add(double a, double b)
   {
       return a + b;
   }
   double sub(double a, double b)
   {
       return a - b;
   }
   double mult(double a, double b)
   {
       return a * b;
   }
   double div(double a, double b)
   {
       return a / b;
   }
   double exp(double a, double b)
   {
       return pow(a,b);
   }
  


This post has been edited by rancor: 11 Apr, 2008 - 11:39 AM
User is offlineProfile CardPM
+Quote Post

jeronimo0d0a
RE: Problem With Break/end Of File Statement
11 Apr, 2008 - 12:19 PM
Post #2

D.I.C Head
**

Joined: 3 Mar, 2008
Posts: 141


My Contributions
cin is a strange animal. Here is the corrected program. Polish of the I/O I leave to you. cin.fail will not help unless you process it. remember that you can do a
cin >> x >> y >> z ;
cin returns an istream object whether it is successful or not so...
while(cin >> x)
is always true because cin returned itself
Hope this helps.
Jeronimo

CODE

#include <math.h>
#include <iostream>
#include <stdlib.h>
#include <conio.h>

using namespace std;

double  firstOperand;
char    operation;      
double  secondOperand;  
double  result;      

double add(double,double);
double sub(double,double);
double mult(double,double);
double div(double,double);
double exp(double,double);
char ch;
bool keep_going = 1;
int main()
{
    while (keep_going)
    {
        cout << "Enter expression (Q to quit): ";
        cin >> firstOperand;
        if (cin.fail()) // could not assign to int
        {
            cin.clear(); // end error condition
            cin >> ch; // get one char
        }
        if( (ch=='q') || (ch=='Q') ) { break; }

        cout << "\n\tEnter Operation: ";
        // cin >> operation // plus causes problems (not +, PLUS)
        operation = _getche();
        if( (operation=='q') || (operation=='Q') ) { break; }
        cout << "\nSecond Expression: ";
        cin >> secondOperand;
        if (cin.fail())
        {
            cin.clear();
            cin >> ch;
        }
        if( (ch=='q') || (ch=='Q') ) { break; }
              
        if (operation == '+') {
            result = add(firstOperand,secondOperand);
        } else if (operation == '-') {
            result = sub(firstOperand,secondOperand);
        } else if (operation == '*') {
            result = mult(firstOperand,secondOperand);
        } else if (operation == '/') {
            if (secondOperand == 0) {
                cout << "\n\nError: Cannot divide by zero\n\n" << endl;
                //cout << "Enter expression (^D to quit): ";              
                continue;
            } result = div(firstOperand,secondOperand);
        } else if (operation == '^') {
            result = exp(firstOperand,secondOperand);
        } else {
            cout << "Invalid operator: " << operation << endl;
            continue;
        }
      
        cout << "\n\tThe result is: " << result << endl;
        //cout << "Enter expression (^D to quit): ";
    }
    cout << "\n\n\tThank you for using the calculator program.\n\n\t";
    system("pause");
    return 0;
}

   double add(double a, double b)
   {
       return a + b;
   }
   double sub(double a, double b)
   {
       return a - b;
   }
   double mult(double a, double b)
   {
       return a * b;
   }
   double div(double a, double b)
   {
       return a / b;
   }
   double exp(double a, double b)
   {
       return pow(a,b);
   }
  




User is offlineProfile CardPM
+Quote Post

rancor
RE: Problem With Break/end Of File Statement
11 Apr, 2008 - 12:35 PM
Post #3

New D.I.C Head
*

Joined: 30 Nov, 2007
Posts: 22



Thanked: 3 times
My Contributions

Thanks you very much for your help, but the problem I'm having is a little different than the one you solved. My program needs to take in an expression in one line Ex. 5 + 4, not operand, operator, operand in three separate prompts. The problem lies in the input (cin << firstOperand), I need to be able to input the string ^D, not a character. I also need the program to be able to differentiate between characters (mistakes) in the input, and the quit string, ^D.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:26PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month