help Coding

I have a program that prompts the user to put in to dates and then calculates the days the problem is if someone were to put in a / the code would not work properly


i have managed to put in code to make it so the program stops and displays a invalid massage
is it possible to make it so the computer skips write over it and calculates for the numbers anyway


Hears my code:
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* this program will prompt the user to type in two dates then*\
\*the computer will calculate the amount of years between them*/


#include <iostream>
using namespace 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;
    // divides by 4 and checks if there is a remander        
       if((year % 4) == 0)
       {
            Divisible_4 = true;
       }
       else
       {
            Divisible_4 = false;           
       }
     // checks 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 && ! Divisible_100) || (Divisible_100 && ! Divisible_400))
         {
           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 Spacer;
    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: " << endl;
    // input Date 1
    cin  >> Month1;
    cin  >> Spacer;
     if(Spacer == '/')
    {
           cout << "  Invalid operation  " << endl;
      break;
      } 
    cin  >> Day1;
    cin  >> Spacer;
    if(Spacer == '/')
    {
           cout << "  Invalid operation  " << endl;
      break;
      } 
    cin  >> Year1;    
       
    TotalDay1 = MonthToDays(Month1, Year1) + Day1 + (Year1 * 365.25);
    
    cout << "Date 2: ";
    // input Date 2
    cin  >> Month2; 
    cin  >> Spacer;
     if(Spacer == '/')
    {
           cout << "  Invalid operation  " << endl;  
      break;
      } 
    cin  >> Day2;
    cin  >> Spacer;
    if(Spacer == '/')
    {
           cout << "  Invalid operation  " << endl; 
      } 
    cin  >> Year2;
       
    TotalDay2 = MonthToDays(Month2, Year2) + Day2 + (Year2 * 365.25);
    
    // Displays Days between moths alone with dates
    cout <<" There is " << TotalDay1 - TotalDay2 << " Between " << Month1 << "/" << Day1 << "/" << Year1 << " and "
     << Month2 << "/" << Day2 << "/" << Year2 << endl << endl << endl << 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;
}
          
          
    
                
Last edited on
I just put some help in the program and now it should work. Check it out:
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* this program will prompt the user to type in two dates then*/
/*the computer will calculate the amount of years between them*/


#include <iostream>
using namespace 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;
    // divides by 4 and checks if there is a remander        
       if((year % 4) == 0)
       {
            Divisible_4 = true;
       }
       else
       {
            Divisible_4 = false;           
       }
     // checks 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 && ! Divisible_100) || (Divisible_100 && ! Divisible_400))
         {
           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 Spacer;
    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: Month: " << endl;
    // input Date 1
    cin  >> Month1;
    cout<<"input separator"<<endl;
    cin  >> Spacer;
     if(Spacer == '/')
    {
           cout << "  Invalid operation  " << endl;
      break;
      } cout<<"day: "<<endl;
    cin  >> Day1;
    cout<<"input separator"<<endl;
    cin  >> Spacer;
    if(Spacer == '/')
    {
           cout << "  Invalid operation  " << endl;
      break;
      } 
      cout<<"insert year:"<<endl;
    cin  >> Year1;    
       
    TotalDay1 = MonthToDays(Month1, Year1) + Day1 + (Year1 * 365.25);
    
    cout << "Date 2: month2: ";
    // input Date 2
    cin  >> Month2; 
    cout<<"input separator"<<endl;
    cin  >> Spacer;
     if(Spacer == '/')
    {
           cout << "  Invalid operation  " << endl;  
      break;
      } cout<<"day: "<<endl;
    cin  >> Day2;
    cout<<"input separator"<<endl;
    cin  >> Spacer;
    if(Spacer == '/')
    {
           cout << "  Invalid operation  " << endl; 
      } 
      cout<<"insert year"<<endl;
    cin  >> Year2;
       
    TotalDay2 = MonthToDays(Month2, Year2) + Day2 + (Year2 * 365.25);
    
    // Displays Days between moths alone with dates
    cout <<" There is " << TotalDay1 - TotalDay2 << " Between " << Month1 << "/" << Day1 << "/" << Year1 << " and "
     << Month2 << "/" << Day2 << "/" << Year2 << endl << endl << endl << 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;
}
Topic archived. No new replies allowed.