Header file issue. (simple program)

I just learned how to use header file recently, and try to write this small program but somehow it does not work. I am using Microsoft visual c++

main.cpp
#include <iostream>
#include <string>
#include <stdlib.h>
#include "bandit.h"
using namespace std;


int main(int argc, char*argv[])
{
cout << "One Armed Bandit!!" <<endl;
string user = getname();
cout << "Hello " << user << endl;
system("PAUSE");
return 0;
}

getname.cpp
#include <iostream>
#include <string>
using namespace std;

string getname(){
cout <<"What's your name? ";
string name;
cin >> name;
return name;
}

#bandit.h

#ifndef BANDIT_H
#define BANDIT_H
#include <string>

string getname();
#endif BANDIT_H

I just get an error whenever I run it. I keep saying "'user' : undeclared identifier
When I run it just with main + source file , it's perfectly fine.
Please help guys
Last edited on
The header file probably needs to declare the function prototype like this: std::string getname();

This is because by the time you #include the header file bandit.h you haven't reached the "using namespace std;" line yet.

And I tell you now: Adding "using" statements to header files is bad practice, so start getting used to qualifying names with their namespace inside header files.
Thank you sooo much for helping. Thank you for your advice and i'll be sure to start practicing it
Topic archived. No new replies allowed.