How would I fix these errors?

When I try to run it, it will go so far as the distance then stop. But it will not tell me a legitimate value for the distance, always 1. What it should do is allow the user to input the speed, hours and radius, then using this, output the total distance, number of laps, fuel used and the distance until the next completed lap.
[EDIT] - I need to find the remainder (hence the %) but it isn't working as I am using doubles rather than int.




Last edited on
well right away i see on line 32

cout << "The remaining distance to the finish line is: ";

you didn't display a variable to the screen

forgot to add the function remaining

***You are calling the function wrong in your cout statement!
line 25 you should pass the variables V and T
cout << totalDistance (V, T);


Call function remaining like this

cout << remaining(R);

What you are doing when you first create the functions like

double myFunc(double x , double y);

these are more or less "place holder values" not actual values until you call the function and pass your actual values into the function.

**This is how you use functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

using namespace std;

void myFunc(double placeHolderVariable); // this is called a prototype...lets compiler know that there is a function called myFunc before it is fully defined.

int main()
{
    double x = 5.5;
    double y = 6.3;
    
    myFunc(x);
    myFunc(y);
    
    //string xx = "asdf";
    //string yy = "asdfasdf";
    
    //myFunc(xx); // myFunc only accepts doubles this will not work or int since double basically extends ints functionally
    //myFunc(yy);
    
    // int variable will work with double
    
    int xx = 5;
    int yy = 7;
    
    myFunc(xx);
    myFunc(yy);
    
    return 0;
}

void myFunc(double placeHolderVariable)
{
    cout << "This variable can have a different name than what goes into it...and only variables with the same DataType can be passed into the function" << placeHolderVariable << endl;
}
Last edited on
line 25, 27 and line 30 you don't need the type of variable just the name of variable. You only need the type when you are declaring it.

What are you trying to do with
1
2
3
4
5
6
7
8
9
double Radius (double R){
	return R;
}
double Velocity (double V){
	return V;
}
double Time (double T){
	return T;
}
? These seem 100% useless to me. It would help if you mentioned what you were trying to do.

1
2
3
4
5
6
const double fuel (double totalDistance){
	return 0.25 * totalDistance;
}
int Laps (double Radius){
	return totalDistance/(2*M_PI*Radius);
}
Why not name these better like "fuelConsumed" and "lapCompleted" and get rid of the ambugity with the local variables in the main function that are 100% useless. It almost seems like you are trying to assign to those variables with these functions. If that is the case then you would have to do either a) fuel *= totalDistance; or fuel = fuelConsumed(totalDistance); Though, fuel is a constant so you can't modify the value anyways.

By the way if you try and compile it you will get these errors:
In function 'int main()': 
25:25: error: expected primary-expression before 'double' 
25:42: error: expected primary-expression before 'double' 
27:9: error: expected primary-expression before 'const' 
27:9: error: expected ';' before 'const' 
30:10: error: expected primary-expression before 'int' 
30:10: error: expected ';' before 'int' 
15:15: warning: unused variable 'fuel' [-Wunused-variable] 
16:6: warning: unused variable 'Laps' [-Wunused-variable] In function 'int Laps(double)': 
52:37: error: invalid operands of types 'double(double, double)' and 'double' to binary 'operator/' In function 'int remaining(double)': 
54:29: error: new declaration 'int remaining(double)' 
9:8: error: ambiguates old declaration 'double remaining(double)' 
55:37: error: invalid operands of types 'double(double, double)' and 'double' to binary 'operator%' 
56:1: warning: control reaches end of non-void function [-Wreturn-type] In function 'int Laps(double)': 
53:1: warning: control reaches end of non-void function [-Wreturn-type]
For the part about the fuel, what I am trying to do is multiply 0.25 by the totalDistance calculated earlier and output it during the main function (without completing the calculations inside main).

This is what I have now, but it obviously still doesn't work. Again, it stops working once it hits line 25, where it should tell me the value of velocity * time (both numbers inputed by the user)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cmath>
using namespace std;

double remainingDistance (double Radius);

int main () {
	double R = 0.3 ;
	double V = 0.4;
	double T = 0.5;
	const double fuelConsumed = 0.25;
	int lapsCompleted = 3;

	cout << "The remaining distance to the finish line is: ";
	cout << remainingDistance (R);
	cout << "km. \n";
	return 0;
}
int remainingDistance (double Radius){
	return totalDistance%(2*M_PI*Radius);
}
[/output]
Last edited on
That doen't even compile. You are missing the basics on how to write correct code.

On line 46 and 49 you are using functions as if they were variables. Functions are not variables, they may calculate and return data, and to do this you have to call the functions (like on line 25).
Furthermore, remainingDistance is declared (line 9) as returning a double and defined (line 48) as returning an int. This is illegal.
Last edited on
closed account (48T7M4Gy)
You need to define M_PI. You can use "const double MP_I = ..." in the appropriate place
Thanks for all the suggestions! I've done some additional readings on the basics but I still have some questions. Are lines 14-17 necessary? And again, at line 26 I am trying to output the calculation from line 43, but thats where things stop and outputs 1 every time. I am so lost. Thank you so much in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <cmath>
using namespace std;

double totalDistance (double V, double T);
const double fuelConsumed (double V, double T);
int lapsCompleted (double R, double V, double T);
double remainingDistance (double R, double V, double T);

