Click here to Skip to main content
15,917,645 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to implement some simple programs in C++ using MS VISUAL STUDIO 2008.
The program listed below gave the output correctly twice,but the next time I modified it by adding declaration for some variables and some cout statements as
int n1=1,n2=2,....n16=16;
cout<<n1;
it did not give any output but gave the errors which are listed below, after the program code.
The following is the program
C++
//jprojectname.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
	int id=0,
		nodeid1,
		nodeid2,
		nodeid3,
		nodeid4,
		nodeid5,
		nodeid6,
		nodeid7,
		nodeid8,
		nodeid9,
		nodeid10,
		nodeid11,
		nodeid12,
		nodeid13,
		nodeid14,
		nodeid15,
		nodeid16;
nodeid1=1;
nodeid2=2;
nodeid3=3;
nodeid4=4;
nodeid5=5;
nodeid6=6;
nodeid7=7;
nodeid8=8;
nodeid9=9;
nodeid10=10;
nodeid11=11;
nodeid12=12;
nodeid13=13;
nodeid14=14;
nodeid15=15;
nodeid16=16;
	cout<<"The LORD is good!\n\n";
	cout << "Hello World!\n";
	cout<<id;
	cout<<n1;
return 0;
}

The following is the output window contents
1>------ Build started: Project: jprojectname, Configuration: Debug Win32 ------
1>Compiling...
1>jprojectname.cpp
1>c:\documents and settings\students\my documents\visual studio 2008\projects\jprojectname\jprojectname\jprojectname.cpp(46) : error C2065: 'n1' : undeclared identifier
1>Build log was saved at "file://c:\Documents and Settings\students\My Documents\Visual Studio 2008\Projects\jprojectname\jprojectname\Debug\BuildLog.htm"
1>jprojectname - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Could you tell what needs to be done in order to succesfulyy execute the porgram and get the output.

I have followed the following steps to create my program:

1.I created a new project,by clicking File-new-project-win32-win32console application
-then entered the name-clicked ok
-then I replaced t_main funtion by the code given in the end.
Then I saved the file.Then I followed the following steps.I clicked on the debug menu,debug-start without debugging
A message popped up asking
This project is out of date Would you like to build it?
The contents displayed above were obtained after selecting yes.

[edit]Code block added, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]
Posted
Updated 27-Jan-12 0:08am
v7

The error messages doesn't match with the source code you provided. Are you sure you reported exactly both?
By the way, in the posted code namespace is written wrongly (as a blank in, namely 'na mespace').

[update (since the question was updated)]
You didn't declare (as required) n1 and the compiler complains about.
[/update]
 
Share this answer
 
v3
Comments
RUTH J 27-Jan-12 5:59am    
I have replaced the program with the original,actual one and executed it again.I have also replaced the output window contents and have updated it.
CPallini 27-Jan-12 6:08am    
I've updated accordingly my answer. You must declare every variable you use.
C++
jprojectname.cpp(46) : error C2065: 'n1' : undeclared identifier

This part of the message says it all: the file and line where it is, and the problem at hand. 'undeclared identifier' is a message by the compiler to tell you that it found a symbol (in this case 'n1') which it couldn't identify. The usual reason is either a typo, or you forgot to declare a variable by that name. Fix the typo or insert a declaration and you're good.

In this particular case you apparently used the variable name 'n1' originally, but then added a whole list of variables. In doing so you decided to give these variables a more meaningful name ('nodeid'), but forgot to adapt the line that tries to send it to the output stream.

On a sidenote, why don't you just use an array?
 
Share this answer
 
Declare n1 and then use it....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900