PDA

View Full Version : Enumeration Issues in C++

steelphantom
Feb 2, 2006, 05:16 PM
I'm currently reading SAMS' C++ Primer Plus, and am attempting to do one of the programming exercises. The goal of the program is to ask for the person's first and last names, the grade that they want, and their age. I can do everything easily except for the grade part. The grade is supposed to end up being one letter grade worse than the grade the user inputs. I get a nasty error when I try to use cin to get the value of the person's grade. I'm not allowed to use if statements, switch statements, etc. because I haven't gotten that far in the book and I need to go only on information the book has given me thus far. Without further ado, here's the code:

#include <iostream>

int main()
{
using namespace std;
char firstname[25];
char lastname[25];
enum grades {A = 4, B = 3, C = 2, D = 1};
int age;
cout << "What is your first name? ";
cin.getline(firstname, 26);
cout << "What is your last name? ";
cin.getline(lastname, 26);
cout << "What letter grade do you deserve? ";
cout << "What is your age? ";
cin >> age;

cout << "Name: " << lastname << ", " << firstname << endl;
cout<< "Age: " << age << endl;
return 0;
}

Thanks to anyone who can figure out what I'm doing wrong. I'm sure it's very easy. :o

bousozoku
Feb 2, 2006, 05:22 PM
For what you have, they should be entering 1, 2, 3, or 4 for the letter grade.

Perhaps, if you change

enum grades {A = 4, B = 3, C = 2, D = 1};

to

enum grades {A = 'A', B = 'B', C = 'C', D = 'D'};

it will work the way you want.

steelphantom
Feb 2, 2006, 05:24 PM
Well, the way enumerations work is they assign a number to each value, so, unfortunately, that suggestion won't work. :( The example calls for the user to enter the letter grade, not a number.

mrichmon
Feb 2, 2006, 06:16 PM
Well, the way enumerations work is they assign a number to each value, so, unfortunately, that suggestion won't work. :( The example calls for the user to enter the letter grade, not a number.

First of all, a enumeration is a type declaration. This means that it must happen outside of the main() function. Also, the namespace declaration should be made outside of the main() function... by convention namespace declarations are made on the line immediately after any includes.

zimv20
Feb 2, 2006, 06:18 PM
is it necessary to use an enum for the grades? if not, you could read in a char, check to make sure it's within range, then subtract one from it.

steelphantom
Feb 2, 2006, 06:26 PM
is it necessary to use an enum for the grades? if not, you could read in a char, check to make sure it's within range, then subtract one from it.

You pretty much got it. I just had to read in a char, and add one to it (A to B, B to C, etc.). I knew it was something easy like that! Thanks!

zimv20
Feb 2, 2006, 06:39 PM
ah, *add* one, of course. the school systems have it backwards :-)

bousozoku
Feb 2, 2006, 07:12 PM
Well, the way enumerations work is they assign a number to each value, so, unfortunately, that suggestion won't work. :( The example calls for the user to enter the letter grade, not a number.

Since when is a single character not an integer?

steelphantom
Feb 2, 2006, 07:19 PM
Since when is a single character not an integer?

Well, they are two different data types. As far as I'm aware, you can only assign symbolic constants to integer values, not characters. I could easily be wrong, though. I'm rather horrid at C++, as you can see. :o

bousozoku
Feb 2, 2006, 08:21 PM
Well, they are two different data types. As far as I'm aware, you can only assign symbolic constants to integer values, not characters. I could easily be wrong, though. I'm rather horrid at C++, as you can see. :o

"A" and 'A' are two different data types, but 'A' and 65 (or 0x41) should be the same thing.

In the old days, we would find a characters place in the (ASCII) alphabet by subtracting 'A' from a character, at least in C.

steelphantom
Feb 2, 2006, 08:45 PM
"A" and 'A' are two different data types, but 'A' and 65 (or 0x41) should be the same thing.

In the old days, we would find a characters place in the (ASCII) alphabet by subtracting 'A' from a character, at least in C.

Ah, I see how it would work then. Thanks for increasing my understanding of the matter. It never hurts to know more!

zimv20
Feb 2, 2006, 09:48 PM
In the old days, we would find a characters place in the (ASCII) alphabet by subtracting 'A' from a character, at least in C.
i think you'll enjoy this anecdote from the old days...

i was in a code review meeting, going over a developer's code on a C project, when i saw this uncommented line:

if ((c > '/') && (c < ':'))
{
/* processing */
}

i asked what the heck that was, and i was told it was for checking to make sure the character was a digit. the / and : are one less than 0 and one greater than 9, respectively.

why not use "c >= '0'"? because that was "less efficient", i was told, "it requires two comparison instead of one".

obviously, i objected, first raising the point that comparisons are done through masking and bit-testing, then saying that our compiler had macros for it ("too inefficient" was the response), and finally that such a line, especially uncommented, was a maintenance travesty.

we took a vote, and i lost.

better coding through democracy.

bousozoku
Feb 2, 2006, 10:34 PM
i think you'll enjoy this anecdote from the old days...

i was in a code review meeting, going over a developer's code on a C project, when i saw this uncommented line:

if ((c > '/') && (c < ':'))
{
/* processing */
}

i asked what the heck that was, and i was told it was for checking to make sure the character was a digit. the / and : are one less than 0 and one greater than 9, respectively.

why not use "c >= '0'"? because that was "less efficient", i was told, "it requires two comparison instead of one".

obviously, i objected, first raising the point that comparisons are done through masking and bit-testing, then saying that our compiler had macros for it ("too inefficient" was the response), and finally that such a line, especially uncommented, was a maintenance travesty.

we took a vote, and i lost.

better coding through democracy.

Foolishness is everywhere but not all systems had a full set of branch instructions. That wasn't on an 8080/Z80 system was it? I just pulled out a book and I was noticing the lack of branch operators. JR (Jump Relative) isn't nearly as nice as variety of 6502 operators although the length of the branch was shorter.

zimv20
Feb 2, 2006, 10:44 PM
That wasn't on an 8080/Z80 system was it?
might have been, it was some kind of zilog. they weren't correct in their assertions, i had no trouble using is_digit(). it was an embedded system, we did quite a bit of assembly coding when in the lab, including using jumps to patch in code between builds.

ahhhh, assembly...

caveman_uk
Feb 3, 2006, 02:41 AM
ahhhh, assembly...
You're taking me on a trip down memory lane....Z80 assembly....oooohhhh using LDIR to fill a block of memory with zeros. Only having 48K to play with (of which the system and screen needed about 8K). I've got image resources in my xcode projects that are bigger than 48K:rolleyes:

bousozoku
Feb 3, 2006, 05:08 AM
You're taking me on a trip down memory lane....Z80 assembly....oooohhhh using LDIR to fill a block of memory with zeros. Only having 48K to play with (of which the system and screen needed about 8K). I've got image resources in my xcode projects that are bigger than 48K:rolleyes:

I could do a lot with 48KB but anymore, compiling a "Hello World!" programme takes 12KB.

Z80 assembly wasn't too bad but the lack of memory mapped I/O was a pain. I think our newbie programmers would be hard-pressed to deal with most of it.