a c++ program to calculate time given the speed and distance

Have been on this for 2days, have written it severally but i keep on running into errors . DOnt what to do again....



#include <iostream>
using namespace std;
int main()
{
int speed; // km per hour
int km; // km
cout << "Please enter the speed:";
cin >>[40];
cout << "please enter the total km traveled:";
cin >> [100];
cout << endl << endl << "it will take a cyclist" << distance / speed << "hours for the cyclist to get its" << endl;
cout << "destination.";
return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
int main()
{
  int speed;
  int km;
  cout << "Please enter the speed:";
  cin >>[40]; 
  cout << "please enter the total km traveled:";
  cin >> [100];
  cout << endl << endl << "it will take a cyclist" << distance / speed << "hours for the cyclist to get its" << endl;
  cout << "destination.";
  return 0;
}

Please use code tags.

cin >> [40]; You need to read it into a variable, like your speed variable. cin >> speed;
The same for con >> [100];. You want the km traveled, so think about which variable you want to store that in.

cout << endl << ... << distance / speed << ...
A couple issues.
distance isn't declared anywhere. You'll want to use the actual name of the variable you stored the distance traveled in.
This is integer division. Integer division truncates towards zero, so if you want to keep any decimal places, you'll need to use doubles or floats - something that has decimal precision.
cout << (distance * 1.0) / speed << .... Change the distance variable accordingly.
i dont understand, am kind of lost here.
Thanks alot got it running;



#include <iostream>
using namespace std;
int main()
{
int speed;
int distance;
cout << "Please enter the speed:";
cin >> speed;
cout << "please enter the distance:";
cin >> distance;
cout << endl << endl << "it will take a cyclist" << distance/speed << "hours .to get its" << endl;
cout << "destination.";
return 0;
}
cin >> [40];
We can't do that. You need to read into something that can store it.
cin >> speed; Will get the speed entered by the user and store that value in speed (assuming the user didn't under something invalid, like a non-numeric character).
The same applies for reading in the km traveled.
If you want to just assign some value to it, you can do that int speed = 40;. The value of speed will be 40 to start with. The same applies to km

In C++, diving two ints won't give you something with decimals. The division result will only contain the quotient and no remainder as a decimal.
5/7 in C++ will be 2, not 2.5. 1000/333 will be 3, not 3.003...
The same applies to any integral type (bool, char, short, int, long, long long), as they can only store whole numbers.

If you use floats/doubles, you can keep the remainder.
1
2
double speed;
double km;

Now doing km / speed will yield a result with decimals.
thanks



#include <iostream>
using namespace std;
int main()
{
double speed;
double km;
cout << "Please enter the speed:";
cin >> speed;
cout << "please enter the total km traveled:";
cin >> km;
cout << endl << endl << "it will take a cyclist" << km / speed << "hours for the cyclist to get its" << endl;
cout << "destination.";
return 0;
}
To do float math ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// A
int distance;
int speed;
// input
cout << (distance * 1.0) / speed;

// B
double distance;
double speed;
// input
cout << distance / speed;

// C
int distance;
int speed;
// input
cout << static_cast<double>(distance) / speed;

In the A the distance * 1.0 is evaluated first, and since the 1.0 is a double, the distance is converted to match before multiplication. The result of double*double is double. Therefore, before division the speed converts to double too, to get a "divide a double with a double".

In the B both operands are already double.

In the C we explicitly convert the distance into double (without multiplication). Therefore, before division the speed converts to double too, to get a "divide a double with a double".


There are situations, where you cannot have variables of some type, but nevertheless need a value of that type within evaluation. The static_cast is for those occasions.
Last edited on
It kinda looks like you don't know your basics.
- Variables need to be declared before used
- When using std::cin you need to have a variable, array, etc to put the input in (something you can store it in and use again)
- "[40]" is undefined or not declared and a variable couldn't be named that
- It seems like you want to use "km" and "distance" the same way so replace distance with km
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main()
{
   double speed;
   double km;

   cout << "Please enter the speed: ";
   cin >> speed;

   cout << "Please enter the total km traveled: ";
   cin >> km;

   cout << endl << endl << "It will take a cyclist " << km / speed << " hours for the cyclist to get to the destination.";
   return 0;
}
Last edited on
Topic archived. No new replies allowed.