Stack + Template

I've been trying to create a system where you can enter in the student name(will do the name later)and grades for each class, but I'm having trouble with displaying the grades correctly in the program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 #include<iostream>
#include<string>
#include <stack>
using namespace std;

template <class T>
class grades
{
public:
	grades();
	void input(T);
	string getname() { return name; }
private:
	T* arr;
	string name;
	int size = 4;
	int top;
};

template <class T>
grades<T>::grades()
{
	arr = new T[size];
	top = -1;
}

template <class T>
void grades<T>::input(T grade)
{

	cout << "Enter in the grades for the following: " << endl;
	cout << "----------------------------------------" << endl;
	cout << "Math, English, History, Science" << endl;
	cout << "----------------------------------------" << endl;
	for (int i = 0; i < size; i++)
	{
		cin >> grade;
		arr[++top];
	}
	for (int i = 0; i < size; i++)
	{
		cout << arr[grade];//tried to display grades here
	}
}

int main()
{
	char anwser;
	int grade = 0;
	do {
		grades<int>enter;
		enter.input(grade);
		cout << "wish to keep going?(Y/N): ";
		cin >> anwser;
	} while (anwser == 'Y' || anwser == 'y');
}
arr[++top]; // what do you think this does. I can tell you, it does nothing except increment top, but what did you WANT it to do? Maybe you meant arr[++top] = grade; ??

arr[grade]; //this is going to explode, most likely, unless the grades a from 0-3 in value.
perhaps you meant arr[i] here.


answer is spelled wrong consistently.

where is your stack? You don't seem to have one, yet? You call the array of grades a stack, by giving it a 'top', but that makes no sense, since its an ordered list of grades. It looks like you have badly missed something here... eg it looks like from here that you would have a stack of students each with a list of grades for each subject. Or, if you just have 1 student with a stack of grades, you may have completely misunderstood what that would be about. An array and an index is one way to do a simple stack, but it will lack many of the standard stack concepts/methods. Just double check your assignment, you may not be answering the problem.

Last edited on
Oh wait I just figured it out. Thank you for you input I knew I was missing something obvious. It was the arr[++top] = grade; I'm a little shabby still with coding.
Replace lines 35-43 with:
35
36
37
38
39
40
41
42
43
44
45
46
	for (int i = 0; i < size; i++)
	{
		cin >> grade;
		arr[i] = grade;
	}
	std::cout << '\n';

	for (int i = 0; i < size; i++)
	{
		cout << arr[i] << ' ';//tried to display grades here
	}
	std::cout << "\n\n";

Enter in the grades for the following:
----------------------------------------
Math, English, History, Science
----------------------------------------
1 2 3 4
1 2 3 4

wish to keep going?(Y/N): y
Enter in the grades for the following:
----------------------------------------
Math, English, History, Science
----------------------------------------
3 4 5 6
3 4 5 6

wish to keep going?(Y/N): n

You are getting a grade value from the user at line 37 and then doing nothing with it.

Line 38 increments through your array for no purpose, no valid value in the elements anyway.

Line 42 prints the same element, likely out-of-bounds depending on the last grade entered. No valid values in your array to output, so you get garbage displayed.
answer is spelled wrong consistently.

As long as every instance of the variable uses the same mis-spelling, then is it really wrong spelling? :)
Topic archived. No new replies allowed.