You are here:

C++/output incorrect, please help


Hi there,

I know that I already asked you a question a couple days ago, but I asked someone else and they could not answer my question. I am hoping that you will be able to.

I am having great difficulty getting my program to work correctly. My program is designed to define the rational class  and have the user to choose their input and output method. The user also chooses the type of operation that they want to do. My problem is that the program appears to output the value for the default constructor and not the correct value. I am a student and I have done all that I can think of to do to mend this problem. I am not supposed to use overloaded operators for this assignment, but I know that that would make it a lot simpler. Thank you for helping me make my program work correctly.

Here is my code:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
using namespace std;

class Rational {
         Rational ( );
         Rational(int leftOp);
         Rational(int leftOp, int rightOp);
         // Accessors
         void input(istream& in);
         void output(ostream& out);
         bool less(Rational r);
         // Factories
         Rational neg( );
         Rational add(Rational r);
         Rational sub(Rational r);
         Rational mul(Rational r);
         Rational div(Rational r);
         void fix ( );
         int n;
         int d;
void getInput(ifstream& input, Rational& r1, Rational& r2);
void askType( );
bool again( );

int main ( )
   Rational a, b, c;
   char answer;
   int n, d;
   ifstream in, input;
   ofstream out, output;
   cout << "This program will ask the user to choose an input method for rational \n"
       << "numbers and an operation to perform with the numbers. It will then \n"
       << "perform the operation and output to the users chosen location.\n"
       << " \n"
       << "When inputting two numbers separated by a / for both left and right operand.\n"
       << " \n";
   getInput(input, a, b);
   askType( );
   input.close( );
// output
   cout << "Do you want to output to the screen? (y/n)\n"
         << "Enter 'n' when want to output to a file.\n";
     cin >> answer;
   if (answer == 'y' || answer == 'Y') {
       cout << "The result of the selected operation is ";
       c.output(cout); }
   else {
       cout << "The output will be put into a file."
         << "The result of the selected operation is ";
   if ( )) {
         cout << "Open file failed.\n";
         exit(1); }
       output << "The result of the selected operation is ";
       output.close( );}
// end output   
   return 0;
Rational::Rational(int leftOp) {
 n = leftOp;
 d = 1; }

Rational::Rational(int leftOp, int rightOp) {
 n = leftOp;
 d = rightOp;
 fix( ); }

Rational::Rational( ) {
 n = 0;
 d = 1; }

void Rational::input(istream& in) {
char c;
   in >> n;
   in >> c;
   in >> d;
   fix ( ); }   

void Rational::output(ostream& out) {
   out << n;
   out << '/';
   out << d; }
bool Rational::less(Rational r) {
 return (n*r.d) < (r.n*d); }

Rational Rational::neg( ) {
 n *= -1; }

Rational Rational::add(Rational r) {
 Rational temp;
 temp.n = (n*r.d + d*r.n);
 temp.d = (d*r.d);
 temp.fix( );
   return temp; }

Rational Rational::sub(Rational r) {
 Rational temp;
 temp.n = (n*r.d - d*r.n);
 temp.d = (d*r.d);   
 temp.fix( );
   return temp; }

Rational Rational::mul(Rational r) {
 Rational temp;
 temp.n = (n*r.n);
 temp.d = (d*r.d);
 temp.fix( );
   return temp; }

Rational Rational::div(Rational r) {
 Rational temp;
 temp.n = (n*r.d);
 temp.d = (r.n*d);
 temp.fix( );
   return temp; }

void Rational::fix( ) {
   if (d < 0) {
         d *= -1;
         n *= -1; } }

void getInput(ifstream& input, Rational& r1, Rational& r2) {
char answer;
int n, d;
   cout << "Do you want to input from the keyboard? (y/n)\n"
         << "Enter 'n' when you want to input from a file.\n";
   cin >> answer;
   if (answer == 'y' || answer == 'Y') {
       cout << "Please input a left operand as a rational number.\n";
       cout << " \n";
       cout << "Please input a right operand as a rational number.\n";
       cout << " \n";}
   else {
       cout << "The input will be taken from a file.";
         if ( )) {
         cout << "Open file failed.\n";
         exit(1); }} }
void askType ( ) {
   string type;
   Rational a, b, c;
   cout << "What type of calculation do you want to do? (less, neg, add, sub, mul, div) \n";
   cin >> type;
   cout << " \n";
 if (type == "less")
     c = a.less(b);
 else if (type == "negate")
     c = a.neg( );
 else if (type == "add")
     c = a.add(b);
 else if (type == "subtract")
     c = a.sub(b);
 else if (type == "multiply")
     c = a.mul(b);
 else if (type == "divide")
     c = a.div(b); }

Hi, I came through the program and have some questions. Please, do answer it so that I can proceed further.
In Rational::input function you have used a charcter c as a local variable and you are not using any where?

In function  askType, you have declared locale variables Rational a, b, c; and then you perform the operations on it. I think you should pass the arguments to the function and then do corresponding operation on it.Since the values of a,b,c are not the values you set from input function.


All Answers

Answers by Expert:

Ask Experts




Difficult questions related to C .


C ,vc ,opc ,windows CE, ActiveX

Masters in electrical engineering

©2017 All rights reserved.

[an error occurred while processing this directive]