My code is to read from a file, the id, maths score and English score and determine each of their grades using a function.
How do I store the grades in memory using pointers and then print them by accessing the memory locations referenced by the grade pointers? I'm very new to pointers and I'm just trying to get an understanding of how and why it is needed
#include <iostream>
#include <fstream>
usingnamespace std;
char gradeSearch(int score){
int i;
char g;
if (score<50)
g='F';
if ((score>=50) && (score<=59))
g='D';
if ((score>=60) && (score<=79))
g='C';
if ((score>=80) && (score<=89))
g='B';
if ((score>=90) && (score<=100))
g='A';
return g;
}
int main(){
ifstream in;
ofstream out;
int eng[10];
int math[10];
int id;
int s1;
int s2;
char g1;
char g2;
int amt;
int h1;
int h2;
int sCounter1;
int sCounter2;
int total1;
int total2;
double ave1;
double ave2;
amt=0;
s1=0;
s2=0;
h1=0;
h2=0;
total1=0;
total2=0;
ave1=0.0;
ave2=0.0;
sCounter1=0;
sCounter2=0;
in.open("scores.txt");
cout<<"Id\tMaths\tGrade\tEnglish\t Grade"<<endl;
cout<<"-------------------------------------"<<endl;
in>>id;
while(id!=0){
in>>s1;
in>>s2;
total1=total1+s1;
sCounter1=sCounter1+1;
total2=total2+s2;
sCounter2=sCounter2+1;
if (s1>h1)
h1=s1;
if (s2>h2)
h2=s2;
g1=gradeSearch(s1);
g2=gradeSearch(s2);
cout<<id<<"\t "<<s1<<"\t "<<g1<<"\t "<<s2<<"\t "<<g2<<endl;//"\t";
amt=amt+1;
in>>id;
}//eof
ave1=total1/(sCounter1*1.0);
ave2=total2/(sCounter2*1.0);
cout<<endl;
cout<<"The total number of candidates are: "<<amt<<endl;
cout<<"The highest mark in Mathematics is: "<<h1<<endl;
cout<<"The highest mark in English : "<<h2<<endl;
cout<<"The average mark in Mathematics is: "<<ave1<<endl;
cout<<"The average mark in English is : "<<ave2<<endl;
cout<<endl;
return 0;
}
a good and fair question!
first, modern c++ greatly reduces the need for pointers in your code because tools in the language do most of this low level stuff for you. You still need them from time to time, but the allocation and deallocation of memory manually is not as critical as it once was. It is not uncommon for fairly large blocks of code to have no explicit pointers at all now; 25 years ago, that was nearly unheard of.
your program has a small amount of memory allocated to it. This is actually pretty large in bytes but small in terms of % of total available possible memory you could use. If you tried to make an array of a billion doubles, for example, your compiler might say:
x.cpp:19:22: error: size of array 'd2' is too large
this is because the stack (the memory your program was given to use) is not that big.
A pointer can go around this; it asks for MORE memory in a different location that you can use. (For some unholy reason this is called the heap and the other the stack, and the names stuck despite being virtually meaningless descriptions)
so one place where you need pointers is when you run out of stack memory, which happens pretty quickly in commercial sized software. This sort of issue is solved by vector and other containers though!
Another use of pointers it to string data together into oddly "shaped" containers. Here again, this has been done for you, but imagine a family tree type structure. Pointers link parents and children nicely.
There are other uses of them, but these 2 illustrate some basic ideas of why they are needed. Hopefully your class will get to these ideas, but most of the early examples don't make any sense because you can do the things asked of you in much simpler ways, it seems like a useless complexity at first.