General

Chat

Any C++ experts here?

EDIT// C++ didn't show up in title

I've been given functions that contain errors in them and what I need to do is to find them and rewrite the correct function.
Are my answers correct? If not could anyone guide me through this?

pastebin as requested http://pastebin.com/HeTxAZhX

1. void total (int value1, value2, value3)
{
return value1 + value2 + value3;
}

2. double average(int value1, int value2, int value3)
{
double average;
average = value1 + value2 + value3 / 3;
}

3. void area (int length = 30, int width)
{
return length * width;
}

4. void getValue(int value& )
{
cout << "Enter a value: ";
cin >> value&;
}

These are my answers.

1. - use a cout statement instead of return and include brackets

void total (int value1, value2, value 3)
{
cout<< (value1 + value2 + value 3) << endl;
}

2. - include brackets around the values and unnecessary declaration inside the function

double average(int value1, int value2, int value3)
{
average = (value1 + value 2 + value3)/3;
}

3. - can't return an equation, width no value
void area (int length = 30, int width)
{
area = (length * width);
return 0;
}

4. - only need cin
void getValue(int value& )
{
cin >> value&;
}

any help at all would be appreciated! thank you!

March 8, 2014

7 Comments • Newest first

icemage11

[quote=prestigechef]woah...C++ is so similar to Java. I never knew it was THIS similar o-o[/quote]

this is only very basic C++. wait until you get to pointers...ugh pain in the ass.

Reply March 8, 2014
prestigechef

woah...C++ is so similar to Java. I never knew it was THIS similar o-o

Reply March 8, 2014
iDrinkOJ

1. void function can't return anything; can make it: int functionname
type not declare for parameter value2, value3.

2. can't get a double doing integer division, have to multiple it by 1.0 or change type of parameters to double
return type is double but function doesn't return anything; so make it return double or change return type to void in function name.

3. void function can't return anything, change return type or make it not return anything. i don't think you can do declare it length= 30 in parameter. idk.

Reply March 8, 2014
Hexadecagon

can't you pastebin this or something?
looking at this makes me physically ill

Reply March 8, 2014
Spectatable

I agree with @neuro:, then again i've only been in c++ for like 4 months

Reply March 8, 2014
xoqtprincessxo

I have no C++ experience, so I don't know the syntactical details

1. Change the return type to int, declare value2 and value3 as int in the function params, keep the return statement as it is in the original.
It makes more sense than getting rid of the return and printing output

2. You probably need to return a double value.
You probably need to declare average as a double, or just

return (value1 + value2 + value3)/3;

3. Change return type to int, declare area as an int, return area. Or just return length * width.
Get rid of the value assignment to length in the function params.

Reply March 8, 2014 - edited
Neuro

1. shouldnt it return something?
2. ^
3. void function, length value shouldnt be in param

Reply March 8, 2014 - edited