Need help :)

Hey guys good evening.

So i've been stuck on this for a bit.
The exercise asks me to get 2 answer from a void().
So I tried it doing it with void but I forgot how to do it correctly.
Tried it with seperate int()'s and it worked with the answers.

The exercise gives time = t, distance1 = a1, distance = a2;
It asks for the speed . So the easy way to get the answer is to : distance/ time and multiply it by 3.6 to get the asnwer.

( 20/5 = 4; 4 * 3.6 = 14.4)

The numbers are 5 20 25
Answers are
The first cars speed : 14.40
The second cars speed : 18.00


Idk what im doin wrong.
void Carspeed (int t , int a1, int a2, double speed1, double speed2)
//---------------------------------------
int main ()
{
int a1, a2, t;
double speed1, speed2;

cin >> t >> a1 >> a2;


Carspeed (5 , 20, a2, speed1 , speed2 );
Carspeed (5 , a1, 25, speed1 , speed2 );

cout << fixed << setprecision(2) << "The first cars speed: " << speed1 << endl;
cout << fixed << setprecision(2) << "The second cars speed: " << speed2 << endl;


}
//----------------------------------------
void Carspeed (int t , int a1, int a2, double speed1, double speed2)
{
speed1 = (a1 / t) * 3.6;
speed2 = (a2 / t) * 3.6;
}
Any help would be appreciated :)
Consider using references for speed1 and speed2 as parameters.

https://www.tutorialspoint.com/cplusplus/passing_parameters_by_references.htm

Also:
Please learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
You haven’t actually asked a question...

I’m going to just guess (by glancing at it) that your answers are zero or something.
You need to use a double (or float) to perform non-integer arithmetic.

That is, an int divided by an int is always an int, even if you afterwards assign the value to a double. Make sure that your source operands are also double:

 
  double a1, a2, t;

Hope this helps.
Using code tags and function parameter references in a complete compileable example (along with a bit of formatting and more descriptive variable names):
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
#include <iostream>
#include <iomanip>

void Carspeed(double time, double dist1, double dist2, double& speed1, double& speed2); // <<-- forgot the ;

int main()
{
   double dist1;
   double dist2;
   double time;
   double speed1;
   double speed2;

   std::cout << "Enter time, distance 1 and distance 2: ";
   std::cin >> time >> dist1 >> dist2;

   Carspeed(5, 20, dist2, speed1, speed2);
   Carspeed(5, dist1, 25, speed1, speed2);

   std::cout << std::fixed << std::setprecision(2) << "The first cars speed: " << speed1 << '\n';
   std::cout << std::fixed << std::setprecision(2) << "The second cars speed: " << speed2 << '\n';
}

void Carspeed(double time, double dist1, double dist2, double& speed1, double& speed2)
{
   speed1 = (dist1 / time) * 3.6;
   speed2 = (dist2 / time) * 3.6;
}
Enter time, distance 1 and distance 2: 5 20 25
The first cars speed: 14.40
The second cars speed: 18.00
For gits and shiggles I replaced the <iomanip> library with C++20's <format> library, making (IMO) the formatting used easier to understand with less typing.

Replace #include <iomanip> with #include <format> and change lines 20-20:

20
21
   std::cout << std::fixed << std::setprecision(2) << "The first cars speed: " << speed1 << '\n';
   std::cout << std::fixed << std::setprecision(2) << "The second cars speed: " << speed2 << '\n';

with

20
21
   std::cout << std::format("The first car's speed: {:.2f}\n",  speed1);
   std::cout << std::format("The second car's speed: {:.2f}\n", speed2);

I'm really, really liking what the <format> library can do. :)
Last edited on
So that means if I use void do I have to use double? Or do I use it differently depending on the question itself?
And thank you guys for the help. Appreciate it a lot :)
Oh and a question. What does std:: mean? I know what ios::app means but thats the first time seeing that. What does it do?
Last edited on
So that means if I use void do I have to use double?

No, you don't HAVE to use double, just recommended because of the potential problems with int division. See the reply by Duthomas.

What does std:: mean?

I'm gonna bet you have using namespace std; in your code, right?

https://stackoverflow.com/questions/5469060/why-is-std-used-by-experienced-coders-rather-than-using-namespace-std
https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
Last edited on
Everything in C++ has a type. Not all things need have the same type.

A function's return type is only the type of value it returns. Currently your Carspeed function does not return any value, so it’s return type is, correctly, void. (It modifies the value of two reference arguments, sometimes called something like “out” arguments.)

According to your apparent instructions you are doing it correctly.

But in real code, you should try to have a function do one thing instead of multiple things. I do not know where 3.6 comes in to your equation, but the speed of something is (distance ÷ time), hence:

    t=5, d=20 → 20/5 = 4 

If we were talking, say, kilometers per hour, then 20 kilometers ÷ 5 hours = 4 kph.

You could write a simple function:
1
2
3
4
5
double speed( double distance, double time )
{
  if (time == 0.0) return 0.0;
  return distance / time;
}

And use it thus:
1
2
3
4
5
6
7
8
9
10
11
12
13
  double d1, d2, t;

  std::cout << "distance 1? ";
  std::cin >> d1;

  std::cout << "distance 2? ";
  std::cin >> d2;

  std::cout << "time? ";
  std::cin >> t;

  std::cout << "speed 1 = " << speed( d1, t ) << "\n";
  std::cout << "speed 2 = " << speed( d2, t ) << "\n";


Hopefully this helps in your thinking. The assignment seems to be to use reference arguments with a void function so you are doing OK, as far as I can see.

Hope this helps.
I consider this exercise to be rude. It is bad form to pass arguments by reference and change their value as I do not know that is happening by just looking at the function call.

Secondly, if you use return value double, you do not need to pass speed1 and speed2 at all.
It is bad form to pass arguments by reference and change their value as I do not know that is happening by just looking at the function call.

C++ Core Guidelines has this suggestion:

F.17: For “in-out” parameters, pass by reference to non- const
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines.html#Rf-inout

Reason This makes it clear to callers that the object is assumed to be modified.


Knowing the object might be modified is never bad form, it is essential.

Objects can be obscenely expensive to copy by value, so passing by pointer or reference is what is done.

Using pointers to pass objects is all C allowed. Using references, const or otherwise, is good C++ practice.
if you use return value double, you do not need to pass speed1 and speed2 at all.

The function in question is void, so no values can be returned at all. Modifying the return value is "not part of the assignment."

Function returns allow only one value to be passed out. Using references (or pointers) allows 2 or more values to be passed in, potentially modified, and then passed out. The calling code as currently written needs two different values to be returned from the function.

Using pointers instead of references tends to make for messy code. To modify a value you have to de-reference the pointer, each and every time. What happens if you forget that?

Code intent should always be clear. What intent can you derive from the following implicit and explicit loops?

1. for (const auto& v: vec) { ... }

2. for (auto& v: vec) { ... }

3. std::for_each(std::execution::par, vec, [](auto v) { ... });

Loop (1) does not modify the elements of the container vec.

This does not hold for the range-based for loop (2). The elements can be modified, though it is not a requirement they will be.

Loops (1) & (2) process the elements in sequential, ascending order.

The algorithm std::for_each (3) performs its job in parallel (std::execution::par). This means that we don’t care in which order the elements are processed.

Expressing intent is also an important guideline for good documentation of your code. Documentation should state what should be done and not how it should be done.
Last edited on
Registered users can post here. Sign in or register to post.