-2

I am having trouble with the portion where I need to print the day. I tried making a second variable but it is not working. Basically I take a user input for their birthday. Then I'm trying to call this function that determines the day of birth (it determines a number that represents a day). Then I am trying to send this number to the function that takes the number and prints the birthday day in words. I am now getting the error that 'int day2' redefinition.

Here is my code:

void determineDayOfBirth() {

    int day;
    int month;
    int year;
    char backslash;
    char backslash2;

    cout << "Enter your date of birth" << endl;
    cout << "format: month / day / year -->" << endl;
    cin >> month >> backslash >> day >> backslash2 >> year;

    if (isValidDate(month, day, year)) {
        int day2;
        cout << "You were born on a: ";
        int day2 = determineDay(month, day, year);
        printDayOfBirth(day2); 
        cout << endl;
        cout << "Have a great birthday!!!";
    }
    else {
        cout << "Invalid date";
    }
    return;
}
xinaiz
  • 7,744
  • 6
  • 34
  • 78
NICE8xx
  • 21
  • 1
  • 3
  • 1
    How else should the compiler tell you that you've redefined `day2`? . Please, check your code again, you'll see two declarations of `int day2`. You simply use the name when assigning. No need for the second type declaration `int`. – WhiZTiM Oct 07 '16 at 22:59

4 Answers4

2

Remove the int from the second assignment, you can't define a variable twice in the same block.

To fix your code, replace:

int day2;
cout << "You were born on a: ";
int day2 = determineDay(month, day, year);

With:

cout << "You were born on a: ";
int day2 = determineDay(month, day, year);
Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
1

You have put two times "int day2", which indeed is a redefinition. You have to define "day2" only once:

if (isValidDate(month, day, year)) {
    int day2;
    cout << "You were born on a: ";
    day2 = determineDay(month, day, year); // REMOVE "int"
    printDayOfBirth(day2); 
    cout << endl;
    cout << "Have a great birthday!!!";
}
else {
    cout << "Invalid date";
}
return;
Raul Luna
  • 1,945
  • 1
  • 17
  • 26
0

The cause of the problem is

    int day2;
    cout << "You were born on a: ";
    int day2 = determineDay(month, day, year);

The second is a redefinition of day2.

Remove the int keyword from that line, and it will become a simple assignment.

Peter
  • 35,646
  • 4
  • 32
  • 74
0

you can't declare two variables in the same scope so day2 is declared twice in your if block. you can directly write:

//if(){
     int day2 = determineDay(month, day, year);
//}
Raindrop7
  • 3,889
  • 3
  • 16
  • 27