954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Undeclared Identifier 'x' Probably something easy to fix.

Hello I an new to C++ and am making my first program from scratch that has a useful solution after a while debugging I notice a problem with a displayed item and so I change the the code and a new error comes up...
[TEX]------ Build started: Project: A_Message, Configuration: Debug Win32 ------
name.cpp
g:\vs2010projects\a_message\a_message\name.cpp(28): error C2065: 'x' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========[/TEX]

My code is like this and I just can't quite fix it...

#include "stdafx.h"

#include "iostream"


int printing();

int naming()
{
	using namespace std;
		cout << "Enter your name:" << endl; 
			int x;
				cin >> x; 


#ifndef USER_NAME
#define USER_NAME x
#endif
	printing();
	return 0;
}	



int printing()
{
	using namespace std;
	cout << "Hello,"<< USER_NAME << endl;
		return 0;
}


All the other code is fine just the #define USER_NAME x is a problem and the error is in Printing() when it tries to find x or something, as I said I'm new to this so any help is appreciated. Im using visual studio express 2010 on windows XP professional SP3
Thanks,
Bobbysmile
(FIles as atachments)

Attachments name.cpp (0.35KB)
Bobbysmile
Newbie Poster
21 posts since Jul 2011
Reputation Points: 6
Solved Threads: 0
 

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
Moderator
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
 

Thank you for explaining
The error message is copied from Msvs 2010
Also yes my coding orginisation is rubbish, I started learning C++ about 2 weeks ago at home and since I'm 13 it's not a thing I plan to do professionaly
This is a small part of a program that I plan to make user specific.

Also does this mean you can have multiple main groups? I didn't make it clear that this is a multifile program.

Bobbysmile
Newbie Poster
21 posts since Jul 2011
Reputation Points: 6
Solved Threads: 0
 
#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
 

Sorry I have been away for a bit and because of this I have not been able to reply,
The error message is at the top off the post but I understand that is is not particularly clear due to a error by me in the posting of this thread.
Thank you for your help and here is the corrected error message.
Insults are not appreciated :P

ERROR:
error C2065: 'x' : undeclared identifier

Simple as that.

Bobbysmile
Newbie Poster
21 posts since Jul 2011
Reputation Points: 6
Solved Threads: 0
 

Oh and as for how far in I am you can see here:

Here!

Im using free online material, not ideal but as this is a hobby it works well enough for me.

Bobbysmile
Newbie Poster
21 posts since Jul 2011
Reputation Points: 6
Solved Threads: 0
 

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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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';
}


SO declaring X in both should fix it?

Bobbysmile
Newbie Poster
21 posts since Jul 2011
Reputation Points: 6
Solved Threads: 0
 

It'll fix the error, but may not be what you want logically.

Narue
Bad Cop
Administrator
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.


Ok thanks, I just simplified it and that fixed it all.

Bobbysmile
Newbie Poster
21 posts since Jul 2011
Reputation Points: 6
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: