C++ project

Ok so my code runs, but when i input a date it doesn't show up. I just started taking this class and barely know what to do. Please help me...

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
  #include <iostream>

using namespace std;

int main()
{
    int currentdate, day, month, year;
    
    cout << "Enter (date (mm/dd/yyyy): 06/10/2014 <enter> " << endl;
    cin >> currentdate;
    
if (currentdate < 0 || day < 0 || month < 0 || year < 0 )

    cout << "***Error...Please enter positive numbers" << endl;
   
if (month == 1)
    cout << "01" << endl;
if (month == 2)
    cout << "02" << endl;
if (month == 3)
    cout << "03" << endl;
if (month == 4)
    cout << "04" << endl;
if (month == 5)
    cout << "05" << endl;
if (month == 6)
    cout << "06" << endl;
if (month == 7)
    cout << "07" << endl;
if (month == 8)
    cout << "08" << endl;
if (month == 9)
    cout << "09" << endl;
if (month == 10)
    cout << "10" << endl;
if (month == 11)
    cout << "11" << endl;
if (month == 12)
    cout << "12" << endl;
    
     cout << "Month entered: " << endl;
     cin >> month;
     
if (day <= 31 &&  day >= 1) {
   day = day;
}
   cout << "Day entered: " << endl;
   
if (year >= 2000 && year <= 2014) {
    year = year;
}
    cout << "Year entered: " << endl;

    
    
return(0);
}
The structure of your program doesn't make a lot of sense. You declare variables currentdate, day, month, year, but you are only defining a value for currentdate.

Your day, month, and year variables are still undefined at this point, but you are trying to use them in your program (Line 12).

Edit: Reading more of it, you seem not understand the logic or syntax. What are you trying to accomplish with if (whatever) { year = year; }? This does nothing.


One option is to use cin 3 times: first entering the month and saving that to your month variable, then the day, then the year.
Another option, if you want to still be able to enter the numbers in a 'mm/dd/yy' format, is to cin that as a std::string instead of an int, and then make substring variables (http://www.cplusplus.com/reference/string/string/substr/), and cast them to ints.
Last edited on
Topic archived. No new replies allowed.