i need help when i run this program it has a lot of problems
what i want the program to do is prompt the user to put in two differernt dates and output the number of days in between them.
#include <iostream>
usingnamespace std;
// inputs year and outputs febuary as 1 or 2 to be used in the MonthToDays function
int Febuary(int year)
{
int Divisible_4;
int Divisible_100;
int Divisible_400;
if((year % 4) == 0)
{
Divisible_4 = 1;
}
else
{
Divisible_4 = 0;
}
// checkes if years is divisible by 100
if((year % 100) == 0)
{
Divisible_100 = 1;
}
else
{
Divisible_100 = 0;
}
// checkes if years is not divisible by 400
if((year % 400) == 0 && year >= 400)
{
Divisible_400 = 1;
}
else
{
Divisible_400 = 0;
}
if((Divisible_4 == 1 && Divisible_100 == 0) || (Divisible_100 == 1 && Divisible_400 == 1))
{
Febuary = 1;
}
else
{
Febuary = 2;
}
return Febuary;
}
// inputs month and Year and outputs amount of days
int MonthToDays(int Month, int Year)
{
Febuary(Year);
int days;
days = (Month - 1) * 30;
switch(Month)
{
case Month == 1:
days = days + 1
case Month == 2:
days = days - Febuary + 1
case Month == 3:
days = days - Febuary + 2
case Month == 4:
days = days - Febuary + 2
case Month == 5:
days = days - Febuary + 3
case Month == 6:
days = days - Febuary + 3
case Month == 7:
days = days - Febuary + 4
case Month == 8:
days = days - Febuary + 5
case Month == 9:
days = days - Febuary + 5
case Month == 10:
days = days - Febuary + 6
case Month == 11:
days = days - Febuary + 6
case Month == 12:
days = days - Febuary + 7
}
return days;
}
int main()
{
double Day1;
double Month1;
double Year1;
double Day2;
double Month2;
double Year2;
double TotalDay1;
double TotalDay2;
double EndResult;
char again;
bool Running = true;
cout << " **Welcome to the Date Day Calculator** " << endl;
while(Running == true)
{
cout << " Type in the year: ";
cin >> year;
// user information
cout << " Plese type in the date in numbers " << endl;
cout << " make sure you type in the whole year not just the last two digits " << endl;
cout << " Press enter after each number" << end;
cout << " this program will caculate the days inbetween two Dates " << end;
cout << "Date 1: ";
// input Date 1
cin >> Month1;
cout <<"/";
cin >> Day1;
cout <<"/";
cin >> Year1;
cout << endl;
cout << "Date 2: ";
TotalDay1 = MonthToDays(Month1, Year1) + Day1 + (Year1 * 365.25);
cout << "Days: " << TotalDay1 << endl;
// input Date 2
cin >> Month2;
cout <<"/";
cin >> Day2;
cout <<"/";
cin >> Year2;
cout << endl;
TotalDay2 = MonthToDays(Month2, Year2) + Day2 + (Year2 * 365.25);
cout << "Days: " << TotalDay2 << endl;
// Displays Days between moths alone with dates
cout <<" There is " << TotalDay1 - TotalDay2 << " Between " << Month1 << "/" << Day1 << "/" << Year1 << " and "
<< Month2 << "/" << Day2 << "/" << Year2 << endl;
// asks user if they would like to redo program
cout << " Would You Like To Run this Program Again ( y or n ) " << endl;
cin >> again;
if(again == 'y' || again == 'Y')
{
Running = true;
}
else
{
if(again == 'n' || again == 'N')
Running = false;
}
}
system("PAUSE");
return 0;
}
First of all, your "February" function is kind of like this:
1 2 3 4 5 6 7 8 9
int February(int year) // Signature is OK
{
if (...) {
February = 1; // Wrong, you can't assign a value to the function name
} else {
February = 2; // Wrong again
}
return February; // You must put the value to return here, e.g. a variable or a literal integer
}
You could change that to:
1 2 3 4 5 6 7 8 9
int February(int year)
{
if (...) {
return 1; // Return directly when you know the result
} else {
return 2; // Same here
}
// No need to put a return here, because one of the above will always be taken
}
When you call return, you must assign the result to a variable, like this:
1 2
February(year); // This is not useful
int feb = February(year); // Make "feb" the result of the call
Finally, when you have variables like "Divisable_4" that is either true or false, you should use bool instead of int, and the keywords true/false instead of 1/0.
#include <iostream>
usingnamespace std;
// inputs year and outputs febuary as 1 or 2 to be used in the MonthToDays function
int FebuaryMonth(int year)
{
bool Divisible_4;
bool Divisible_100;
bool Divisible_400;
if((year % 4) == 0)
{
Divisible_4 = true;
}
else
{
Divisible_4 = false;
}
// checkes if years is divisible by 100
if((year % 100) == 0)
{
Divisible_100 = true;
}
else
{
Divisible_100 = false;
}
// checkes if years is not divisible by 400
if((year % 400) == 0 && year >= 400)
{
Divisible_400 = true;
}
else
{
Divisible_400 = false;
}
if((Divisible_4 == true && Divisible_100 == false) || (Divisible_100 == true && Divisible_400 == false))
{
return 1;
}
else
{
return 2;
}
}
// inputs month and Year and outputs amount of days
int MonthToDays(int Month, int Year)
{
int Febuary = FebuaryMonth(Year);
int days;
days = (Month - 1) * 30;
switch(Month)
{
case Month == 1:
days = days + 1
case Month == 2:
days = days - Febuary + 1
case Month == 3:
days = days - Febuary + 2
case Month == 4:
days = days - Febuary + 2
case Month == 5:
days = days - Febuary + 3
case Month == 6:
days = days - Febuary + 3
case Month == 7:
days = days - Febuary + 4
case Month == 8:
days = days - Febuary + 5
case Month == 9:
days = days - Febuary + 5
case Month == 10:
days = days - Febuary + 6
case Month == 11:
days = days - Febuary + 6
case Month == 12:
days = days - Febuary + 7
}
return days;
}
int main()
{
double Day1;
double Month1;
double Year1;
double Day2;
double Month2;
double Year2;
double TotalDay1;
char again;
bool Running = true;
cout << " **Welcome to the Date Day Calculator** " << endl;
while(Running == true)
{
cout << " Type in the year: ";
cin >> year;
// user information
cout << " Plese type in the date in numbers " << endl;
cout << " make sure you type in the whole year not just the last two digits " << endl;
cout << " Press enter after each number" << end;
cout << " this program will caculate the days inbetween two Dates " << end;
cout << "Date 1: ";
// input Date 1
cin >> Month1;
cout <<"/";
cin >> Day1;
cout <<"/";
cin >> Year1;
cout << endl;
cout << "Date 2: ";
TotalDay1 = MonthToDays(Month1, Year1) + Day1 + (Year1 * 365.25);
cout << "Days: " << TotalDay1 << endl;
// input Date 2
cin >> Month2;
cout <<"/";
cin >> Day2;
cout <<"/";
cin >> Year2;
cout << endl;
TotalDay2 = MonthToDays(Month2, Year2) + Day2 + (Year2 * 365.25);
cout << "Days: " << TotalDay2 << endl;
// Displays Days between moths alone with dates
cout <<" There is " << TotalDay1 - TotalDay2 << " Between " << Month1 << "/" << Day1 << "/" << Year1 << " and "
<< Month2 << "/" << Day2 << "/" << Year2 << endl;
// asks user if they would like to redo program
cout << " Would You Like To Run this Program Again ( y or n ) " << endl;
cin >> again;
if(again == 'y' || again == 'Y')
{
Running = true;
}
else
{
if(again == 'n' || again == 'N')
Running = false;
}
}
system("PAUSE");
return 0;
}
but it still dosent work there is a long list of errors
and i was thinking that it might have a library missing from the include directroy because when i click on some of the errors it hilights the include statment
if im not wrong, "system("PAUSE");" needs another library, stdlib.h i think? cant remember.
Anyway, solve your errors top down. solve the 1st one and compile. to your surprise, solving 1 error can solve 100+ of them together. So, do not be daunted by large errors. Only be daunted when you have no errors, yet your program cant run. XD
Double click the error statement, it'll bring u to the line of error.
I haven't actually tried to find your errors, you have to do that yourself, I just try to give you a place to start.
Here's another line that you might want to simplify:
1 2 3 4 5
// When you have boolen values, you don't need to use "== true", and instead
// of "== false", you can use the invertor operator:
if((Divisible_4 == true && Divisible_100 == false) || (Divisible_100 == true && Divisible_400 == false))
// After:
if((Divisible_4 && ! Divisible_100) || (Divisible_100 && ! Divisible_400))
thanks fro the advice i cut down a lot of errors
but i still have long list of errors that when i double click on them they show different codes not any that i wrote there probly the library codes for the editor iam using
well hers the code with all the corrections i made but it still dosent work and the stdlib dosen't solve the problem
#include <iostream>
usingnamespace std;
// inputs year and outputs febuary as 1 or 2 to be used in the MonthToDays function
int FebuaryMonth(int year)
{
bool Divisible_4;
bool Divisible_100;
bool Divisible_400;
if((year % 4) == 0)
{
Divisible_4 = true;
}
else
{
Divisible_4 = false;
}
// checkes if years is divisible by 100
if((year % 100) == 0)
{
Divisible_100 = true;
}
else
{
Divisible_100 = false;
}
// checkes if years is not divisible by 400
if((year % 400) == 0 && year >= 400)
{
Divisible_400 = true;
}
else
{
Divisible_400 = false;
}
if((Divisible_4 == true && Divisible_100 == false) || (Divisible_100 == true && Divisible_400 == false))
{
return 1;
}
else
{
return 2;
}
}
// inputs month and Year and outputs amount of days
int MonthToDays(int Month, int Year)
{
int Febuary = FebuaryMonth(Year);
int days;
Month = Month - 1;
switch(Month)
{
case 1:
days = (days + 1) * 30;
break;
case 2:
days = (days - Febuary + 1) * 30;
break;
case 3:
days = (days - Febuary + 2) * 30;
break;
case 4:
days = (days - Febuary + 2) * 30;
break;
case 5:
days = (days - Febuary + 3) * 30;
break;
case 6:
days = (days - Febuary + 3) * 30;
break;
case 7:
days = (days - Febuary + 4) * 30;
break;
case 8:
days = (days - Febuary + 5) * 30;
break;
case 9:
days = (days - Febuary + 5) * 30;
break;
case 10:
days = (days - Febuary + 6) * 30;
break;
case 11:
days = (days - Febuary + 6) * 30;
break;
case 12:
days = (days - Febuary + 7) * 30;
break;
default:
cout << " You Have typed a invalid month" << endl;
}
return days;
}
int main()
{
int Day1;
int Month1;
int Year1;
int Day2;
int Month2;
int Year2;
double TotalDay1;
double TotalDay2;
char again;
bool Running = true;
cout << " **Welcome to the Date Day Calculator** " << endl;
while(Running == true)
{
// user information
cout << " Plese type in the date in numbers " << endl;
cout << " make sure you type in the whole year not just the last two digits " << endl;
cout << " Press enter after each number" << endl;
cout << " this program will caculate the days inbetween two Dates " << endl;
cout << "Date 1: ";
// input Date 1
cin >> Month1;
cout <<"/";
cin >> Day1;
cout <<"/" << endl;
cin >> Year1;
cout << "Date 2: ";
TotalDay1 = MonthToDays(Month1, Year1) + Day1 + (Year1 * 365.25);
cout << "Days: " << TotalDay1 << endl;
// input Date 2
cin >> Month2;
cout <<"/";
cin >> Day2;
cout <<"/" << endl;
cin >> Year2;
TotalDay2 = MonthToDays(Month2, Year2) + Day2 + (Year2 * 365.25);
cout << "Days: " << TotalDay2 << endl;
// Displays Days between moths alone with dates
cout <<" There is " << TotalDay1 - TotalDay2 << " Between " << Month1 << "/" << Day1 << "/" << Year1 << " and "
<< Month2 << "/" << Day2 << "/" << Year2 << endl;
// asks user if they would like to redo program
cout << " Would You Like To Run this Program Again ( y or n ) " << endl;
cin >> again;
if(again == 'y' || again == 'Y')
{
Running = true;
}
else
{
if(again == 'n' || again == 'N')
Running = false;
}
}
system("PAUSE");
return 0;
}