Why doesn't this code work

Can you please help me with why this code doesn't work.

1
2
3
4
5
6
namespace date { 

   int day; int month; int year; 
   void setdate(int x, int y, int z) { day = x; month = y; year = z;} 

} 




1
2
3
4
5
6
7
8
#include<iostream> 
#include "date.h" 
using namespace std; 

int main() {
   cin >> date.day >> date.month >> date.year;
   cout << date.day << '/' << date.month << '/' << date.year << endl;
}
When using your own namespaces it needs to be namespace::variable/function. The method you are using in main is for structs or classes. You need to do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

namespace date{
	int day, month, year;

	void setDate(int x, int y, int z) { day = x; month = y; year = z;}
}

int main()
{
	int dday, mmonth, yyear;

	cin >> dday >> mmonth >> yyear;

	date::setDate(dday, mmonth, yyear);

	cout << date::day << '/' << date::month << '/' << date::year << endl;

	return 0;
}


I could have used your code, but figured I'd use the function and then print out the data you enter just to make sure all of your namespace was used. I hope that helps.
Last edited on by closed account z6A9GNh0
Thank you :)
Topic archived. No new replies allowed.