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;
}
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.
So we're assigned at school to create a class time. They want us to seperate the class with a time.h header file, a time.cpp cpp file and a main.cpp cpp file. I've got the following code, but for some reason I keep getting an "undeclared identifier" error. All 3 files are included in my project right now.
Here is the code:
time.h
class time
{
private:
int hours;
int minutes;
int seconds;
public:
time();
time(int sec);
time(int h, int min, int sec);
int getTime();
void setHours(int h);
void setMinutes(int min);
void setSeconds(int sec);
bool equals(time t);
void addTime(time t);
void printTime();
void normalize();
};
time.cpp
#include "time.h"
#include <iostream>
#include <iomanip>
using namespace std;
time::time()
{}
time::time(int sec)
{}
time::time(int h, int min, int sec)
{}
int time::getTime()
{
return (hours * 60 * 60) + (minutes * 60) + seconds;
}
void time::setHours(int h)
{
hours = h;
}
void time::setMinutes(int min)
{
minutes = min;
}
void time::setSeconds(int sec)
{
seconds = sec;
}
bool time::equals(time t)
{
if (hours == t.hours && minutes == t.minutes && seconds == t.seconds)
return true;
else return false;
}
void time::addTime(time t)
{
hours += t.hours;
minutes += t.minutes;
seconds += t.seconds;
}
void time::printTime()
{
cout << setfill('0') << setw(2) << hours
<< ":" << setfill('0') << setw(2) << minutes
<< ":" << setfill('0') << setw(2) << seconds;
}
void time::normalize()
{
seconds %= 60;
minutes = minutes + (seconds / 60);
hours = hours + (minutes / 60);
minutes = minutes % 60;
}
main.cpp
#include "time.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
time time1;
int seconds1;
cout << "Enter the amount of seconds: ";
cin >> seconds1;
time time2(seconds1);
int hours2, minutes2, seconds2;
cout << "Enter the amount of hours: ";
cin >> hours2;
cout << "Enter the amount of muinutes: ";
cin >> minutes2;
cout << "Enter the amount of seconds: ";
cin >> seconds2;
time time3(hours2, minutes2, seconds2);
time1.equals(time2);
}
Compiling the given code I receive a whole raft of error messages, the most telling being
warning: statement is a reference, not call, to function 'time'
time time1;
^
This leads to time1 being reported as not declared later because it isn't.
The Standard library contains a header time.h and a function time. To ensure the program included the correct time.h and used the correct time, I renamed the header to mytime.h and the time class to mytime. All errors went away (a few warnings about unused parameters remain) as soon as the potential for ambiguity was removed.
I recommend using something a little less banal than mytime, but feel free to use whatever you wish so long as the name is descriptive and there are no more collisions.
I'm working on my Intro to C++ homework right now, and just got stuck on this last bit and would really appreciate some help.
The directions for the homework are:
Ensure that the hour value is in the range 0 – 23; if it is not, set the hour to 1.
Ensure that the minute value is in the range 0 – 59; if it is not, set the minute to 0.
Ensure that the second value is in the range 0 – 59; if it is not, set the second to 0.
Provide a set function for each data member to validate the input going into the data member, using the values above.
Also, provide a get function for each data member to retrieve its value.
Provide a member function displayTime() that displays the hour, minute and second, each separated by a colon (Example: 3:45:29). displayTime should use the get functions to retrieve the data in the data members.
Write a test program that demonstrates class Time’s capabilities as follows:
1.Prompt for hour, minute and second.
2.Create a Time object passing the values entered in response to the prompt(s) above.
3.Call displayTime to display the “Initial Time”.
4.Prompt again for hour, minute and second, and call the set methods for each of the 3 data members.
5.Call displayTime again to display the “Modified Time”.
My current code for my project beginning with .cpp file:
#include <iostream>
using namespace std;
class Time {
public:
//Time constructor
Time(int hour, int minute, int second)
{
setTime(hour, minute, second);
}
void setTime(int input_hour, int input_minute, int input_second)
{
setHour(input_hour);
setMinute(input_minute);
setSecond(input_second);
}
//set hour function
void setHour(int input_hour)
{
if (input_hour >= 0 && input_hour < 24)
{
hour = input_hour;
}
else
hour = 1;
}
//set minute function
void setMinute(int input_minute)
{
if (input_minute >= 0 && input_minute < 60)
{
minute = input_minute;
}
else
minute = 0;
}
//set second function
void setSecond(int input_second)
{
if (input_second >= 0 && input_second < 60)
{
second = input_second;
}
else
second = 0;
}
//get functions
int getHour()
{
return hour;
}
int getMinute()
{
return minute;
}
int getSecond()
{
return second;
}
// display function
void displayTime()
{
cout << "Time is " << hour << ":" << minute << ":" << second;
}
//private data members
private:
int hour;
int minute;
int second;
};
Now the .h file:
#include <iostream>
#include "Time.h"
using namespace std;
int main()
{
int hour, minute, second;
cout << "Enter the hour: ";
cin >> hour;
cout << "Enter the minute: ";
cin >> minute;
cout << "Enter the second: ";
cin >> second;
Time printTime{ hour, minute, second };
cout << "Time is " << printTime.getHour() << ":" << printTime.getMinute() << ":" << printTime.getSecond();
cout << "\n\nEnter the hour: ";
cin >> hour;
cout << "Enter the minute: ";
cin >> minute;
cout << "Enter the second: ";
cin >> second;
cout << "Time is " << setTime();
//the two lines to keep my debugger from crashing
std::cin.ignore();
std::cin.get();
}
I've gotten through 1-3 fine, but its steps 4 and 5 thats throwing me off. I'm not entirely sure what I'm supposed to be doing here. I understand what is supposed to happen. It's supposed to ask the user again to input data, and then its supposed to spit out 1:0:0 which is the "modified time" via the set functions (I think), but I'm not sure how to code it properly. I've got a feeling its something very simple, but again, I'm not sure what to do. "The third to the last line of cout << "Time is " << setTime(); obviously doesn't work. This is the first time I've ever learned how to program, so I'm not entirely sure what to do. Anyway, thank you for any and all help.
From what I can understand, you just have to change the time using the set functions you have previously created. Something like this: (untested)
EDIT: there is printTime.displayTime();use it!
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
int hour, minute, second;
cout << "Enter the hour: ";
cin >> hour;
cout << "Enter the minute: ";
cin >> minute;
cout << "Enter the second: ";
cin >> second;
Time printTime{ hour, minute, second };
printTime.displayTime();
cout << "\n\nEnter the hour: ";
cin >> hour;
//Change the hour, minutes, and seconds - then display again
printTime.setHour(hour);
cout << "Enter the minute: ";
cin >> minute;
printTime.setMinute(hour);
cout << "Enter the second: ";
cin >> second;
printTime.setSecond(hour);
//Modified time
printTime.displayTime();
}
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.
I'm trying to figure out how to get both military and standard time to display.
A User inputs the time, then its shown in its standard form.
It's mostly there, but the standard time portion is giving me a few issues.
#ifndef TIME_H
#define TIME_H
class Time
{
public:
Time(int = 0, int = 0, int = 0);
~Time();
int hour; // valid values are 0 to 23
int minute; // valid values are 0 to 59
int second; // valid values are 0 to 59
void setTime(int, int, int); // function that checks if inputs are valid
void printUniversal(); // prints in HH:MM:SS format
void printStandard(); // prints in HH:MM:SS AM/PM format
static int count; //counter
};
#endif
#include "stdafx.h"
#include "Time.h" //header file that contains the Time class file
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int Time::count = 12;
Time::Time(int hr, int min, int sec)
{
hour = hr; minute = min; second = sec;
count++;
}
Time::~Time()
{
count--;
}
void Time::setTime(int hr, int min, int sec)
{
hour = (hr >= 0 && hr < 24) ? hr : 0; // checks if hour input is valid
minute = (min >= 0 && min < 60) ? min : 0; // checks if minute input is valid
second = (sec >= 0 && sec < 60) ? sec : 0; // checks if seconds input is valid
}
void Time :: printUniversal()
{
cout << setfill('0') << setw(2) << hour << ":" << setw(2) << minute << ":" << setw(2) << second;
}
void Time::printStandard()
{
cout << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":" << setfill('0') << setw(2) << minute << ":" << setw(2) << second << (hour < 12 ? " AM" : " PM");
}
//(Where I implement the functions - main.cpp)
#include "Time.h"
#include <iostream>
using namespace std;
int main()
{
int hour, minute, second;
Time t; //t is the time object //(PROBLEM!!!-Dont understand how to write the correct parameters)
//test is also a time object //(PROBLEM!!!-Dont understand how to write the correct parameters)
//Time *tp = new Time;
//Time *tarray = new Time[5];
cout << "Enter hour in military time ";
cin >> hour;
cout << "Enter minute ";
cin >> minute;
cout << "Enter second ";
cin >> second;
cout << "\nThe standard time is ";
t.printStandard(); //(PROBLEM!!!- I have the number 12 appearing right after AM and i can't get rid of it. )
cout << "\nThe universal time is ";
t.printUniversal();
cout << endl;
return 0;
} // end main
Update: The errors are gone for the moment, thanks to the suggestions posted.
However now when I run it, I get this....well since I can't post images yet...
I'll input 13, 45, 05 and almost like a old VCR, I can't 12:00:00 for standard or 00:00:00 for universal
No matter what I input into its all the same output.
Time t(); // This declares a function named t which takes
// no arguments and returns a Time object
Time test(); // This declares a function named test which
// takes no arguments and returns a Time object
Time t; // this is a default constructed Time object named t
Time t( 12, 7, 5 ); // this is a Time object with all
// parameters passed to the constructor
In printStandard, you have
<< sizeof(Time)
This is where the 12 is coming from. Objects of your Time class type take 12 bytes in memory.
I strongly suggest you get a book on C++, as all of this would be explained in there.