New Posts  All Forums:Forum Nav:

Easy help! c++

post #1 of 3
Thread Starter 
Hey guys im new to c++ / programming in general.
currently working on an assignment and i just about got it, but im having trouble debugging it and fixing my problems.

could anyone help me correct this PLEASE!!

// lab2.h

#include <iostream>
#include <cmath>
using namespace std;


//functions used...

void instructUser();
float computeSum(num1,num2,num3,num4);
float computeDifference(num1,num2,num3,num4);
float computeProduct(num1,num2,num3,num4);
float computeQuotient(num1,num2,num3,num4);





int main ()
{
float num1;//input: first number
float num2;//input: second number
float num3;//input: third number
float num4;//input: fourth number
char action;//input: operator
float sum;//output: sum
float difference;//output: difference
float product;//output: product
float quotient;//output: quotient


instructUser();
cout << "Input four numbers each followed by a space, and the operation" << endl;
cout << "you wish to use on these numbers. The operators are; + , - , * , / ." << endl;
cin >> num1 >> num2 >> num3 >> num4 >> action;//take in the four numbers and operator

if( action == '+' )
sum = computeSum(num1,num2,num3,num4);//if the action is add, initiate computeSum
else
if(action == '-' )
difference = computeDifference(num1,num2,num3,num4);//if the action is minus, initiate computeDifference
else
if(action == '*' )
product = computeProduct(num1,num2,num3,num4);//if the action is multiply, initiate computeProduct
else
if(action == '/' )
quotient = computeQuotient(num1,num2,num3,num4);//if the action is divide, initiate computeQuotient

//print out the sum, difference, product, or quotient.

if( action == '+' )
cout << "The sum of the four numbers is " << sum << "." << endl;
else
if(action == '-' )
cout << "The difference of the four numbers is " << difference << "." << endl;
else
if(action == '*' )
cout << "The product of the four numbers is " << product << "." << endl;
else
if(action == '/' )
cout << "The quotient of the four numbers is " << quotient << "." << endl;

return 0;
}
//display user instructions
void instructUser();
{
cout << "This program takes any four numbers desired by the user and computes " << endl;
cout << "one mathematical operation to all of the numbers. If the user chooses " << endl;
cout << "addition, then the sum of the four numbers is computed, and so on.." << endl;
}
// compute sum
float computeSum()
{
sum = num1 + num2 + num3 + num4;

return sum;
}

//compute difference
float computeDifference()
{
difference = num1 - num2 - num3 - num4;

return difference;
}

//compute product
float computeProduct()
{
product = num1 * num2 * num3 * num4;

return product;
}

//compute quotient
float computeQuotient()
{
quotient = num1 / num2 / num3 / num4;

return quotient;
}
post #2 of 3
First, surround your code with [ code] tags

Second, just think about your code.

Code:
float computeSum()
{
    sum = num1 + num2 + num3 + num4;

    return sum;
}
What is wrong here? The compiler is probably wondering what sum, and num1-4 are, it has no idea, since they are not defined in the scope (ComputeSum). They are defined in your main, but that is out of scope.

What it needs to look more like is how you defined it in your .h (header) file:

Code:
float computeSum(float num1, float num2, float num3, float num4)
{
    return num1 + num2 + num3 + num4;
}
Remember the computer is very dumb, it does not know what num3 is, you must tell it. All of the major mistakes that I see stem from this issue. Also, from the computers point of view, the function ComputeSum() is completely different that the function computeSum(float num1, float num2, float num3, float num4).
post #3 of 3
basically what shamrock said...

The variables that store the data from the keyboard need to be sent to the functions to do the compute operations. In the function header...you should have space for all the parameters in the add, subtract....operations.

Code:
float computeProduct(float num1, float num2, float num3){

      return num1 * num2 * num3;
}
Since you are just starting out...you will eventually get to error handling...exceptions and so on...in order for your program not to crash...such as division by zero in your computeQuotient function...but in the meantime get the fundamentals and syntax down just like you are doing.
Edited by surfbumb - 6/8/11 at 6:28pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming