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...
------ 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 ==========

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)

WaltP commented: Have you ever heard of sentences? -4

Recommended Answers

All 10 Replies

Not a clue. Can't read the error message, you don't tell us what line it's on, and your indenting is baaaad.

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.

commented: So if it was a Spanish board he would have given us more info? It's just harder in English? I don't think so. -4
commented: Waltp is a bit harsh. I see no point that you should get down rep +10

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.

#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.

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.

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.

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

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?

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.