Problem using Structures, Arrays, and Functions

Hello, I don't usually ask for help, but I'm in need of assistance. I'm working on a homework assignment that involves using a Structure to organize, input and view recorded data. I've been asked to make a menu and what-not, but I'm having problems in Inserting the Student Name, it just skips the input process and moves straight to the next input prompt. I'm also having a problem creating a program that will collect the inputed data for a few numbers and give the average. Anyone who can correct me would be most appreciative.


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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <stdio.h>
using namespace std;

const int NAME_LENGTH = 30; //Name length
int i;

struct StudentRecord
{
char name[NAME_LENGTH];
int id;
int score[4];
double grade;
};
StudentRecord mystu[3];
int d;
int main()
{
    char stu;
    char sel;
    int Grade;
    do {
cout << "========================================\n";
cout << "1. Enter student data for the class\n";
cout << "2. Print all student records\n";
cout << "3. Print a single student record\n\n";
cout << "0. Exit\n";
cout << "========================================\n";
cout << "Enter selection: ";
    cin >> sel;

switch (sel) {
    case '1':
        {
            for (d=0; d<3; d++)
            {
        cout << "Enter the Student name:\n";
        cin.getline(mystu[d].name, NAME_LENGTH);
        cout << "Enter student ID:\n";
        cin >> mystu[d].id;
        cout << "Enter four exam scores:\n";
        for (i=0; i<4; i++)
        {
           cin >> mystu[d].score[i];
        }
            }

        break;
        }
    case '2'://print all student records //50: previously used here
        {
            void grade(mystu[d].score[i]);  //52:  variable or field ‘grade’ declared void
            printf ("Name %-10d ID %10d Score %15d Grade");
            for (d=0; d<3; d++)
            {
        printf ("%10c %10d %10d %10f",mystu[d].name, mystu[d].id, mystu[d].score[i], mystu[d].grade );
            }
        break;
        }
    case '3'://search for a specified record. //60: previously used here
        {
            cout << "Select which student you wish to view (1-3)\n";
        cin >> stu;
        switch (stu)
            case '1':
        {
            printf ("%10c %10d %10d %10f",mystu[0].name, mystu[0].id, mystu[d].score[i], mystu[0].grade );
             break;
        }
            case '2': //70: duplicate case value
        {
            printf ("%10c %10d %10d %10f",mystu[1].name, mystu[1].id, mystu[1].score[i], mystu[1].grade );
             break;
        }
            case '3': //75: duplicate case value
        {
            printf ("%10c %10d %10d %10f",mystu[2].name, mystu[2].id, mystu[2].score[i], mystu[2].grade );
             break;
        }
            default: //80: this is the first default label
             cout << "unknown command\n";

         break;
        }
    case '0'://leave the program
        {
           cout << "Goodbye\n";

        break;
        }

    default: //92: multiple default labels in one switch
        cout << "unknown command\n";
             }

   }while (sel != '0');

return 0;
}
//Function grade
void grade (mystu[3].score[i]) //101:variable or field ‘grade’ declared void
{
for (i=0; i<4; i++)
 {
        grade = (score[0]+score[1]+score[2]+score[3])/4
 }
}


Last edited on
The nested switch in case '3' is missing its open/close braces.

This:
1
2
3
4
5
6
7
void grade (mystu[3].score[i]) //101:variable or field ‘grade’ declared void
{
for (i=0; i<4; i++)
 {
        grade = (score[0]+score[1]+score[2]+score[3])/4
 }
}
Should be:
1
2
3
4
double grade(int score[4])
{
    return (score[0]+score[1]+score[2]+score[3])/4;
}


This:
1
2
3
4
5
6
void grade(mystu[d].score[i]);  //52:  variable or field ‘grade’ declared void
printf ("Name %-10d ID %10d Score %15d Grade");
for (d=0; d<3; d++)
{
printf ("%10c %10d %10d %10f",mystu[d].name, mystu[d].id, mystu[d].score[i], mystu[d].grade );
}
Should be:
1
2
3
4
5
6
printf ("Name      ID        Grade");
for (d=0; d<3; d++)
{
    mystu[d].grade = grade(mystu[d].score[i]);
    printf("%10s %10d %10f",mystu[d].name, mystu[d].id, mystu[d].grade);
}

I've take the score out, you'll need to decide what you want to do with it and put it back in somewhere.
Last edited on
Topic archived. No new replies allowed.