how can i transfer the dayOfWeek variable to printDayOfBirth?

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
int determineDay(int month, int day, int year) {
	char slash_dummy;
	cout << "Enter your date of birth" << endl;
	cout << "format : month / day / year--> ";
	cin >> month >> slash_dummy >> day >> slash_dummy >> year;
	int monthEq;
	if (month == 1 || month == 2) {
		monthEq = month + 12;
	}
	else {
		monthEq = month;
	}
	int dayEq = day;
	int yearPrefix = year / 100;
	int yearEq = year % (yearPrefix * 100);
	int dayOfWeek = (day + ((13 * (monthEq + 1)) / 5) + yearEq + (yearEq / 4) + (yearPrefix / 4) + 5 * yearPrefix) % 7;
    return 0;
}


void printDayOfBirth(int day) {
   if (dayOfWeek == 0) {
   cout << "Saturday";
   }
   else if (dayOfWeek == 1) {
   cout << "Sunday";
   }
   else if (dayOfWeek == 2) {
   cout << "Monday";
   }
   else if (dayOfWeek == 3) {
   cout << "Tuesday";
   }
   else if (dayOfWeek == 4) {
   cout << "Wednesday";
   }
   else if (dayOfWeek == 5) {
   cout << "Thurday";
   }
   else {
   cout << "Friday";
   }
   return;
}

how can i transfer the value of dayOfWeek to printDayOfBirth? It says the variable cant be found.
Last edited on
1
2
int dayOfWeek = determineDay(month, day, year);
printDayOfBirth(dayOfWeek);
You have posted two threads on same issue. The other thread: http://www.cplusplus.com/forum/general/243605/

That is double-posting. You had to write two posts. You have to follow two threads in case someone answers. Those, who can answer, will waste their time, if they don't notice that issue has already been resolved in the other thread. You don't want to double-post.


How to use functions: http://www.cplusplus.com/doc/tutorial/functions/


variable cant be found

We should say "What variable? Show exact error message."
1
2
3
4
void printDayOfBirth( int day ) // Remark: 'day' is not used in this function
{
   if ( dayOfWeek == 0 ) {} // Error: What is 'dayOfWeek'?
}

If function is called with a value, then the function parameter has the value:
1
2
3
4
void printDayOfBirth( int day )
{
   if ( day == 0 ) {} // OK
}



1
2
3
4
5
6
int determineDay( int snafu )
{
  cin >> snafu; // You totally ignore the value that the function was called with
  int answer = 42 * snafu; // You never use the answer
  return 0; // Why every day is Saturday? Lucky you?
}

Use the data, return data:
1
2
3
4
5
int determineDay( int snafu )
{
  int answer = 7 * snafu;
  return answer;
}
Last edited on
In addition to keskivertos answer my two cents:

I assume there is a global variable 'int dayOfWeek' in your program that you're trying to use in printDayOfBirth().
That doesn't work because you declare a new variable with the same name (dayOfWeek) in determineDay().
The stuff you write to dayOfWeek inside determineDay() will not end up in the global variable also named dayOfWeek.
Again, following my assumption that there is a global variable named dayOfWeek,
your code will work if you remove the 'int' in front of int dayOfWeek in determineDay().
That way you access the global variable instead of the local variable with the same name.

However, the solution keskiverto provided is better, because it doesn't involve global variables, which is always a good thing.
Topic archived. No new replies allowed.