Not a clue. Can't read the error message, you don't tell us what line it's on, and your indenting is baaaad.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Not a clue. Can't read the error message, you don't tell us what line it's on, and your indenting is baaaad.
Give people some break would you. Not all are fluent in english or do they have enough skills to communicate in english. And since he is a beginner, one should expect bad question like this.
@OP, I'm not completely sure why you are doing the things you are doing, but here is a simple fix,
#include "stdafx.h"
#include <iostream>
#include <string>
int printing(const std::string& name);
int main()
{
using namespace std;
cout << "Enter your name:" << endl;
string name;
cin >> name;
printing(name);
return 0;
}
int printing(const std::string& userName)
{
using namespace std;
cout << "Hello,"<< userName << endl;
return 0;
}
you need to define,main, which is the entry point of the program;where the program starts. You also need to use std::string if you want the user to input characters.
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
#define USER_NAME x is a problem
Right, but WHAT is the problem? You should get an exact error message from the compiler and/or linker. Not sure what you mean by a main "group". You need a main function and you need EXACTLY ONE main function. So no, you can't have multiple "main" functions.
Aside from any compiling and/or linking errors, you define x as an int and then ask the user to type their NAME. Unless their name is an integer, that's not going to work. Make x a string.
And I'd get rid of the #define stuff till you get a little farther along, ESPECIALLY if this is a multiple file project.
Also, stick "using namespace std" BEFORE the functions, not inside of them. Namespaces are tricky. Generally you stick that line at the top in all your programs, at least in the beginning. Then when you learn about namespaces, you change according to your needs. But two weeks in, I would just stick that line at the top.
firstPerson, I'd get rid of the "const" and the "&" in your code. It's not wrong and indeed it's preferable, but given that the OP is 2 weeks into this, he probably hasn't gotten to "const" and "pass by reference" yet. Just have the function take a regular old string or even have it take nothing and make the variable global. We don't know if he's covered parameter passing yet.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
error C2065: 'x' : undeclared identifier
Simple as that.
Yes, quite simple. You try to use x in printing() (via the USER_NAME macro), but x isn't declared in printing(). Macros are usable from the point of definition down (unless you #undef) and not bound by scope, so the effect is no different than this:
#include <iostream>
#define FOO x
void foo()
{
int x = 11;
// OK. x is declared here
std::cout << FOO << '\n';
}
int main()
{
// Error! x is not declared here
std::cout << FOO << '\n';
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
It'll fix the error, but may not be what you want logically.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401