Identifier not found when calling a function under a function - c++

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.

Related

24 hour clock to standard

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.

24HR to 12HR convert program, new values not recognized in output?

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;
}

How to get both military and standard time to display?

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.

Displaying Issue Involving Time

I am having trouble displaying "next day" in the correctly in my program. The assignment states, ".The user is prompted to enter the hour, min, second of the current processing time - currTime. Your program will compute the next processing time which is 30 mins after the current processing time. Your program will display the current processing timetime currTime and the next processing time 1 nextTime1 following processing time 2 nextTime2. If the next processing time goes to the next day then
you display “Next Day” next to the next processing time (see sample output).
The sample output shows:
Current Proc Time = 23:50:10 | Next Proc Time = 0:20:10 (Next Day) | Next Proc Time 2 = 0:50:10
My problem occurs during the bold line "Next Proc Time 2 = 0:50:10," in my code it displays (Next Day) in which it should not, only for the previous line, "Next Proc Time (Next Day" should it be displayed. So my question is what should I do to alter my code under the Void TimeType::Display() function to have it display "Next Day" when the day is equal to one correctly. Also, I apologize if my question is not too specific, I am just at a loss and do not know what to do to fix this issue.
#ifndef TIME_H
#define TIME_H
class TimeType { //class definition
private:
int sec;
int min;
int hr;
int day; // variable stores the information about the day, you need to track the transition to the next day
public:
TimeType();
void SetTime(int s, int m, int h, int d);
TimeType NextProcTime(void);
void Display();
};
#endif
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "timetype.h"
using namespace std;
TimeType::TimeType()
{
sec = 0;
min = 0;
hr = 0;
}
void TimeType::SetTime(int s, int m, int h, int d)
{
sec = s;
min = m;
hr = h;
day = d;
}
TimeType TimeType::NextProcTime(void)
{
long buf = 0;
int h, m, s, d;
buf = (day * 86400 + hr * 3600 + min * 60 + sec) + (30 * 60); //calculation time in seconds needed for the allocation of time to reflect additional minutes
d = buf / 86400; // recalculation time for days hours minutes seconds
h = (buf - 86400 * d) / 3600;
m = (buf - 86400 * d - 3600 * h) / 60;
s = (buf - 86400 * d - 3600 * h - 60 * m);
TimeType T; //creating a buffer TimeType object
T.SetTime(s, m, h, d); //full a buffer TimeType object
return T; //return buffer TimeType object
}
void TimeType::Display()
{
if (day == 1)
{
printf(" %02i:%02i:%02i (next day)\t", hr, min, sec);
}
else {
printf(" %02i:%02i:%02i \t", hr, min, sec);
}
};
int main(int argc, char *argv[])
{
int hr = 0, min = 0, sec = 0;
TimeType currTime, nextTime1, nextTime2;
char t;
do
{
system("cls");
while (1) {
cout << "Please enter the current processing hour." << endl;
cin >> hr;
if (hr >= 0 && hr < 24)
break;
cout << "Invalid Input, try again." << endl << endl;
}
cout << endl;
while (1)
{
cout << "Please enter the current processing minute." << endl;
cin >> min;
if (min >= 0 && min < 60)
break;
cout << "Invalid Input, try again." << endl << endl;
}
cout << endl;
while (1)
{
cout << "Please enter the current processing second." << endl;
cin >> sec;
if (sec >= 0 && sec < 60)
break;
cout << "Invalid Input, try again." << endl << endl;
}
cout << endl;
currTime.SetTime(sec, min, hr, 0);
nextTime1 = currTime.NextProcTime();
nextTime2 = nextTime1.NextProcTime();
cout << "Current Proc Time = ";
currTime.Display();
cout << endl;
cout << "Next Proc Time 1 = ";
nextTime1.Display();
cout << endl;
cout << "Next Proc Time 2 = ";
nextTime2.Display();
cout << endl;
cout << endl << "Do you have more data to enter? (y/n)" << endl;
cin >> t;
cout << endl;
} while (t == 'y'); // cycle for program multiple using
return 0;
}

C++ Calling Member Function from a different class member function

