someguy85
02-23-09, 15:23
I'm taking my first c++ programming course and I'm having some problems writing this simple program. The only errors I'm getting is "undeclared identifier", but the variable is declared and initialized (by user input) before it is used. Any help would be appreciated. The following is the code:
#include <iostream>
using namespace std;
int main()
{
int abbrev;
cout << "Please enter one of the following state abbreviations:\n";
cout << "\nNC, SC, GA, FL, or AL.";
cin >> abbrev;
if (!(abbrev == NC || abbrev == nc || abbrev == SC || abbrev == sc ||
abbrev == GA || abbrev == ga || abbrev == FL || abbrev == fl ||
abbrev == AL || abbrev == al))
{
cout << "You did not enter one of the valid abbreviations, \n"
"please restart the program and try again.\n";
}
else if (abbrev == NC || abbrev == nc)
{
cout << "You entered " << abbrev << ". The state that corresponds with that "
"\n abbreviation is North Carolina.\n";
}
else if (abbrev == SC || abbrev == sc)
{
cout << "You entered " << abbrev << ". The state that corresponds with that "
"\n abbreviation is South Carolina.\n";
}
else if (abbrev == GA || abbrev == ga)
{
cout << "You entered " << abbrev << ". The state that corresponds with that "
"\n abbreviation is Georgia.\n";
}
else if (abbrev == FL || abbrev == fl)
{
cout << "You entered " << abbrev << ". The state that corresponds with that "
"\n abbreviation is Florida.\n";
}
else
{
cout << "You entered " << abbrev << ". The state that corresponds with that "
"\n abbreviation is Alabama.\n";
}
return 0;
}
Also, are the curly braces necessary for these else if statements?
The following is the list of errors:
Compiling...
state abbreviations.cpp
c:\documents and settings\jeff\my documents\visual studio 2005\projects\week 6 homework\week 6 homework\state abbreviations.cpp(17) : error C2065: 'NC' : undeclared identifier
c:\documents and settings\jeff\my documents\visual studio 2005\projects\week 6 homework\week 6 homework\state abbreviations.cpp(17) : error C2065: 'nc' : undeclared identifier
c:\documents and settings\jeff\my documents\visual studio 2005\projects\week 6 homework\week 6 homework\state abbreviations.cpp(17) : error C2065: 'SC' : undeclared identifier
c:\documents and settings\jeff\my documents\visual studio 2005\projects\week 6 homework\week 6 homework\state abbreviations.cpp(17) : error C2065: 'sc' : undeclared identifier
c:\documents and settings\jeff\my documents\visual studio 2005\projects\week 6 homework\week 6 homework\state abbreviations.cpp(18) : error C2065: 'GA' : undeclared identifier
c:\documents and settings\jeff\my documents\visual studio 2005\projects\week 6 homework\week 6 homework\state abbreviations.cpp(18) : error C2065: 'ga' : undeclared identifier
c:\documents and settings\jeff\my documents\visual studio 2005\projects\week 6 homework\week 6 homework\state abbreviations.cpp(18) : error C2065: 'FL' : undeclared identifier
c:\documents and settings\jeff\my documents\visual studio 2005\projects\week 6 homework\week 6 homework\state abbreviations.cpp(18) : error C2065: 'fl' : undeclared identifier
c:\documents and settings\jeff\my documents\visual studio 2005\projects\week 6 homework\week 6 homework\state abbreviations.cpp(19) : error C2065: 'AL' : undeclared identifier
c:\documents and settings\jeff\my documents\visual studio 2005\projects\week 6 homework\week 6 homework\state abbreviations.cpp(19) : error C2065: 'al' : undeclared identifier
Build log was saved at "file://c:\Documents and Settings\Jeff\My Documents\Visual Studio 2005\Projects\week 6 homework\week 6 homework\Debug\BuildLog.htm"
week 6 homework - 10 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
#include <iostream>
using namespace std;
int main()
{
int abbrev;
cout << "Please enter one of the following state abbreviations:\n";
cout << "\nNC, SC, GA, FL, or AL.";
cin >> abbrev;
if (!(abbrev == NC || abbrev == nc || abbrev == SC || abbrev == sc ||
abbrev == GA || abbrev == ga || abbrev == FL || abbrev == fl ||
abbrev == AL || abbrev == al))
{
cout << "You did not enter one of the valid abbreviations, \n"
"please restart the program and try again.\n";
}
else if (abbrev == NC || abbrev == nc)
{
cout << "You entered " << abbrev << ". The state that corresponds with that "
"\n abbreviation is North Carolina.\n";
}
else if (abbrev == SC || abbrev == sc)
{
cout << "You entered " << abbrev << ". The state that corresponds with that "
"\n abbreviation is South Carolina.\n";
}
else if (abbrev == GA || abbrev == ga)
{
cout << "You entered " << abbrev << ". The state that corresponds with that "
"\n abbreviation is Georgia.\n";
}
else if (abbrev == FL || abbrev == fl)
{
cout << "You entered " << abbrev << ". The state that corresponds with that "
"\n abbreviation is Florida.\n";
}
else
{
cout << "You entered " << abbrev << ". The state that corresponds with that "
"\n abbreviation is Alabama.\n";
}
return 0;
}
Also, are the curly braces necessary for these else if statements?
The following is the list of errors:
Compiling...
state abbreviations.cpp
c:\documents and settings\jeff\my documents\visual studio 2005\projects\week 6 homework\week 6 homework\state abbreviations.cpp(17) : error C2065: 'NC' : undeclared identifier
c:\documents and settings\jeff\my documents\visual studio 2005\projects\week 6 homework\week 6 homework\state abbreviations.cpp(17) : error C2065: 'nc' : undeclared identifier
c:\documents and settings\jeff\my documents\visual studio 2005\projects\week 6 homework\week 6 homework\state abbreviations.cpp(17) : error C2065: 'SC' : undeclared identifier
c:\documents and settings\jeff\my documents\visual studio 2005\projects\week 6 homework\week 6 homework\state abbreviations.cpp(17) : error C2065: 'sc' : undeclared identifier
c:\documents and settings\jeff\my documents\visual studio 2005\projects\week 6 homework\week 6 homework\state abbreviations.cpp(18) : error C2065: 'GA' : undeclared identifier
c:\documents and settings\jeff\my documents\visual studio 2005\projects\week 6 homework\week 6 homework\state abbreviations.cpp(18) : error C2065: 'ga' : undeclared identifier
c:\documents and settings\jeff\my documents\visual studio 2005\projects\week 6 homework\week 6 homework\state abbreviations.cpp(18) : error C2065: 'FL' : undeclared identifier
c:\documents and settings\jeff\my documents\visual studio 2005\projects\week 6 homework\week 6 homework\state abbreviations.cpp(18) : error C2065: 'fl' : undeclared identifier
c:\documents and settings\jeff\my documents\visual studio 2005\projects\week 6 homework\week 6 homework\state abbreviations.cpp(19) : error C2065: 'AL' : undeclared identifier
c:\documents and settings\jeff\my documents\visual studio 2005\projects\week 6 homework\week 6 homework\state abbreviations.cpp(19) : error C2065: 'al' : undeclared identifier
Build log was saved at "file://c:\Documents and Settings\Jeff\My Documents\Visual Studio 2005\Projects\week 6 homework\week 6 homework\Debug\BuildLog.htm"
week 6 homework - 10 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========