Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

steelphantom

macrumors 6502a
Original poster
Oct 15, 2005
555
1
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:

PHP:
#include <iostream>

int main()
{
	using namespace std;
	char firstname[25];
	char lastname[25];
	enum grades {A = 4, B = 3, C = 2, D = 1};
	grades yourgrade;
	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? ";
	cin >> yourgrade;
	cout << "What is your age? ";
	cin >> age;
	
	yourgrade = grades(yourgrade - 1);
	
	cout << "Name: " << lastname << ", " << firstname << endl;
	cout << "Grade: " << yourgrade << 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. :eek:
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
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

macrumors 6502a
Original poster
Oct 15, 2005
555
1
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

macrumors 6502a
Jun 17, 2003
873
3
steelphantom said:
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

macrumors 601
Jul 18, 2002
4,402
11
toronto
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

macrumors 6502a
Original poster
Oct 15, 2005
555
1
zimv20 said:
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!
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
steelphantom said:
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

macrumors 6502a
Original poster
Oct 15, 2005
555
1
bousozoku said:
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. :eek:
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
steelphantom said:
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. :eek:

"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

macrumors 6502a
Original poster
Oct 15, 2005
555
1
bousozoku said:
"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

macrumors 601
Jul 18, 2002
4,402
11
toronto
bousozoku said:
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:

Code:
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

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
zimv20 said:
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:

Code:
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

macrumors 601
Jul 18, 2002
4,402
11
toronto
bousozoku said:
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

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
zimv20 said:
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

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
caveman_uk said:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.