I am writing a program for my C++ class but I am getting a lot of errors. We are dealing with the new concept of arrays and structures.
I get errors in my functions regarding undeclared indentifiers and incorrect "lvalue."
************************************************
Code:
#include <iostream.h>
#include <fstream.h>
#include <ctype.h>
#include <cstring>
typedef char str[30];
// This program will read an array of structures from a file, and allow the user to
// Issue commands to list the students, or all grades.
struct studentgrades
{
str name;
char grade;
}s;
// Read file function
// This function should create the studentgrades struct called 's', open a txt file, and stream the
// text file into the struct 's'. The file is then closed.
int Readfile (/*out*/ studentgrades s[], /*in*/ int i)
{
char fn[30];
cout << "Enter file name of student records:" << endl;
cin >> fn;
ifstream infile(fn,ios::nocreate);
i=0;
if (infile)
{while(!infile.eof())
{
infile >> s[i].name >> s[i].grade;
i++;
}
infile.close ( );
}
else cout << "Invalid file. Please enter a valid file name." << endl;
return i;
}
// Sort by name function
// This function should create an
void Sortbyname(/*in*/ int count)
{
str n[count];
for (int i=0; i < count-1; i++)
for (int j=0; j<count-1; j++)
if (strcmp(s[j+1].name, s[j].grade) < 0)
{
n=s[j].name;
s[j].name=s[j+1].name;
s[j+1].name=n;
str g=s[j].grade;
s[j].grade=s[j+1].grade;
s[j+1].grade=g;
};
}
//Sort by grade function
void Sortbygrade (/*in*/ int count)
{
str n[count];
for (int i=0; i< count-1; i++)
for (int j=0; j<count-1; j++)
if (s[j+1].grade < s[j].grade)
{
n=s[j].name;
s[j].name=s[j+1].name;
s[j+1].name=n;
str g=s[j].grade;
s[j].grade=s[j+1].grade;
s[j+1].grade=s[j].grade;
}
}
//List function
void List (/*in*/ int i)
{
char menu;
cout << "List A)lphabetically or by G)rade?" << endl;
cin >> menu;
switch(menu=toupper(menu)){
case 'A':
Sortbyname(int i);
break;
case 'G':
Sortbygrade(int i);
break;
}
for (int i=0; i<=count; i++)
cout << s.name[i] << '\t' << s.grade[i] << endl;
}
//Count grades function
void Grades (/*in*/ int count)
{
int a=0;
int b=0;
int c=0;
int d=0;
int f=0;
char grades;
for (int i=0; i < count; i++)
switch (s.grade[i]=toupper(grades))
{
case 'A':
a++;
break;
case 'B':
b++;
break;
case 'C':
c++;
break;
case 'D':
d++;
break;
case 'F':
f++;
break;
};
cout << "Total number of grade A:" << a << endl;
cout << "Total number of grade B:" << b << endl;
cout << "Total number of grade C:" << c << endl;
cout << "Total number of grade D:" << d << endl;
cout << "Total number of grade F:" << f << endl;
}
//Main function
int main (void)
{
int count=0;
char cmd;
do{
cout << "Please enter a command: R)ead L)ist G)rades Quit" << endl;
switch (cmd=toupper(cmd)){
case 'R':
cout << Readfile(count)<< "records read into memory.";
break;
case 'L':
List(count);
break;
case 'G':
Grades(count);
case 'Q':
break;
default:
cout << "Invalid command.";
}
} while (cmd !='Q');
return 0;
}