Loop subtracts 1 day each time it loops.
My code is currently subtracting 1 and not outputting the correct day number of the year. Here is what I have so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main() {
int month, day, year, dayNum;
string x;
do {
cout << "Enter date in the form: month-day-year: ";
cin >> month >> day >> year;
if (((((((year % 4 == 0) && (year % 100 != 0))) || (year % 400 == 0))) && (month >=3))) {
day += 1;
}
switch (month)
{
case 1:
dayNum = abs (day);
break;
case 2:
dayNum = 31 + abs (day);
break;
case 3:
dayNum = 59 + abs (day);
break;
case 4:
dayNum = 90 + abs (day);
break;
case 5:
dayNum = 120 + abs (day);
break;
case 6:
dayNum = 151 + abs (day);
break;
case 7:
dayNum = 181 + abs (day);
break;
case 8:
dayNum = 212 + abs (day);
break;
case 9:
dayNum = 243 + abs (day);
break;
case 10:
dayNum = 273 + abs (day);
break;
case 11:
dayNum = 304 + abs (day);
break;
case 12:
dayNum = 334 + abs (day);
break;
}
cout << "This is day number " << dayNum<< " of the year "<< abs (year)<< endl;
cout << "Do you wish to continue yes(y or Y) or no (n or N) : ";
cin >> x;
if ((x == "y") || (x == "Y")) {
continue;
}
if ((x == "n") || (x == "N")) {
break;
}
} while((x=="y") || (x=="Y"));
return 0;
}
|
Last edited on
> Enter date in the form: month-day-year:
so day and year are negative
Also ymd is a lot better.
> cin statement towards the end won't take string
It does, unless you have put the stream in a bad state
show an example of your input
@ne555
I'll keep that in mind but for now I'm going to comply with the rules of the assignment.
I figured out why I was getting an error for cin statement towards the end. I did not include the string library in the header.
Here is the new code. I am able to get it to ask again but this time the dayNum variable is subratcting one every single time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main() {
int month, day, year, dayNum;
string x;
do {
cout << "Enter date in the form: month-day-year: ";
cin >> month >> day >> year;
if (((((((year % 4 == 0) && (year % 100 != 0))) || (year % 400 == 0))) && (month >=3))) {
day += 1;
}
switch (month)
{
case 1:
dayNum = abs (day);
break;
case 2:
dayNum = 31 + abs (day);
break;
case 3:
dayNum = 59 + abs (day);
break;
case 4:
dayNum = 90 + abs (day);
break;
case 5:
dayNum = 120 + abs (day);
break;
case 6:
dayNum = 151 + abs (day);
break;
case 7:
dayNum = 181 + abs (day);
break;
case 8:
dayNum = 212 + abs (day);
break;
case 9:
dayNum = 243 + abs (day);
break;
case 10:
dayNum = 273 + abs (day);
break;
case 11:
dayNum = 304 + abs (day);
break;
case 12:
dayNum = 334 + abs (day);
break;
}
cout << "This is day number " << dayNum<< " of the year "<< abs (year)<< endl;
cout << "Do you wish to continue yes(y or Y) or no (n or N) : ";
cin >> x;
if ((x == "y") || (x == "Y")) {
continue;
}
if ((x == "n") || (x == "N")) {
break;
}
} while((x=="y") || (x=="Y"));
return 0;
}
|
Topic archived. No new replies allowed.