I am writing a simple program to simulate a watch. Class Date, Class Time, Class Watch. Watch is derived from Date and Time. Trying to call a date member function from Time member function. It says I cant do it without an object. I am learning and I am sure most of this is crap haha. Any help would be appriciated. Basically at the end of Time::Tick, I am trying to call Class Date member function inDay. Calling one member function from another class member function. I dont know how to do a friend function from another class. I tried to make my functions static but it leads me down a long road of errors. Any advice?
#include <iostream>
#include <iomanip>
#include <windows.h>
using namespace std;
class Date
{
public:
Date();
void inDay();;// increments day, tests if month increments, if true, increment
void inMonth(); // increments month, tests if year increments, if true, increment
void inYear(); // increment year
void displayWatch(); // calls daysInMonthSwith, calls inDay, Display date
int daysInMonthSwitch(); // determines the number of days in the month based on which month using a switch
//void testPrint(); //Testing purposes only
void setDay();
void setMonth();
void setYear();
private:
int day;
int month;
int year;
int daysInMonth;
};
class Time{
public:
Time();
void setTime(int, int, int ); // set hours, minutes, seconds
void printStandard(); // print time in standard time format 12h
void setHour();
void setMin();
void setSec();
void tick();
private:
int hour;
int minute;
int second;
};
class Watch: public Time, public Date {
public:
Watch();
int getDate();
void setTime(int, int, int);
int getTime();
private:
};
//class Time::
Date::Date() : // Class Constructor
day(27),
month(2),
year(2013),
daysInMonth(31)
{
} // End Constructor
void Date::setDay()
{
int tempDay;
cout << "There are " << daysInMonth << " in the current month. Please enter a day between 1 and " << daysInMonth << ": ";
cin >> tempDay;
while (tempDay < 1 || tempDay > daysInMonth)
{
cout << "You entered a day outside the range." << endl;
cout << "There are " << daysInMonth << " in the current month. Please enter a day between 1 and " << daysInMonth << ": ";
cin >> tempDay;
}
day = tempDay;
}
void Date::setMonth()
{
int tempMonth;
cout << "There are 12 in the year. Please enter a day between 1 and 12: ";
cin >> tempMonth;
while (tempMonth < 1 || tempMonth > 12)
{
cout << "You entered a month outside the range." << endl;
cout << "There are 12 in the year. Please enter a day between 1 and 12: ";
cin >> tempMonth;
}
month = tempMonth;
}
void Date::setYear()
{
int tempYear;
cout << "Please enter a year in the full number(Correct: 2005, Incorrect: 05): ";
cin >> tempYear;
year = tempYear;
}
void Date::displayWatch()
{
daysInMonthSwitch();
cout << "Date: " << month << "/" << day << "/" << year << endl;
//testPrint(); // Prints number of days in current month for testing only
//cout << "Month: " << month << endl; // Prints month for testing purposes
//inDay();
}
////////////// inDay function increments day and if conditions are met, increments month and year
void Date::inDay() //increments month and if day is more greater than number of days in month, call inMonth function
{
day++;
if (day > daysInMonth)
{
inMonth();
day = 1;
}
}// end function
void Date::inMonth() // increment month and if month is more greater than or equal to 12, call inYear function
{
month++;
if (month >= 12)
{
Date::inYear();
}
} // end function
void Date::inYear() // increment year
{
year++;
month = 1;
} // end function
//void Date::testPrint()
//{
// cout << "Days in month: " << daysInMonth << endl;
//} // for testing purposes only
int Date::daysInMonthSwitch() // Function contains switch that determines the number of days in the current month
{
//int month = m;
int result;
switch(month)
{
case 1:
result = 31;
break;
case 2:
result = 28;
break;
case 3:
result = 31;
break;
case 4:
result = 30;
break;
case 5:
result = 31;
break;
case 6:
result = 30;
break;
case 7:
result = 31;
break;
case 8:
result = 31;
break;
case 9:
result = 30;
break;
case 10:
result = 31;
break;
case 11:
result = 30;
break;
case 12:
result = 31;
break;
default :
cout << "Invalid Month" << endl;
}
daysInMonth = result;
return daysInMonth;
} // end daysInMonthSwitch function
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Time::Time()
{
const time_t currentTime = time(0);
const tm *localTime = localtime( &currentTime );
setTime(localTime->tm_hour, localTime->tm_min, localTime->tm_sec);
}
void Time::setHour()
{
int h;
cout << "Please enter the hour: ";
cin >> h;
hour = ( h >= 0 && h <= 24 ) ? h : 0;
}
void Time::setMin()
{
int m;
cout << "Please enter the minute: ";
cin >> m;
minute = ( m >= 0 && m <= 60 ) ? m : 0;
}
void Time::setSec()
{
int s;
cout << "Please enter the second: ";
cin >> s;
second = ( s >= 0 && s <= 60 ) ? s : 0;
}
void Time::setTime( int h, int m, int s )
{ // validating time time, if incorrent, set to 0
hour = ( h >= 0 && h <= 24 ) ? h : 0;
minute = ( m >= 0 && m <= 60 ) ? m : 0;
second = ( s >= 0 && s <= 60 ) ? s : 0;
}
///void Time::tick();
void Time::printStandard()
{
cout << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":" <<
setfill('0') << setw(2) << minute << ":" << setw(2) << second
<< ( hour < 12 ? " AM" : " PM");
}
void Time::tick()
{
while (second >=0 && second <=60)
{
if (second < 59)
{
Sleep(1000);
second++;
}
else
{
Sleep(1000);
minute++;
second = 0;
if (minute >= 60)
{
hour++;
minute = 0;
if (hour >= 24)
hour = 0;
inDay();
}
}
}
};
///////////////////////////////////////////////////////////////////
Watch::Watch()
{}
////////////////////////////////////////////////////////////////// End Member Functions
int main()
{
Watch watch1;
watch1.displayWatch();
watch1.printStandard();
system("pause");
watch1.setDay();
watch1.displayWatch();
watch1.printStandard();
system("pause");
watch1.inDay();
watch1.displayWatch();
watch1.printStandard();
system("pause");
return 0;
} // end main
Your class Time does not descend from Date. It can't call methods on classes that might also happen to be parents of child classes. Imagine if you instantiated a Time alone. What Date would it operate on?
A method that increments Time until it rolls over and then increments Date would need to be in Watch where it can see both.