why is the output always 1:00? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why is the output always 1:00?

#include <iostream> using namespace std; void input (int hour, int min, char mode); void convert (int hour, int min, char mode); void output (int hour, int min, char mode); int main(){ int hour, min; char mode; input (hour, min, mode); convert (hour, min, mode); output (hour, min, mode); return 0; } void input (int hour, int min, char mode){ do{ cout << "Enter hour/s: " << endl; cin >> hour; cout << "Enter minute/s: " << endl; cin >> min; cout << "Enter A or P: " << endl; cin >> mode; if ((hour<1 && hour>12)||(min<0 && min>59)||(mode!='A' && mode!='P')){ cout << "Invalid inputs. Try again." << endl; continue; } break; } while (1); } void convert (int hour, int min, char mode){ switch (mode){ case 'P': hour = hour + 12; break; case 'A': if (hour==12){ hour = 00; } else{ hour = hour; break; } } } void output (int hour, int min, char mode){ if (min<=9){ cout << "24-hr Time Mode: " << hour << ":0" << min; } else{ cout << "24-hr Time Mode: " << hour << ":" <<min; } }

24th Mar 2019, 8:19 AM
Gail Tafalla
Gail Tafalla - avatar
1 Answer
+ 4
You will want to pass your variables to input() function by reference, so that the modified values will be reflected in the variables local to main. void input(int& hour, int& min, char& mode); Edit both your function header and function prototype.
24th Mar 2019, 8:46 AM
Hatsy Rei
Hatsy Rei - avatar