int main () {
	double R;
	double V;
	double T;
	double totalDistance = V * T;
	const double fuelConsumed = 0.25 * totalDistance; 
	int lapsCompleted = (V * T) / (2*3.14*R);
	double remainingDistance = (V * T) % (2*3.14*R);

	cout << "Enter the Radius of the circular path in km:";
	cin >> R;
	cout << "Enter the speed of the car in km/h: ";
	cin >> V;
	cout << "Enter the Time of the race in hours: ";
	cin >> T;
	cout << "The total distance traveled was: ";
	cout << totalDistance (V,T);
	cout << " km.";
	cout << "The amount of fuel consumed was: ";
	cout << fuelConsumed (V,T);
	cout <<" L.";
	cout << "The driver completed:";
	cout << lapsCompleted (V, T, R);
	cout <<"Laps.";
	cout << "The remaining distance to the finish line is: ";
	cout << remainingDistance (V, T, R);
	cout << "km. \n";
	return 0;
}


double totalDistance (double V, double T)
{
	return V*T;
}

const double fuelConsumed (double V, double T)
{
	return 0.25*V*T;
}

int lapsCompleted (double V, double T, double R)
{
	return (V*T) /(2*M_PI*R);
}

double remainingDistance (double R, double V, double T)
{
	return (V*T)%(2*3.14*R);
}
closed account (48T7M4Gy)
OK just as a start - your first proble occurs at line 17. You are referring to integer division on double variables. Best thing to do is replace % with /

Also you need to change M_PI to 3.14 everywhere eg lines 53 and 58.

Make those changes and get back. You'll still have errors but at least you'll move forward.
Up to you :-)

I think I changed those two in a post above. I will edit the original post too
closed account (48T7M4Gy)
So what have u got?
Are lines 14-17 necessary?

You're creating variables with the same names as functions. This is a poor practice. The compiler knows the difference, but this can be confusing to the reader.

First, you're not actually using the variables created on lines 14-17, so yes, these variables can be removed. Second, every one of these calculations uses uninitialized variables, so the results are garbage.
Last edited on
How would I fix the uninitialized variables?
Just to avoid confusion, below is what I have most recently. Not sure if I even need cmath.
my error is line 46 "error: invalid operands to binary expression ('double' and 'double')" But i need to calculate the remainder.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cmath>
using namespace std;


void totalDistance (double Velocity, double Time);
void fuelConsumed (double Velocity, double Time);
void lapsCompleted (double Radius, double Velocity, double Time);
void remainingDistance (double Radius, double Velocity, double Time);

int main () {

double R = 0.0;
double V = 0.0;
double T = 0.0;

cout << "Enter the Radius of the track in km:";
cin >> R;
cout << "\n Enter the Velocity of the racecar in km/h: ";
cin >> V;
cout << "\n Enter the Time of the race in hours: ";
cin >> T;
cout << endl;

totalDistance (V,T);
fuelConsumed (V, T);
lapsCompleted (V, T, R);
remainingDistance (R, V, T);

return 0;
}

void totalDistance (double Velocity, double Time){
	cout << "The total distance traveled was: " << Velocity * Time << "km. \n\n";
}

void fuelConsumed (double Velocity, double Time){
	cout << "The amount of fuel consumed was: " << 0.25 * Velocity * Time << "L. \n\n";
}

void lapsCompleted (double Velocity, double Time, double Radius){
	cout << "The driver completed: " << (Velocity * Time) / (2*3.14* Radius) << "Laps. \n\n";
}

void remainingDistance (double Velocity, double Time, double Radius){
	cout << "The remaining distance to the finish line is: " << (Velocity * Time)%(2*3.14*Radius) << "km. \n\n";
}
Instead of using the % operator, is there any way to store the answer of lapsCompleted as a variable?
The modulo operator is defined only on integers, because floating point division doesn't yield any remainder, just a fractional result.
There is however a function that does this: http://www.cplusplus.com/reference/cmath/fmod/

Instead of using the % operator, is there any way to store the answer of lapsCompleted as a variable?
1
2
3
4
5
6
7
8
9
10
int function(int a, int b)
{
    return a+b;
}

int main()
{
    int variable = function(1, 1);
    return 0;
}
closed account (48T7M4Gy)
is there any way to store the answer of lapsCompleted as a variable?

The short answer is yes.

What you need your function to do is, after you send it V , T and R, to send back the number of laps. And that number is an int.

So, first step, you need to change void to int at lines 41 and 8.

Next in the function you need to add a line return (int)((Velocity * Time) / (2*3.14* Radius)); This goes after line 42. (int) converts the intermediate answer to whole laps.

Now in your main() coding instead of saying lapsCompleted (V, T, R); you can write int xyz = lapsCompleted (V, T, R); now you have the laps stored in the variable xyz which you can display cout << etc or use elsewhere.

You can do the same for all the others ... :-)
Last edited on
How would I fix the uninitialized variables?

Don't use the variables until you've stored something meaningful in them.
You did that by eliminating lines 14-17 (two posts back).

In addition to the use of the % operator that was mentioned, you have a problem with your call to remainingDistance(). At line 28, you call remainihngDistance with (R,V,T), but at line 45, the function is expecting (V,T,R).

Your code as posted will certainly work, once you've fixed those two problems, however, I have to say I liked your earlier code better where each of your functions returned a value, and you used that value in your cout statements in main. This is purely a matter of style though. The functions as you originally had them served a single purpose: Do a specific calculation. You could use the result of that calculation in a cout statement, or in another calculation. Combining the combining the calculation and cout functionality in a single function means that you can no longer use the functions just for calculation.
Topic archived. No new replies allowed.