Hello all, this is my first post. My problem is with division and/or the way i am declaring variables. here's my code:

#include <iostream.h>

int main () {

	int minutes;
	float hours;
	float minutes_late;
	
	
	cout << "How many minutes late are you? " ;
	cin >> minutes;
	
	hours = minutes / 60;
	minutes_late = hours / 60;
	
	
	
	cout << "You are " << hours << " hour(s) " << minutes_late << " minutes late " << endl;

basically, when i try to insert a number for minutes, such as 73.5, it will say 1 hour and 13 minutes late, leaving off the .5 .....thank you for all your help

#include <iostream>

int main () {

	float minutes;
	float hours;
	float minutes_late;

Is this the change you are talking about?

If so i get the following error messages:
project2.cpp: In function `int main()':
project2.cpp:10: error: `cout' was not declared in this scope
project2.cpp:11: error: `cin' was not declared in this scope
project2.cpp:18: error: `endl' was not declared in this scope


Thanks.

Under <iostream> type "using namespace std;" without the quotes. still use float, it will work on almost any compiler!

That does work- however, I need my Hours to appear in interger form.

Suggestions?

THen you are going to have to round off with just using "int", do you want apples or oranges?

>still use float, it will work on almost any compiler!
Almost any compiler, huh? And here I thought that because it's a standard type, every C++ compiler is required to support it. :icon_rolleyes:

>That does work
It does? Presumably you want 60.5 to give you 1 hour and 30 minutes, but that's certainly not what your formula does, even when minutes is a floating-point data type. The hour calculation is simple and works as you have it, but the minute calculation is slightly more difficult. You need to slice the whole value off of minutes and multiply the fractional part by 60. This gives you a reasonable approximation:

#include <iostream>

int main()
{
  double minutes;
  double hours;
  double minutes_late;

  std::cout << "How many minutes late are you? " ;
  std::cin >> minutes;

  hours = minutes / 60.0;
  minutes_late = ( minutes - static_cast<int> ( minutes ) ) * 60.0;

  std::cout << "You are " << static_cast<int> ( hours )
    << " hour(s) " << static_cast<int> ( minutes_late )
    << " minutes late\n";
}

basically, when i try to insert a number for minutes, such as 73.5, it will say 1 hour and 13 minutes late, leaving off the .5 .....thank you for all your help

First of all, why would you want to keep track of .5 minutes? Make them all int s, then the formula becomes

hours = minutes / 60;
minutes = minutes % 60;

Now you have total minutes converted into hours and minutes

This article has been dead for over six months. Start a new discussion instead.