Little help on this please - very confused

Okay, so I'm a complete novice to c++, and am having a bit of an issue with this one particular program. I don't want the solution, I just want to know where I am going wrong... so anyway, here is what the program is supposed to do.

User is supposed to input two numbers for example

12 36, which is then supposed to show it as

12 hrs 36 minutes. (I have this part, so don't need help here)

The second part is supposed to drop it down and output it like this 12.36 hrs for the total hours, again, I am good to go here.

My problem is getting the modulus stuff right incase say someone puts 12 75, so that instead of it saying 12.75 hrs it instead rolls over to say 13.15 hrs.

I know that my issue lies somewhere within my math (as I am pretty horrible at math) so any suggestions will be greatly appreciated guys/gals. I'll post my code below, also, it is no where near completed and still needs formatting so don't knock me too hard on it.

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
#include <iomanip>
#include <cstdlib>
#include <iostream>


using namespace std;

int main()
{
    int hrs, min;
    double totaltime;
    totaltime=(hrs+min)%60;
	
    cout <<"Please enter your time, ex 12 45 "<<endl<<endl;
	
    cin>>hrs>>min;
	
    cout <<hrs<<" Hours" << setw(3) <<min<<" Minutes" <<endl<<endl;
	
	cout << setiosflags(ios::fixed)
             << setiosflags(ios::showpoint)
             << setprecision(0);
		 
    cout << totaltime << min<< " Hours" <<endl;  //<<min is to show the 12.<min> for some reason it refuses to show without that.
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Last edited on
Hmm..

1
2
3
4
5
6
7
8
9
int hrs, min;
double totaltime;
totaltime=(hrs+min)%60;
	
cout <<"Please enter your time, ex 12 45 "<<endl<<endl;
	
cin>>hrs>>min;
	
cout <<hrs<<" Hours" << setw(3) <<min<<" Minutes" <<endl<<endl;


At a glance it appears you are trying to access uninitialised variables.

The variable totaltime is set without initialising with the cin first. Can you see it?

Try moving the input before setting the variable first.

1
2
3
4
5
6
7
8
 int hrs, min;
    double totaltime;
	
    cout <<"Please enter your time, ex 12 45 "<<endl<<endl;
	
    cin>>hrs>>min;

    totaltime=(hrs+min)%60;
@Sublyminal

My problem is getting the modulus stuff right incase say someone puts 12 75, so that instead of it saying 12.75 hrs it instead rolls over to say 13.15 hrs.


Quite simple, actually. Have an if statement right after your input of hrs and min. Check if min is greater than or equal to, 60. If yes, decrease min by 60 and add 1 to hrs.

As an aside, the totaltime variable is just using garbage values at the start of your program, as it takes the values from uninitiated hrs and min. Put that line AFTER the final input and if checking.

totaltime=(hrs+min)%60;


Not sure what this was meant to show, or represent. Could you explain?
Last edited on
@whitenite1


We can't use IF statements just yet, not allowed to. (professors orders) but, thank you for the suggestion.


@megatron

I have tried that as well.

Would it be best to define the time as something like
1
2
total=hrs/(24*60);
min=hrs%60;



I'm actually pretty sure the above wouldn't work, but am I on the right path?

Sigh.. I am with whitenite on this one, it's something that could be done in a single line with an if statement, but I guess your professor either wants you think abstractly, or just teach you to do things in silly ways. :/

Try converting the hours to minutes first, then adding the minutes. Proceed to divide by 60 to get the amount of hours, then use the modulus to get the fraction of minutes?

EDIT: I think this works

1
2
3
4
5
6
7
8
int hrs, mins;
double total, totalmins;

std::cin >> hrs;
std::cin >> mins;

totalmins = ((hrs*60)+mins); // total number of minutes
total = totalmins/60;
Last edited on
No if statements needed.
1
2
3
cin>>hrs>>min;
hrs += (min / 60);
min %= 60;
@megatron

I agree, it's silly to do it in such a manner, especially when one little IF statement is all it would take to make this nightmare go away. I'll try what you suggested when I get back from class.

Quick question as again, my math isn't the best in the world to convert from hours to minutes, I'd just type in.

min=(24*60); correct? or would it be easier to define another int called min1 and then do it that way?

Thanks again for the suggestions, they're helping a bunch.
I have given you the solution. What was I thinking! You just had to convert the hours to minutes and divide... I think.

Ignore that, the . threw me off I thought you wanted a fraction. I need some sleep.
Last edited on
You're asking the user to enter hours and minutes. If the users enters 75 for minutes, is that a valid value for minutes? I would say no. You should be editing the value to ensure that it is a valid value between 0 and 59.
1
2
3
4
5
6
7
  do 
  { cout << "Enter minutes: ";
     cin >> min;
     if (min < 0 || min > 59)
       cout << "Not a valid value" << endl;
  } while (min < 0 || min > 59);
  //  Now we have a value for minutes between 0 and 59 

Last edited on
@Sublyminal

Hopefully, you can use the while statement, then.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    cin>>hrs>>min;
while(min>=60)// If min is over, or equal to, 60. 120 min will increase hrs by 2
{
hrs++; // Increase hrs 
min-=60;//Decrease min by 60
}

totaltime=(hrs*60)+min; // Get how many minutes are in the hrs and min, combined

    cout <<hrs<<" Hours" << setw(3) <<min<<" Minutes" <<endl<<endl;
	
	cout << setiosflags(ios::fixed)
             << setiosflags(ios::showpoint)
             << setprecision(0);
		 
    cout << totaltime << " minutes" <<endl;  // Show total amount of minutes from inputs
     
    system("PAUSE");
Okay, so I spoke with my professor, and I was going about it completely the wrong way. I figured it out, but, you guys have all helped me out tremendously. I appreciate each of the replies and each of the suggestions. Thanks again!

Gonna mark this as solved.


@megatron

You and me both need some sleep lol... note to self, never try to write a program at 3 in the morning.
Hey I write a filehandler at 5. ;) If you ever need other help don't be afraid to PM me.
Topic archived. No new replies allowed.