0

So I'm trying to put together this program for a project, and it's behaving strangely for me. Here's an example of what happens in the console:

Compute completion time from current time and waiting period

Current time:

Enter 24 hour time in the format HH:MM 12:50
Waiting time:
Enter 24 hour time in the format HH:MM Completion time in 24 hour format:
4644952:4198980
Enter Y or y to continue, any other halts
Process returned 0 (0x0) execution time : 3.927 s
Press any key to continue.

The part in bold is the only part I could input and then it just runs through the cout statements without letting me input the second pieces of data. Then outputs some garbage number from the calculation function. Here is my code:

#include <iostream> using namespace std; void getEndTime(int c_hours, int c_minutes, int w_hours, int w_minutes, int& e_hours, int& e_minutes); void getCurrentTime(int& c_hours, int& c_minutes); void getWaitingTime(int& w_hours, int& w_minutes); void runLoop(); int main() { char select; cout << "Compute completion time from current time and waiting period \n"; do { runLoop(); cout << "Enter Y or y to continue, any other halts"; cin >> select; } while (select == 'y' || select == 'Y'); return 0; } void runLoop() { int current_hours, current_minutes, waiting_hours, waiting_minutes; int end_hours, end_minutes; getCurrentTime(current_hours, current_minutes); getWaitingTime(waiting_hours, waiting_minutes); getEndTime(current_hours, current_minutes, waiting_hours, waiting_minutes, end_hours, end_minutes); cout << "Completion time in 24 hour format:\n" << end_hours << ":" << end_minutes << endl; } void getCurrentTime(int& c_hours, int& c_minutes) { cout << "Current time:\n" << "Enter 24 hour time in the format HH:MM "; cin >> c_hours >> c_minutes; } void getWaitingTime(int& w_hours, int& w_minutes) { cout << "Waiting time:\n" << "Enter 24 hour time in the format HH:MM "; cin >> w_hours >> w_minutes; } void getEndTime(int c_hours, int c_minutes, int w_hours, int w_minutes, int& e_hours, int& e_minutes) { if ((c_hours + w_hours) >= 24) { e_hours = (c_hours + w_hours - 24); } else { e_hours = (c_hours + w_hours); } if ((c_minutes + w_minutes) >= 60) { e_hours += 1; e_minutes = (c_minutes + w_minutes) - 60; } else { e_minutes = c_minutes + w_minutes; } return; } 

I am pretty new to this so I apologize if there is something obvious I am missing. But I'm hoping one of you can help me out here, I'm totally stumped as to why this is not working! Thanks!

5
  • 1
    I strongly suggest you (a) learn to use your platform's debugger, and (b) in the absence of (or even in addition to) a debugger, liberally sprinkle cerr << your-var-here << std::endl; throughout your code to ensure what you think is being read is, in fact, the case. Commented Jul 13, 2015 at 19:29
  • 1
    Reasonable testcase but this topic has been covered a bazillion times on SO. Please perform research before asking. Commented Jul 13, 2015 at 19:31
  • @WhozCraig Maybe I am missing something, my compiler gave no build errors. Is a debugger something different? Can you point me to some information on that subject, please? Commented Jul 13, 2015 at 19:35
  • @LightnessRacesinOrbit I did try looking for an answer, but it can be difficult as a beginner when you don't know the right question. Maybe you can help guide me to the right search phrases? Commented Jul 13, 2015 at 19:35
  • The compiler only generates the appropriate machine instructions for your code. If your code tells the computer to do something legal but not in line with your program's intended logic, the compiler's useless to you. A debugger allows you to run your code line by line making the appropriate branches at conditional expressions and function calls and inspect the changes to the variables as you go. This way you can see where your program went and what it did. If you are using a development environment to write your program, a debugger is typically built in. Commented Jul 13, 2015 at 20:34

1 Answer 1

2

The problem you are having is cin is entering an error state and every subsequent call to cin will automatically fail and the program will continue on. When you get the time in:

void getCurrentTime(int& c_hours, int& c_minutes) { cout << "Current time:\n" << "Enter 24 hour time in the format HH:MM "; cin >> c_hours >> c_minutes; } 

You are not eating the : that exist in 12:50. As such it tries to insert the : into minutes and fails.

What you need to do is call cin.get() to eat the : and then get the minutes.

void getCurrentTime(int& c_hours, int& c_minutes) { cout << "Current time:\n" << "Enter 24 hour time in the format HH:MM "; cin >> c_hours; cin.get(); // eats : cin >> c_minutes; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! For some reason I thought it would accept the : the same way it would a space. This is very helpful, thank you again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.