Jump to content
  • Advertisement
Sign in to follow this  
ugoff

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.

If you intended to correct an error in the post then please contact us.

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 multi_answer;
	multi_answer = multi1 * multi2;
	return multi_answer;
}

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

float subtract(float val1, float val2)
{
	float sub_answer;
	
	sub_answer = val1 - val2;
	return sub_answer;
}

float add(float val3, float val4)
{
	float add_answer;
	
	add_answer = val3 + val4;
	return add_answer;
}

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

	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;
	cout << "Ok, on to addition. First Number to add? ";
	cin >> num3;
	cout << "Alright, second number? ";
	cin >> num4;
	
	add_solved = add(num3, num4);
	cout << num3 << " + " << num4 << " = " << add_solved << endl;
	cout << endl;
	cout << "Ready to multiply!? First number please :-): ";
	cin >> multi1;
	cout << "Second number for multiplying? ";
	cin >> multi2;
	multi_answer = multi1 * multi2;
	cout << multi1 << " * " << multi2 << " = " << multi_answer << endl;
	cout << endl;
	cout << "Isnt multiplication great!! Ok, lets try squaring a number now: ";
	cin >> square1;
	square_answer = square(square1);
	cout << square1 << " squared = " << square_answer << endl;
	return 0;
}

THANK YOU!!

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Firstly - size isn't important.

Secondly - your code is probably more readable and correct than your classmates. Don't worry about it.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 Calculator
August 03, 2004
By: 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 this post


Link to post
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 ugoff
not have someone write the program for me


...

Share this post


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

Share this post


Link to post
Share on other sites
Quote:
Original post by ugoff
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


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!