Enumerations and my mini-project.

I just have a little project that takes your name, age and a grade and than spits it back at you, but the grade is one lower than you wanted. (Ie You want an A, it gives you a B). Everything works but the grade part, I tried it with enumerations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
char * grade() {                
	enum grade{A = 5, B = 4, C = 3};
	char temp;
	cout << "Please enter your wanted grade:";
	cin >> temp;
	char * gr = new char (temp - 1);
	return gr;}
//random functions...


int main() {
char * firstnamec;     
firstnamec = firstname();
char * lastnamec;        
lastnamec = lastname();  
char * gradec;           
gradec = grade();
char * agec;         //here
agec = age();        //here
cout << "Name:" << lastnamec << "," << firstnamec;
cout << "Recieved Grade:" << gradec;
cout << "Age:" << agec;
return 0;
Last edited on
Why are you using pointers when you are just using a single char. You have not cleared any of the memory allocated with new creating potential memory leaks.

 
char * gr = new char (temp - 1);


should be:
1
2
  char *gr = new char[1];
  gr[0] = temp;


You could use
 
char * gr = new char (temp);


also. Because the contents of ( ) are the value to set it to. Not how big it's suppose to be. It will also implicitly only create a 1 length char-array.
Last edited on
I know how I could do it much easier, but there is a work problem that wanted me to do this pointers and enumerators.

To put it into perspective, I was trying to mold this code, but for the "grade"
1
2
3
4
5
6
7
char * firstname() {
	char temp[30];
cout << "Please enter your first name:";
cin.get(temp, 30).get();
char * fn = new char[strlen(temp) + 1];
strcpy(fn, temp);
return fn;
Last edited on
always remember to delete or delete [] any memory you allocate with new. If you don't then thats memory that will be lost when the pointer is moved or de-referenced.
I still can't get the grade to go down a level from what the user imputed. A->B. How would I go about that? Enumerators wise.
You want to do it in an enumeration? What would happen if they entered 'C'?
I haven't put D = 2 or F = 1 yet, I've just been testing it with 'A' until I can get that working.
Last edited on
1
2
3
4
5
6
7
8
9
10
char Result;
cin >> temp;
switch(temp) {
case A:
 Result = 'B';
 break;
case B:
 Result = 'C'
 break;
}


That'd be the easiest way I can think of. You have to remember that when you enter 'A' into the console the computer sees that as 'A'. But when your enum is compiled the enum A no longer exists. Thats converted to 5. So You'd have to do a manual comparison, or some math comparison to check 'A' against A etc.
I'll try that out, thanks. I probably made this whole practice excercise a lot harder than it should have been.
Topic archived. No new replies allowed.