undeclared identifier

New to C++ and getting "undeclared identifier" on "course" and "gpa". Jut trying to write simple array program to store and print grades. Next program will convert to letter grades. Thank you


// array tj1.cpp : Defines the entry point for the console application.
#include "stdafx.h" //
#include <iostream> // allows program to output data to the screen
// defines format for cout, setw, endl so you can use short name //
#include <iomanip>
using std::cout;
using std::endl;
using std::setw;
// functions main begins program execution
int main()
{
int gpa[5] = {3, 3, 2, 4, 4 };
cout << "Course" << setw(10) << "GPA " << endl;
for ( int p =0; p < 5; p++ )
cout << setw (6) << p << setw(10) << gpa[p] << endl;
// end of function main
return 0; //indicates program ended successfully
}
That compiles fine for me when I remove the #include "stdafx.h" line (not available on Linux)
what are you using to compile the code? dev c++, VC++, etc?
GCC

Can you post the exact error message?
Last edited on
Hi Adam8797,
I am using VC++ 2005 express edition. Came with the class.
Hi Galik,
Her is the error message
1>------ Build started: Project: array tj1, Configuration: Debug Win32 ------
1>Compiling...
1>stdafx.cpp
1>Compiling...
1>array tj1.cpp
1>c:\documents and settings\administrator\my documents\visual studio 2005\projects\test print program\array tj1\array tj1.cpp(16) : error C2065: '“Course”' : undeclared identifier
1>c:\documents and settings\administrator\my documents\visual studio 2005\projects\test print program\array tj1\array tj1.cpp(16) : error C2065: '“GPA”' : undeclared identifier
1>Build log was saved at "file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\test print program\array tj1\Debug\BuildLog.htm"
1>array tj1 - 2 error(s), 0 warning(s)
Something looks very wrong there. Neither “Course” nor “GPA” are identifiers. My best guess is that you may have bad characters in your source file, perhaps its in the wrong encoding?

Maybe try retyping the program into a fresh project?
Hi,

this comment maybe a bit offtopic, yet usefull.
I find it a lot easier to use using namespace std instead of using std:: ..
This is quite arguable, but seeing that a lot of operators i use (@ experts: please note that i'm a beginner too, so correct me if i'm wrong) are in the namespace std, so it'd spare you a lot of time to declare this, instead of various declarations. That would just get the same result.

As for the on topic part. I've compiled it with devc++ but it worked for me, so sorry I can't help..
I find it a lot easier to use using namespace std instead of using std:: ..

Try to avoid using those (either of them), it makes the whole namespace system lose it's point. You scatter the global namespace with all the symbols defined in the std namespace. It's like copying all your files into the root directory -> easily becomes unmanageable (what if you have two symbols (files) with the same name?).
Galik and all
Thank you.
Galik you were right I must of had a bad character somplace. I retyped into a new project (no copy and paste) and it workded fine. Thank you!!!
// array tj1.cpp : Defines the entry point for the console application.
// Tom Gormeley 6/24/10 10:50PM
// example of array
// array of grades
#include "stdafx.h" //
#include <iostream> // allows program to output data to the screen
// defines format for cout, setw, endl so you can use short name //
#include <iomanip>
using std::cout;
using std::endl;
using std::setw;
// functions main begins program execution
int main()
{
int gpa[5] = {3,3,4,2,2};
cout << "Course" << setw(10) << "GPA" << endl;
for ( int p =0; p < 5; p++ )
cout << setw (6) << p << setw(10) << gpa[p] << endl;
// end of function main
return 0; //indicates program ended successfully
@R0mai: ok, then i'll just use std::... etc :)
@tj513: next time you post a code, please post it using [code ] and [/code ] (without the spaces). That makes it easier for us to read :) and you probably don't code this way too (otherwise start coding using tabs etc, so its easier to read for yourself too (: )

Topic archived. No new replies allowed.