Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
1. When I run my code the input is weird and it won't display anything until I type an operation, it's kind of hard for me to explain this but build it for yourself and you'll most likely see the problem

2. I've made a basic calculator now how do I make it a fraction calculator. Could you perhaps point me in the right direction?


#include<iostream>
#include<fstream> 
#include<string>
#include<iomanip>
using namespace std;
//typedef will simplify declaration of variables
typedef int i;
typedef char c;
typedef bool b;
typedef double d;
void Operands(d&n, d &d); //returning: n = numerator d = denominator

d x = 1.0, y = 1.0, result; 
c operands;
ofstream outfile;
int main(){
	Operands (x,y);
	
	b numerator, denominator;
	cout << "Input operation: ";
	cin >> x >> operands >> y;
	cout << "The result is: " << result << endl;
	system("pause");
	return 0;
}
//function header
void Operands(d &n, d &d){
	x = n;
	y = d;

	switch (operands)
	{
	case '+':
		result = x + y;
		break;

	case '-':
		result = x - y;
		break;

	case '*':
		result = x * y;
		break;

	case '/':
		result = x / y;
		break;
		
	default:
		cin >> x >> operands >> y;
		Operands(x, y);
		break;
	}
}
Posted
Comments
OriginalGriff 15-Mar-15 4:45am    
No, I won't "Build it and try it".
You want help from me, you need to put a bit of effort in to hellping me to help you.
So explain what you expect to happen, and then explain what does happen. "the input is weird" tells me absolutely nothing, and I'm not running code that you have already told me is defective on my machine.

Use the "Improve question" widget to edit your question and provide better information.
TrevonH 15-Mar-15 14:19pm    
This is a questions and answers forum, either you answer my question or leave I don't care about how you feel, this isn't about you it's about me.
OriginalGriff 15-Mar-15 14:52pm    
That is an attitude that will take you a long way; but probably not in a direction you expect.
Keith Barrow 16-Mar-15 6:39am    
Voted as abuse. With that attitude I'm sure you'd have no trouble finding a top flight job in the housekeeping or food service industry.
You're question isn't clear - it isn't about how OG "feels".
What is clear is your code isn't functional - even as someone with minimal c++ experience it's pretty clear it won't work.
TrevonH 16-Mar-15 7:09am    
Just stop responding

1 solution

//typedef will simplify declaration of variables
Don't do that. It makes the code hard to read for others.

You should ask for the operation before calling Operands(). So move the call behind the input line.

It is also good style to pass the operation mode to the function and return the result rather than using global variables:
double Operands(char operation, double &n, double &d)
{
    double result = 0.0;
    switch (operation)
    {
    // handle supported onces here
    default:
        cout << "Unknown operation "  << operation << endl;
    }
    return result;
}

Than you can also remove the data input from this function.

Regarding fraction calculation see the modf[^] function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900