By using arrays, I meant have arrays for the month names (you have this) and the days in month. Then you referer to them with a variable as the subscript:
mont = Months[m5 -1];
do something similar for the days in month array
Also, try to have meaningful names for variables, I don't know why everyone gets into their head to abbreviate the heck out of everything. As you go further in your coding, you will often find yourself making variable names longer.
Good variable names can make the code read like telling a story of what is happening, here is a somewhat comical thing, but there is no reason why we all can't make our code read in this storytelling fashion:
First Im in cs 1428 Which is a beginner class to coding so i have no idea what a toupper function is hahah. And secondly I dont exactly know what you mean by using arrays to add everything up. Lastly I see you saw that my coding has a problem with the looping. What do you think I should do to fix it. What do you mean using a bool quit? And how exactly would I add all the days up. Something like month 1 + month 2 + user input of what day? Like if they put march 5 it would be like January + February + 5?
I dont exactly know what you mean by using arrays to add everything up.
Have an array with the the number days in a months:
1 2 3
std::string MonthNames[] = {"Invalid", "January", "February", "March" /* etc */ }
unsignedint DaysInMonth[] = {0,31,28,30 /* etc */}; // the first one is zero because that's an invalid month
If we are in November what code would you write to add up all the days from Jan to October? Hint: use a for loop and variables, don't do "month 1 + month 2" or "January + February +". The variable operates on the array subscript.
What do you mean using a bool quit?
1 2 3 4 5 6 7
bool Quit = false;
while (!Quit) {
// user wants to quit
Quit = true;
}
And how exactly would I add all the days up.
Well, how would you ? The concept I mean. If you had pen & paper, how would you do it? I am trying to get you to think how to do it with a for loop, with 1 line of code in the loop.
Honestly I have no idea how to do any of that. Im not understanding what the bool quit is even going to be used for. I kind of understand the array I dont know why I need invalid or 0. And lastly I dont know how to add up all the days in a for loop. Sorry If Im being difficult. Ive literally been in coding for like 6 weeks so things are hard for me to understand.
std::string MonthNames[] = {"Invalid", "January", "February", "March" /* etc */ }
unsignedint DaysInMonth[] = {0,31,28,30 /* etc */}; // the first one is zero because that's an invalid month
unsignedint MonthNum = 2;
std::cout << "Second Month is " << MonthNames[MonthNum] << "\n";
std::cout << "Days in Second Month is " << DaysInMonth[MonthNum] << "\n";
MonthNum = 0;
std::cout << MonthNames[MonthNum] << "\n";
unsignedint NumDaysInMonth = DaysInMonth[MonthNum];
if (NumDaysInMonth == 0){
std::cout << "Month Number error " << "\n";
}
std::cout << DaysInMonth[MonthNum] << "\n";
unsignedint MyArray[] = {1,2,3,4,5,6,7,8,9,10}
unsignedint Sum = 0;
unsignedint Size = 10;
// think about how to do the following if it is only some of the months
for (unsignedint Counter = 0; Counter < Size; ++Counter) {
Sum += MyArray[Counter];
}
std::cout << "Sum is " << Sum << "\n";
I didnt see that you replied and I already figured out the code on how to add all the days up. Its very long but it gets the job done so I am content. I dont know how I am going to figure out the leap year however. If you could help me with that that would be wonderful. I already Have a function that finds out if the year is a leap year or not but if you could help me figure out how to implement that into my program I would be very grateful.
#include <iostream>
#include <string>
usingnamespace std;
bool month(int m);
bool day (int d);
bool year (int y);
bool leapyear(int y);
int da(int m);
int total(int b);
string name (int m5);
int main()
{
int m5; // The input for what month it is
int d5; // The input for what day it is
int y5; // The input for what year it is
char choice; // Choice is if user wants to continue
cout << "This C++ Program prints the day number of the year,"
<< " given the date\nin the form of month, then day, then year"
<< endl;
do
{
cout << "Enter date in the form : month day year : \n\nEnter Month :";
cin >> m5;
if (month(m5)== false)
{
cout << m5 <<" is an invalid month number" << endl;
cout << "\n\nTry another date Y/N ---> ";
cin >> choice;
}
cout << "Enter Day : ";
cin >> d5;
if (day(d5)== false)
{
cout << d5 <<" is an invalid day number" << endl;
cout << "\n\nTry another date Y/N ---> ";
cin >> choice;
}
cout << "Enter Year : ";
cin >> y5;
if (year(y5)== false)
{
cout << y5 <<" is an invalid year number" << endl;
cout << "\n\nTry another date Y/N ---> ";
cin >> choice;
}
if (leapyear(y5)== true)
{
cout << "\n" << y5 << " is a leap year" << endl;
}
cout <<"\n" << name(m5) <<" - " << d5 << " - " << y5<< endl;
cout << "Is day number " << total(m5)+d5 << " of the year " << y5 << endl;
if (choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'N')
{
cout << "Invalid choice - enter y/n";
cin >> choice;
}
}
while (choice != 'n' && choice != 'N');
cout << "\nProgram is Terminated";
cout << "\nTaylor Burcham - November - 9th - 2015" << endl;
return 0;
}
bool month(int m5)
{
if (m5 < 1 || m5 > 12)
returnfalse;
elsereturntrue;
}
bool day (int d5)
{
if (d5 < 1 || d5 > 31)
returnfalse;
elsereturntrue;
}
bool year (int y5)
{
if (y5 > 0 && y5 <= 2015)
returntrue;
elsereturnfalse;
}
bool leapyear( int y5 )
{
if ( (y5 % 4 == 0 && y5 % 100 != 0) || ( y5 % 400 == 0))
returntrue;
elsereturnfalse;
}
int da(int m5)
{
int days;
if (m5 == 1 || m5 == 3 || m5 == 5 || m5 == 7
|| m5 == 8 || m5 == 10 || m5 == 12)
days = 31;
elseif (m5 == 2)
days = 28;
elseif
(m5 == 4 || m5 == 6 || m5 == 9|| m5 ==11)
days = 30;
days != m5;
return days;
}
string name(int m5)
{
string mont;
string Months[]={"January","February","March","April","May",
"June","July","August","September","October","November","December"};
if (m5 == 1)
mont = Months[0];
elseif (m5 == 2)
mont = Months[1];
if (m5 == 3)
mont = Months[2];
if (m5 == 4)
mont = Months[3];
if (m5 == 5)
mont = Months[4];
if (m5 == 6)
mont = Months[5];
if (m5 == 7)
mont = Months[6];
if (m5 == 8)
mont = Months[7];
if (m5 == 9)
mont = Months[8];
if (m5 == 10)
mont = Months[9];
if (m5 == 11)
mont = Months[10];
if (m5 == 12)
mont = Months[11];
return mont;
}
int total(int m5)
{
int sum;
if (m5 ==1)
sum = 0;
elseif (m5 ==2)
sum = 31;
elseif (m5 ==3)
sum = 31+28;
elseif (m5 ==4)
sum = 31+28+31;
elseif (m5 ==5)
sum = 31+28+31+30;
elseif (m5 ==6)
sum = 31+28+31+30+31;
elseif (m5 ==7)
sum = 31+28+31+30+31+30;
elseif (m5 ==8)
sum = 31+28+31+30+31+30+31;
elseif (m5 ==9)
sum = 31+28+31+30+31+30+31+31;
elseif (m5 ==10)
sum = 31+28+31+30+31+30+31+31+30;
elseif (m5 ==11)
sum = 31+28+31+30+31+30+31+31+30+31;
elseif (m5 ==12)
sum = 31+28+31+30+31+30+31+31+30+31+30;
return sum;
}
Its very long but it gets the job done so I am content.
To be brutally honest, that is a lackadaisical attitude. I am sorry if you find that offensive, I mean well (honestly) - but I am trying to give you an old fashioned shoulder-shunt in the direction of doing it properly. I gave you a pretty big hint in my last post - so I suggest you read that carefully.
Your current code is exactly NOT how to do it, and seems to miss the point of why we have computers.
What if you had 1 million items of data - would you write 100,000 LOC to add them up?
So you have been learning for 6 weeks now? That should be plenty enough time to learn the concepts to do this assignment.
All your topics seem to start with: "This is my assignment and I have no idea how to do it."
but if you could help me figure out how to implement that into my program I would be very grateful.
Really? Why should I bother - if you just seem to ignore what I am saying to you - and instead do the opposite?
So stop lolly-gagging about, get all your brain cells into 1 sock :+) - and THINK
sometimes it's hard to tell if someone has no idea, or they just want to be annoying on purpose