I am writing a program for class where it converts from military time to standard. Everything else seems to be working fine until I type in 60 for the minutes. For example if I type 23:60 it gives me 11:60 PM which is incorrect. How do I fix this? I tried checking if minutes == 60 to reset minutes to 0 but I cant figure it out.
#include <iostream>
using namespace std;
void inputData(int&, int&, char&); // this function asks users to input
information
void convertData(int&, int&, char&); // this converts the military time to
standard time
int outputData(int&, int&, char&); // this function puts all the other
information together to output certain data
int main ()
{
int hours, minutes;
char am_pm;
char trueValue;
do
{
inputData(hours, minutes, am_pm); // calls to input function
convertData(hours, minutes, am_pm); // calls to the conversion function
outputData(hours, minutes, am_pm); // calls to function that outputs all
the data
cout << "Would you like another conversion? Type Y or y to repeat." <<
endl;
cin >> trueValue;
}
while (trueValue == 'y'|| trueValue == 'Y');
if (trueValue != 'y' || trueValue != 'Y')
cout << "Thanks for using this converter. Have a nice day." << endl;
return 0;
}
void inputData (int &hours, int &minutes, char &am_pm)
{
cout << "Please enter hours (less than or equal to 24): "; // ask user to
input hours.
do
{
cin >> hours;
if (hours > 24)
cout << "ERROR! Must be less than 24" << endl;
}
while (hours > 24); // end of hours loop
cout << "Please enter minutes (less than or equal to 60): "; // ask user to
input minutes.
do
{
cin >> minutes;
if (minutes > 60)
{
cout << "Must be less than 60. Try again!" << endl;
}
}
while (minutes > 60); //end of minutes loop
cout << endl;
cout << "You have entered: " << hours << ":" << minutes; // display what
user inputs together.
cout << endl;
}
void convertData(int &hours, int &minutes, char &am_pm)
{
if (minutes == 60)
{
hours++; // add an hour to 'hours'
minutes = minutes/60;
}
if (hours < 12)
{
hours = 12-12+1;
}
if (hours > 12)
{
hours = hours - 12; // subtracts anything bigger than 12 to get standard
time. Anything over 12 is PM according to project instruction
am_pm = 'P';
}
else
if (hours == 12)
{
am_pm = 'P';
}
else
am_pm = 'A';
}
int outputData(int &hours, int &minutes, char &am_pm)
{
if (am_pm == 'P')
cout <<"Your standard time is: " << hours << ":" << minutes << " P.M" <<
endl;
else
cout <<"Your standard time is: " << hours << ":" << minutes << " A.M" <<
endl;
}
There's a couple of places you test for minutes > 60. Try minutes >= 60 instead.
Same with hours > 24.
Your if statement for minutes being > 60. Change it to >= 60. Currently your if statement is accepting 60 as a valid condition for minutes.
Related
I trying to develop a program that displays a 12 hour and 24 hour clock at the same time. But whenever I compile, I get a build error saying 'GetAM_PM': identifier not found. I get this error on line 26 in spite of using the same variable from my function parameter. What could be the root of this problem? Here is my code:
#include <iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
//converting it into 12 hour format
int TwelveHourFormat(int twelve_hours) {
return (twelve_hours == 0 || twelve_hours == 12) ? 12 :
twelve_hours % 12;
}
//printing the 12 hour format
void Display_12_HourFormat(int seconds, int minutes, int
twelve_hours) {
cout.fill('0');
cout << TwelveHourFormat(twelve_hours) << ":" << minutes << ":"
<< seconds << " " << GetAM_PM(twelve_hours);
}
//printing the 24 hour format
void Display_24_HourFormat(int seconds, int minutes, int
twenty_four_hours) {
cout.fill('0');
cout << twenty_four_hours << ":" << minutes << ":" << seconds;
}
void AddHour(int hour) {
hour = (hour + 1) % 24;
}
void AddMinute(int hour, int min) {
if (min == 59) {
AddHour(hour);
}
min = (min + 1) % 60;
}
void AddSecond(int min, int sec) {
if (sec == 59) {
AddMinute(min, sec);
}
sec = (sec + 1) % 60;
}
// function return AM/PM respect to hour of time
string GetAM_PM(int twelve_hours) {
return twelve_hours >= 12 ? "PM" : "AM";
}
// This method prints the menu options
void DisplayMenu() {
cout << "Chada Tech Clocks Menu" << endl;
cout << "[1] Add one hour" << endl;
cout << "[2] Add one minute" << endl;
cout << "[3] Add one second" << endl;
cout << "[4] Exit program" << endl;
}
int main()
{
int seconds, minutes, hours;
//obtains current time in seconds
time_t total_seconds = time(0);
//getting values of seconds, minutes and hours
struct tm ct;
localtime_s(&ct, &total_seconds);
seconds = ct.tm_sec;
minutes = ct.tm_min;
hours = ct.tm_hour;
// Variable declared
int option;
do
{
// DisplayMenu function is called
DisplayMenu();
cin >> option;
// If user input is 1, Clock function is called
if (option == 1) {
TwelveHourFormat(hours);
AddHour(hours);
GetAM_PM(hours);
Display_12_HourFormat(seconds, minutes, hours);
Display_24_HourFormat(seconds, minutes, hours);
}
// If the option is 2, the Clock function is called
else if (option == 2) {
AddMinute(minutes, seconds);
GetAM_PM(hours);
}
// If the option is 3, the Clock function is called
else if (option == 3) {
AddSecond(minutes, seconds);
GetAM_PM(hours);
}
// If the option is 4, exit message prints and application
stops running
else if (option == 4) {
cout << "You have exited the application.";
break;
}
else {
cout << "You have entered an invalid input." << endl;
}
} while (option != 4);
}
You need to declare GetAM_PM before you define Display_12_HourFormat. Otherwise it'll not be known by the compiler at the time it parses the Display_12_HourFormat function in which GetAM_PM is called.
Just move these lines to the beginning, before any other function:
// function return AM/PM respect to hour of time
string GetAM_PM(int twelve_hours) {
return twelve_hours >= 12 ? "PM" : "AM";
}
It is not this case, but if you end up with circular dependency, try to declare the methods in a .h or forward declare the methods in the code.
First, let me get this out of the way. I know my program is a little rough right now, but I can't even get to fixing it if lines 42-52 (future time input) are being skipped. So, for context, I need to make a time machine that can go forward 24 hours, and I need to make sure that the input for the start and future time is in the HH:MM xm format. Then I need to have the total minute difference, which can convert to the hours and minutes until the future time. Sorry if what I've done gives you a headache still learning.
Here is my code:
#include <iostream>
// Total number of minutes in an hour and day
const int MINUTES_IN_HOUR = 60;
const int MINUTES_IN_DAY = 24 * MINUTES_IN_HOUR;
// Function that computes the difference between two times
int convertDifference(int startHoursPar, int startMinutsPar, bool startIsAMPar, int endHoursPar, int endMinutesPar, bool endIsAMPar);
int main()
{
using namespace std;
int startHours;
int startMinutes;
bool startIsAM;
int endHours;
int endMinutes;
bool endIsAM;
char amPmChar;
char extra; //IMPORTANT!!
char answer;
int diff;
do
{
cout << "Enter start time, in the format 'HH:MM xm', where xm is \n" << "either 'am' or 'pm' for AM or PM: ";
cin >> startHours >> extra >> startMinutes >> startIsAM;
if (startIsAM == 'a' || startIsAM == 'A')
{
startIsAM = true;
}
else
{
startIsAM = false;
}
cout << "\nEnter future time, in the format 'HH:MM xm', where xm is\n" << "either 'am' or 'pm' for AM or PM: ";
cin >> endHours >> extra >> endMinutes >> endIsAM;
if (endIsAM == 'a' || endIsAM == 'A')
{
endIsAM = true;
}
else
{
endIsAM = false;
}
diff = convertDifference(startHours, startMinutes, startIsAM, endHours, endMinutes, endIsAM);
/*if ((hours > 1) && (minutes > 1))
{
cout << "There are " << diff << " minuets " << "(" << hours << " hours and " << minutes << " minutes) between " << startHours << ":" << startMinutes << " and " << endHours << ":" << endMinutes << ".";
}
else
{
cout << "There are " << diff << " minuets " << "(" << hours << " hour and " << minutes << " minute) between " << startHours << ":" << startMinutes << " and " << endHours << ":" << endMinutes << ".";
}
*/
cout << endl << "\nRun again? (y / n): ";
cin >> answer;
} while (answer == 'y');
return 0;
}
int convertDifference(int startHoursPar, int startMinutsPar, bool startIsAMPar, int endHoursPar, int endMinutesPar, bool endIsAMPar)
{
int diff, hours, minutes, minutes2;
if (startIsAMPar == 'p')
{
if (startHoursPar >= 1 && startHoursPar < 12)
{
startHoursPar += 12;
}
}
if (endIsAMPar == 'p')
{
if ((startHoursPar >= 1) && (endHoursPar < 12))
{
endHoursPar += 12;
}
}
minutes = (startHoursPar * 60) + startMinutsPar;
minutes2 = (endHoursPar * 60) + endMinutesPar;
if ((startHoursPar >= endHoursPar) || ((startHoursPar == endHoursPar) && (startMinutsPar > endMinutesPar)))
{
endMinutesPar += MINUTES_IN_DAY;
}
diff = endMinutesPar - startMinutsPar;
if (diff > MINUTES_IN_DAY)
{
diff -= MINUTES_IN_DAY;
}
return diff;
}
so basically i am lost and i don't know where to go the original question says:
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following function: a function to convert the time from 24-hour notation to 12-hour notation, a function to convert the time from 12-hour notation to 24-hour notation, a function to display the choices, function(s) to get the input, and function(s) to display the results. (For 12-hour time notation, your program must display AM or PM.)
i figured out how to make it work but using less function than specified .. how can i make a separate function that has the output ?
#include "stdafx.h"
#include <iostream>
using namespace std;
void choice();
void inputincaseof24to12(int hours, int minutes);
void inputincaseof12to24(int hours, int minutes, char ampm);
int main()
{
int hours = 1;
int minutes = 0;
char ampm = 0;
char yourchoice;
choice();
cin >> yourchoice;
if (yourchoice == 'A')
inputincaseof24to12(hours, minutes);
else if (yourchoice == 'B')
inputincaseof12to24(hours, minutes, ampm);
}
void choice() {
cout << "in case of conversion to 24 hours from 12 hours please enter 'A' " << endl;
cout << "in case of comversion to 12 hours from 24 hours please enter 'B' " << endl;
}
void inputincaseof24to12 (int hours, int minutes) {
cout << "Enter any number of Hours & Minutes to be converted from 24 hours Notation to 12 hours Notation " << endl;
cin >> hours;
cin >> minutes;
if (hours < 12)
cout << hours << " " << minutes << "A.M" << endl;
else if (hours == 12)
cout << hours << " " << minutes << "P.M" << endl;
else if (hours > 12)
cout << hours - 12 << " " << minutes << "P.M" << endl;
}
void inputincaseof12to24(int hours, int minutes, char ampm) {
cout << "Enter any number of Hours & Minutes while considring the AM/PM [where 'A' stands for AM and 'P' stands for PM] state to be converted from 12 hours Notation to 24 hours Notation " << endl;
cin >> hours;
cin >> minutes;
cin >> ampm;
if (ampm == 'A' && hours <= 11)
cout << hours << ":" << minutes;
else if (ampm == 'P' && hours == 12)
cout << 12 << ":" << minutes;
else if (ampm == 'P' && hours > 12);
cout << hours + 12 << ":" << minutes;
}
please do note i am still a noob in programing so go easy on me :) thank you
You need to ask a specific question, nobody here is going to do this assignment for you. I do have some general advice to hopefully help guide you towards the answer.
Sanitize your inputs. When the user inputs their time values, make sure that they are actually numbers within the range you are looking for. It's nice that you have a std::cout message requesting as such, but that's not going to make sure your users actually obey the rules.
Consolidate your I/O operations. You've got calls to std::cin inside your functions that are already passing these arguments, and in fact the way you're doing it they are not passed by reference i.e. int &hours so even if the user puts usable values in they will go out of scope when your function returns.
Think about the logic used to convert 12 and 24 hour time formats. See this diagram for reference. You will see why your current implementation logic for conversionincaseB is not quite right.
You should come up with more descriptive names for your functions. Nobody knows what conversionincaseB is supposed to do, but they might be able to infer what a more descriptive function name like convert24HourTime is supposed to do.
I am trying to learn C++ and have an issue with a small conversion program about time formats.
#include <iostream>
using namespace std;
void conversion(int hours, int minutes) {
if (hours > 12) {
hours -= 12;
minutes = minutes;
}
else
hours = hours;
minutes = minutes;
cout << hours << ":" << minutes << endl;
}
void output(int hours, int minutes) {
cout << hours << ":" << minutes;
}
int main() {
int hours, minutes;
cout << "Enter the hours: ";
cin >> hours;
cout << "Enter minutes: ";
cin >> minutes;
conversion(hours, minutes);
output(hours, minutes);
}
In the main function, the output call is not receiving the updated values for hours and minutes.
This has nothing to do with time formats. A much smaller program that shows the same problem is:
#include <iostream>
using namespace std;
void f(int i) {
i = 20;
}
int main() {
int i = 10;
f(i);
cout << "i is still " << i << endl;
}
The problem is that int i in the parameter list means "pass by value". The function gets a copy of the value. Any changes you make to that copy are thrown away when the function exits. Either pass by reference (void f(int& i)) or by pointer (void f(int *pi)). Look up reference and pointers in your text book.
There are also a number of issues in your function:
void conversion(int hours, int minutes) {
if (hours > 12) {
hours -= 12;
minutes = minutes; // This line does nothing.
}
else
hours = hours; // This line does nothing.
minutes = minutes; // This line will get executed even if hours was originally
// >12. (Only the hours=hours line is covered by the else).
cout << hours << ":" << minutes << endl;
}
You probably wanted:
void conversion(int& hours, int& minutes) {
if (hours > 12) {
hours -= 12;
}
cout << hours << ":" << minutes << endl;
}
I have a problem with my program and I would appreciate help.
It's supposed to allow you to enter an account number, a service code, and the number of minutes the service was used. The program then calculates the bill and it varies depending on your service. When I execute the program, it doesn't allow you to enter anything.
Regular service:$10.00 plus first 50 minutes free. Charges for over 50 minutes are $0.20 per minute.
Premium service:
$25.00 plus:
a)
For calls made from 6:00 am to 6:00 pm, the first 75 minutes are free; charges for over
75 minutes are $0.10 per minute.
b)For calls made from 6:00 pm to 6:00 am, the first 100 minutes are free; charges for over 100 minutes are $0.05 per minute.
Here is the program that I've typed.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int minutes = 0,
day_minutes = 0, //minutes used during the day
night_minutes = 0; //minutes used during the night
string service_code,
account_number;
double final_amount,
final_damount, //final amount for day minutes
final_namount = 0; //final amount for night minutes
cout << "Please enter your account number: ";
cin >> account_number;
cout << "Please enter your service code (r or R for regular service and p or P for premium service): ";
cin >> service_code;
if (service_code == "r")
{
cout << "Please enter the amount of minutes used: " << minutes << endl;
}
if (minutes <= 50)
{
final_amount = 10;
cout << "Your final amount is $: " << final_amount << endl;
}
if (minutes > 50)
{
final_amount = (minutes * 0.20) + 10;
cout << "Your final amount is $: " << final_amount << endl;
}
else if (service_code == "p")
{
cout << "Please enter the amount of minutes used during the day: " << day_minutes << endl;
cout << "Please enter the amount of minutes used during the night: " << night_minutes << endl;
}
if (day_minutes <=75)
{
final_damount = 0;
final_amount = final_damount + final_namount + 20;
}
if (day_minutes > 75)
{
final_damount = day_minutes * 0.10;
final_amount = final_damount + final_namount + 20;
}
if (night_minutes <= 100)
{
final_namount = 0;
final_amount = final_damount + final_namount + 20;
}
if (night_minutes > 100)
{
final_namount = night_minutes * 0.05;
final_amount = final_damount + final_namount + 20;
cout << "Your final amount is: $ " << final_amount << endl;
}
else
cout << "Error, this program does not accept negative numbers.\n";
return 0;
}
Does anyone the problem to my program? Thank you.
In your code you never specify to ask for user input (read from any input streams or files), hence it doesn't ask for any input.
You will probably want to somewhere do something such as cin or cin.readline or any of several other various methods to read the user's input from stdin.
To avoid duplicating other questions I'm not putting further details here.