Public Group

# Why do I have so much... code?!

This topic is 5074 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hey guys, could you help me out with something please. im in class right now, and they just had us write a basic calculator. oh, by the way, C++ ;-). so anyway, i get done with my calculator (and am very proud, dont know why). so i start looking at some of my peers' monitor's, and notice that they have SO much less code than me. so what im asking, is if someone could re-write my program to use less lines and trim it down, but still have the same functionality. also, just use the syntax thats in the program, nothing fancy like arrays, pointers, if/else, when... etc. you get what i mean. so anyway, here's my code, please fix it!!
/***************************************************************************************************************
Simple Calculator
August 03, 2004
By: Chris Egner
***************************************************************************************************************/

#include <iostream.h>

float multiply(float multi1, float multi2)
{
}

float square(float squ1)
{
float num_squared;
num_squared = squ1 * squ1;
return num_squared;
}

float subtract(float val1, float val2)
{

}

{

}

int main()
{
float num1, num2, num3, num4, multi1, multi2, square1;
float sub_solved;

cout << "Ok, first, we will subtract two numbers" << endl;
cout << "First Number? ";
cin >> num1;
cout << "Second number that is smaller than the first? ";
cin >> num2;

sub_solved = subtract(num1, num2);
cout << num1 << " - " << num2 << " is equal to: " << sub_solved << endl;
cout << endl;
cin >> num3;
cout << "Alright, second number? ";
cin >> num4;

cout << num3 << " + " << num4 << " = " << add_solved << endl;
cout << endl;
cin >> multi1;
cout << "Second number for multiplying? ";
cin >> multi2;
cout << multi1 << " * " << multi2 << " = " << multi_answer << endl;
cout << endl;
cout << "Isnt multiplication great!! Ok, lets try squaring a number now: ";
cin >> square1;
cout << square1 << " squared = " << square_answer << endl;
return 0;
}


THANK YOU!!

##### Share on other sites
There are always multiple answers to a problem, as long as your way works, then there is no real reason to worry about code length; especially since it's for learning.

But my guess is your friends are just directly returning the answer instead of using temporary variables and a seperate expression.

ie. return (multi1 * multi2);

Good luck.

##### Share on other sites
Firstly - size isn't important.

##### Share on other sites
Maybe you could ask your instructor for hints as to what you could do better. I don't think that our doing your homework for you would teach you anything. And we're certainly not going to help you cheat on an assignment, sorry.

##### Share on other sites
score :-). thank you guys, i appreciate it

##### Share on other sites
lol, dont worry fruny. its not homework, and im not cheating. im just trying to fix my code, not have someone write the program for me

##### Share on other sites
Also, since all of the operations here are equivalent to C++ operators (except squaring, but it is just as simple), there's literally no need to define functions for every operation. Oh yeah, and there's nothing wrong with reusing variables to perform the same operation.

/***************************************************************************************************************Simple CalculatorAugust 03, 2004By: Chris Egner***************************************************************************************************************/#include <iostream>using namepace std;int main(){	float num1, num2;	cout << "Ok, first, we will subtract two numbers" << endl;	cout << "First Number? ";	cin >> num1;	cout << "Second number that is smaller than the first? ";	cin >> num2;	cout << num1 << " - " << num2 << " is equal to: " << (num1  - num2) << endl;	cout << endl;	cout << "Ok, on to addition. First Number to add? ";	cin >> num1;	cout << "Alright, second number? ";	cin >> num2;	cout << num1 << " + " << num2 << " = " << (num1 + num2) << endl;	cout << endl;	cout << "Ready to multiply!? First number please :-): ";	cin >> num1;	cout << "Second number for multiplying? ";	cin >> num2;	cout << num1 << " * " << num2 << " = " << (num1 * num2) << endl;	cout << endl;	cout << "Isnt multiplication great!! Ok, lets try squaring a number now: ";	cin >> num1;	cout << num1 << " squared = " << (num1 * num1) << endl;	return 0;}

Of course, the objective might have been to use functions for each operation so your instructor might have a problem with this. However, it should be obvious how redundant these functions really are, and functions are meant to reduce redundant code...

@Everyone else: I realize the homework policy, but in this situation (given the simplicity of the problem) I thought that a concrete example would be more informative for the OP.

##### Share on other sites
Quote:
 Original post by ugoff so what im asking, is if someone could re-write my program

Quote:
 Original post by ugoffnot have someone write the program for me

...

##### Share on other sites
You do realize that there are people from Digipen who read these forums?

##### Share on other sites
Quote:
 Original post by ugofflol, dont worry fruny. its not homework, and im not cheating. im just trying to fix my code, not have someone write the program for me

I'm not worried - it's your tuition money, not mine.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631435
• Total Posts
3000056
×