Hey,
I'm still fairly new to C++, and even more new to this forum. Any help would be greatly appreciated. Could someone look at this segment of code for me and tell me what is wrong with the "if" statements to change the time. I'm not getting the output that I want; though, it is pretty close.

void ChangeTimeToMil(int hours, int minutes, string AMPM, int& time)
{
if((AMPM == "AM" || AMPM == "am")&& hours == 12)
{
time = minutes;
cout << "  " << setfill('0') << setw(4) << time;
}
else if((AMPM == "AM" || AMPM == "am") && (hours >= 0 && hours <= 9))
{
time = (hours*100) + minutes;
cout << "  " << setfill('0') << setw(4) << time;
}
else if((AMPM == "AM" || AMPM == "am") && (hours >= 10 || hours <= 11))
{
time = (hours*100) + minutes;
}
else if(AMPM == "PM" || AMPM == "pm" && (hours > 12))
{
time = (hours + 12)*100 + minutes;
cout << "  " << setfill('0') << setw(4) << time;
}
else if((AMPM == "PM" || AMPM == "pm") && (hours == 12))
{
time = 1200 + minutes;
cout << "  " << setfill('0') << setw(4) << time;
}
}

I'm sure it's something quite simple, but I can't find it.

Thanks again,

Can you provide an input to this function that produces an incorrect result?

Also include what the correct result should be.
[/edit]

else if((AMPM == "AM" || AMPM == "am") && (hours >= 10 || hours <= 11))

the Boolean operator should be AND not OR, same as you did in the 0 to 9am block.
or you could simple test for equality to 10 …

All 5 Replies

Can you provide an input to this function that produces an incorrect result?

Also include what the correct result should be.
[/edit]

Input would be "10:00AM"

and I get "1"
also when the input is "11:59AM"
I get nothing at all

The rest works fine...

Thanks

else if((AMPM == "AM" || AMPM == "am") && (hours >= 10 || hours <= 11))

the Boolean operator should be AND not OR, same as you did in the 0 to 9am block.
or you could simple test for equality to 10 OR equality to 11

else if((AMPM == "AM" || AMPM == "am") && (hours >= 10 && hours <= 11))
//or
else if((AMPM == "AM" || AMPM == "am") && (hours == 10 || hours == 11))

I'm not seeing how the erroneous outputs you just described occur. Are you sure they come from the code posted?
Val

What's the actual input to the function? hours and minutes are integers, yet when I pass 10 and 0 with "AM" as the third parameter, it works just fine. 11 and 59 with "AM" works as well. Though you have a lot of redundancy and unnecessary code. Try this:

void ChangeTimeToMil(int hours, int minutes, string AMPM, int& time)
{
// Normalize the string so we don't have to test combinations
for ( string::size_type i = 0; i < AMPM.size(); i++ )
AMPM[i] = toupper ( (unsigned char)AMPM[i] );

if ( AMPM == "AM" && hours == 12 )
hours = 0;
else if ( AMPM == "PM" )
hours += 12;

time = hours * 100 + minutes;
}

Of course, you might also want to validate the input to make sure that it's not already in military time and act accordingly.

Thanks for the help. This is for a project in my c++ class that I turned in this past Friday night. It was just bugging me that I couldn't get the time to output correctly. The time that is input is ALWAYS in 12 hour time, it is read from a text file.

Thanks for all the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.