Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to ask the user to input a time and then add 12 to that time if P is

ID: 3621113 • Letter: I

Question

I am trying to ask the user to input a time and then add 12 to that time if P is the end char entered and the value of the time is < 12. The program is returning a time with 12 added no matter what char or value is entered. Can some one offer me some suggestions on how to fix this problem? Thanks!

cout << "Enter start time (hh:mm A/P): ";

float start_hour = 0;
float start_minute = 0;

char A = 0;
char P = 0;
char dummy(0);

cin >> start_hour >> dummy >> start_minute >> A || P;

if ( P && start_hour < 12);
{
start_hour = start_hour + 12;
}

Explanation / Answer

please rate - thanks

here is the solution, but it has other problems input must have spaces around the :

#include <iostream>
using namespace std;
int main()
{cout << "Enter start time (hh:mm A/P): ";

float start_hour = 0;
float start_minute = 0;

char A = 0;
char P = 0;
char dummy(0);

cin >> start_hour >> dummy >> start_minute >> A;
if ( A=='P' && start_hour < 12)        //you had a ;
{
start_hour = start_hour + 12;
}
cout<<start_hour<<endl;
system("pause");
return 0;
}

--------------------------------

here is what you really want     form of input is a space only before the A or P

#include <iostream>
using namespace std;
int main()
{cout << "Enter start time (hh:mm A/P): ";
string input;
int start_hour = 0;
int start_minute = 0;
int i,j;
char A;
getline(cin,input);
start_hour=0;
for(i=0;input[i]!=':';i++)
      start_hour=start_hour*10+(input[i]-'0');
start_minute=0;
for(j=i+1;isdigit(input[j]) ;j++)
      start_minute=start_minute*10+(input[j]-'0');
A=input[j+1];
if ( A=='P' && start_hour < 12)
{
start_hour = start_hour + 12;
}
cout<<start_hour<<" "<<start_minute<<" "<<A<<endl;
system("pause");
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote