I have an issue compiling and running the code on visual studios. It gives me the error code "declaration is incompatible with" so and so. More specifically, when calling on the header in the code in the beginning of each function from the main CPP. I've tried changing the void to int in the header, but it didn't seem to fix it. I'm stuck now and need some guidance.
The error is:
"Severity Code Description Project File Line Suppression State
Error (active) E0147 declaration is incompatible with "void convertTime::invalidHr(int hour)" (declared at line 9 of "Time.h") Source.cpp 6 "
#include <iostream>
#include "Time.h"
using namespace std;
int convertTime::invalidHr(int hour) *//error on this line*
{
int convertHour = hour;
try
{
if (hour < 13 && hour > 0)
{
hour = hour + 12;
return hour;
}
else {
cin.clear();
cin.ignore();
cout << "Invalid input! Please input hour again in correct 12 hour format: ";
cin >> hour;
invalidHr(hour);
throw 10;
}
}
catch (int c) { cout << "Invalid hour input"; }
}
int convertTime::invalidMin(int min) *//error here*
{
int convertMin = min;
try
{
if (min < 60 && min > 0)
{
return min;
}
else {
cin.clear();
cin.ignore();
cout << "Invalid input! Please input minutes again in correct 12 hour format: ";
cin >> min;
invalidMin(min);
throw 20;
return 0;
}
}
catch (int e) { cout << "Invalid minute input" << endl; }
}
int convertTime::invalidSec(int sec) *//error here*
{
int convertSec = sec;
try
{
if (sec < 60 && sec > 0)
{
return sec;
}
else {
cin.clear();
cin.ignore();
cout << "Invalid input! Please input seconds again in correct 12 hour format: ";
cin >> sec;
invalidSec(sec);
throw 30;
return 0;
}
}
catch (int t) { cout << "Invalid second input" << endl; }
}
void convertTime::printMilTime()
{
cout << "Converted time: " << hour << ":" << min << ":" << sec;
}
And here is my header:
class convertTime
{
public:
int hour, min, sec;
void invalidHr(int hour);
void invalidMin(int min);
void invalidSec(int sec);
void printMilTime();
};
The return types of the member functions don't match.
In the class definition, you have:
void invalidHr(int hour);
void invalidMin(int min);
void invalidSec(int sec);
In the implementations, you have:
int convertTime::invalidHr(int hour) { ... }
int convertTime::invalidMin(int min) { ... }
int convertTime::invalidSec(int sec) { ... }
You need to change one of them so they match.
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.
I'm trying to make it so that if the user chooses 1, it should increase the temp by 5, and decrease if the user chooses 2. the problem I'm having is that it doesn't update the value of the temperature.
UPDATE: I've finally got the math part to work but for some reason when i print the value of temperature it doesn't save the updates. If i do warmer, it's always at 20 and if i do cooler it's always at 10. I'm guessing i have to use the getTemp() because im only editing the local variable? How do I go about this?
#include <iostream>
using namespace std;
class heater
{
public:
int temperature;
int min;
int max;
int increment;
heater(int min=0, int max=60)
{
increment = 5;
temperature = 15;
}
int warmer(int);
int cooler(int);
int getTemp(int);
};
int heater::warmer(int temperature) {
if (temperature > (temperature - increment))
{
temperature += increment;
return temperature;
}
else cout << "Max temp reached.";
}
int heater::cooler(int temperature) {
if (temperature < (temperature + increment)){
temperature -= increment;
return temperature;
}
else cout << "Min temp reached.";
}
int heater::getTemp(int temperature) {
return temperature;
}
int main()
{
heater w;
heater c;
heater t;
heater g;
int i;
int number;
int temp;
for (i = 1; i != 0; i=1)
{
cout << "\n1. Warmer \n";
cout << "2. Cooler \n";
cout << "Press 0 to exit. \n";
cin >> number;
if (number == 1) {
temp = w.warmer(t.temperature);
cout << "The temperature is now: " << temp;
}
else if (number == 2) {
temp = c.cooler(t.temperature);
cout << "The temperature is now: " << temp;
}
else if (number == 0){
break;
}
}
return 0;
}
In int heater::warmer(int temperature) parameter named temperature shadows class member called temperature and you only modify that local parameter.
Simply remove that parameters and modify your class member:
#include <iostream>
using namespace std;
class heater
{
public:
int temperature;
heater()
{
temperature = 15;
}
int warmer();
int cooler();
int getTemp();
};
int heater::warmer() {
temperature -= 5;
}
int heater::cooler() {
temperature += 5;
}
int heater::getTemp() {
return temperature;
}
int main()
{
heater w;
heater c;
heater t;
int number;
cout << "1. Warmer \n";
cout << "2. Cooler \n";
cin >> number;
if (number == 1) {
w.warmer();
cout << "The temperature is now: " << t.temperature;
}
else if (number == 2) {
c.cooler();
cout << "The temperature is now: " << t.temperature;
}
return 0;
}
Also, the line heater getTemp(); doesn't do anything useful. It declares a global function that returns heater and takes no parameters. You probably wanted to call the member function like this:
cout << "The temperature is now: " << t.getTemp();
I know this is a lot of code but it's just a program that prompts for speed, altitude, fuel, and direction and catches any errors. If an error is thrown then the program should stop and if there is no error than the program should just display all the values. My problem is that whenever the last try-catch statement(InvalidDirection) catches an error the program still runs the showAll function.
#pragma once
#include <iostream>
using namespace std;
class FlightInfo
{
private:
int absoluteAltitude = 0;
int speed = 0;
int fuelLevel = 0;
int direction = 0;
public:
class InvalidSpeed
{};
class InvalidDirection
{};
class InvalidFuelLevel
{};
class InvalidAltitude
{};
// setters
//valid 0-10000
void setAbsoluteAltitude(int alt) {
if (alt >= 0 && alt <= 10000) {
absoluteAltitude = alt;
}
else {
throw InvalidAltitude();
}
}
//valid 0 - 650
void setSpeed(int currentSpeed) {
if (currentSpeed >= 0 && currentSpeed <= 650) {
speed = currentSpeed;
}
else {
throw InvalidSpeed();
}
}
// Valid 0 - 100
void setFuelLevel(int level) {
if (level >= 0 && level <= 100) {
fuelLevel = level;
}
else {
throw InvalidFuelLevel();
}
}
// valid 0-359
void setDirection(int heading) {
if (heading >= 0 && heading <= 359) {
direction = heading;
}
else {
throw InvalidDirection();
}
}
// getters
int getAbsoluteAltitude() {
return absoluteAltitude;
}
int getSpeed() {
return speed;
}
int getFuelLevel() {
return fuelLevel;
}
int getDirection() {
return direction;
}
void showAll() {
cout <<"Altitude = " << getAbsoluteAltitude() << endl;
cout <<"Speed = " << getSpeed() << endl;
cout <<"FuelLevel = " << getFuelLevel() << endl;
cout <<"Direction = " << getDirection() << endl;
}
};
Here is my driver/cpp file
#include "FlightInfo.h"
#include <iostream>
using namespace std;
int main() {
FlightInfo flight;
int getTheSpeed;
int getTheAltitude;
int getTheFuel;
int getTheDirection;
cout << "Enter Speed ";
cin >> getTheSpeed;
try {
flight.setSpeed(getTheSpeed);
}
catch (FlightInfo::InvalidSpeed)
{
cout << "ERROR: speed less than 0 or greater than 650" << endl;
}
cout << "Enter altitude ";
cin >> getTheAltitude;
try {
flight.setAbsoluteAltitude(getTheAltitude);
}
catch (FlightInfo::InvalidAltitude)
{
cout << "ERROR: Altitude less than 0 or greater than 10000" << endl;
}
cout << "Enter Fuel Level ";
cin >> getTheFuel;
try {
flight.setFuelLevel(getTheFuel);
}
catch (FlightInfo::InvalidFuelLevel)
{
cout << "ERROR: Fuel level less than 0 or greater than 100" << endl;
}
cout << "Enter direction ";
cin >> getTheDirection;
try {
flight.setDirection(getTheDirection);
}
catch (FlightInfo::InvalidDirection)
{
cout << "ERROR: Direction is less than 0 or greater than 359" << endl;
}
flight.showAll();
system("pause");
return 0;
}
The Problem:
Even when the last try-catch statements catch an error the program still runs the showAll function.
The Reason:
None of the catch clauses exits the program.
Possible Solutions:
In every catch clause, put a return <any number other than zero>.
Do not catch the Exceptions.
In every catch clause, put an exit clause.
I am not sure that I see the issue with what you described. Are you expecting when invalid direction is thrown the program does not get to showall()?:
cout << "Enter direction ";
cin >> getTheDirection;
try {
flight.setDirection(getTheDirection);
}
catch (FlightInfo::InvalidDirection)
{
cout << "ERROR: Direction is less than 0 or greater than 359" << endl;
}
flight.showAll();
But here you are "catch"ing the exception and handling it. Therefore the program continues. If you want it to end you will need to re-throw or not catch the exception:
cout << "Enter direction ";
cin >> getTheDirection;
//try {
flight.setDirection(getTheDirection);
//}
//catch (FlightInfo::InvalidDirection)
//{
// cout << "ERROR: Direction is less than 0 or greater than 359" << endl;
//}
flight.showAll();
Now your inner "throw" will not be caught and your program will end.
The program is supposed to repeat after the user enters incorrect data for hours, minutes, seconds.
I can't get programs to loop on Xcode.
Example:
user enters invalid data for hours, minutes, and seconds.
Program displays "invalid data", and then asks the user if they would like to repeat the program, if the user enters "y or Y", the program asks the user to enter the time again.
#include <iostream>
#include <iomanip>
using namespace std;
struct Time
{
int hours ;
int seconds;
int minutes;
};
void getTime(Time &time);
bool isTimeValid(Time &time);
void addOneSecond(Time &time);
void displayTime(Time &time);
const int MAX_HOURS = 23;
const int MAX_MINS = 59;
const int MAX_SECS = 59;
int main()
{
Time time;
getTime(time);
isTimeValid(time);
addOneSecond(time);
displayTime(time);
return 0;
}
void getTime(Time &time)
{
cout << "Enter the time in \"military time\", (24-hour format), in"
<< " the following order:\nHH:MM:SS, (Hours, Minutes, Seconds).\n\n";
cout << "Hours: ";
cin >> time.hours;
cout << "Minutes: ";
cin >> time.minutes;
cout << "Seconds: ";
cin >> time.seconds;
cout << endl << endl;
}
bool isTimeValid(Time &time)
{
bool answer = ' ';
if (((time.hours >= 0) && (time.hours <= MAX_HOURS)) &&
((time.minutes >= 0) && (time.minutes <= MAX_MINS)) &&
((time.seconds >= 0) && (time.seconds <= MAX_SECS)))
{
return true;
}
else
{
cout << "Invalid Time.\n\n";
return false;
}
cout << "Do it again? (Y/N)";
cin >> answer;
do
{
getTime(time);
isTimeValid(time);
addOneSecond(time);
displayTime(time);
} while(toupper(answer) == 'T' );
}
void addOneSecond(Time &time)
{
time.seconds++;
if (time.seconds > MAX_SECS)
{
time.seconds = 0;
time.minutes++;
}
}
void displayTime(Time &time)
{
cout.fill('0');
cout << "After adding one second, the time is " << setw(2) << time.hours
<< ":" << setw(2) << time.minutes << ":" << setw(2)
<< time.seconds << ".\n\n";
}
I'm trying to perform error checking on my initializer value for date, so that a day or month outside the acceptable range will stop the program from debbuging, but I'm getting a debug error even for the acceptable range. I don't know where the error is from, so i posted the code.
// ConsoleApplication56.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<stdexcept>
#include<array>
using namespace std;
class Date
{
public:
explicit Date(int mo, int da, int ye)
{
setDate( mo, da, ye);
}
void setDate(int &m, int &d, int &y)
{
setMonth(m);
setDay(d);
setYear(y);
}
void setMonth(int &m)
{
if (month>0 && month <13)
{
month = m;
}else
throw invalid_argument("month must be 1-12");
}
unsigned int getMonth()const
{
return month;
}
void setDay(int &d)
{
if(day>0 && day<32)
{
day = d;
}else
throw invalid_argument("day must be 0-31");
}
unsigned int getDay()const
{
return day;
}
void setYear(int &y)
{
year = y;
}
unsigned int getYear()const
{
return year;
}
void print()
{
cout<< month <<'/' << day << '/' << year;
}
void nextDay()
{
int numberOfDaysToAdd = 1;
array <int,12> daysInAMonth = {31,28,31,30,31,30,31,31,30,31,30,31};
cout << "the date before is: " << month << "/" << day << "/" << year << endl;
day += numberOfDaysToAdd;
while (day > daysInAMonth [month - 1 ] )
{
day-= daysInAMonth [month - 1 ];
month++;
if (month > 12){
month = 1;
year++;
}
}
cout << "the day after is: " << month << "/" << day << "/" << year << endl;
}
private:
unsigned int month;
unsigned int day;
unsigned int year;
};
int main()
{
char response = 'y';
Date date(12, 3, 2013 );
cout<<"The date is :";
date.print();
cout<<endl;
cout<<endl;
cout<<"do you wish to check the next date(y/n)? :";
cin >> response;
cout<<endl;
while(response == 'y')
{
date.nextDay();
cout<<endl;
cout<<"do you wish to check the next date(y/n)? : ";
cin >> response;
cout<<endl;
}
return 0;
}
You should check the m and d values not the month and day member variables.
void setMonth(int &m)
{
if (month>0 && month <13)
should be
void setMonth(int &m)
{
if (m>0 && m <13)