cant output doubles

For whatever reason, whenever I try to output a double, the program crashes and I have no idea why.

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
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cstring>
using namespace std;

struct Student
{
       char name[NAME_SIZE]; // Student name
       int idNum; // Student ID number
       int *tests; // Pointer to array of test scores
       double average; // Average test score
       char grade; // Course grade
};

// Function prototypes
Student *initArrays(int, int);
void getInfo(Student [], int, int);
void showInfo(Student [], int, int);

//*****************************************************
// Function getInfo *
// This function populates the Student array s with *
// data entered by the user. The paramater ns is the *
// number of students and nt is the number of tests. *
//*****************************************************

void getInfo(Student s[], int ns, int nt)
{
     //define variables as needed
     double test = 0, numTests = nt, total = 0, *average;
     int testScores[nt], *ptr[ns];
     char name[45];  
     
     for(int i = 0; i < ns; i++)
     {
             average = &s[i].average;
             ptr[i] = s[i].tests;
             // Get the data for each student.
             cout << "Enter student #" << i+1 << "'s name: ";
             cin.ignore(1, ' ');
             cin.getline (name, 45);
             
             int k = 0; 
             while (name[k] != '\n')
             {                                      
                  s[i].name[k] = name[k];
                  k++;
             }

             // Get the ID number.
             cout << "Enter student #" << i+1 << "'s ID Number: ";
             cin >> s[i].idNum;

             for(int j = 0; j < nt; j++)
             {
                     // Get the test scores and accumulate a total.
                     cout << "Enter the score for test #" << j+1 << ": ";
                     cin >> *(ptr[i]+j);                          
             }
             cout << *(ptr[i]) << endl << *(ptr[i]+1) << endl;
             // Calculate the average score.
             for (int l = 0; l < nt; l++)
             {
                 total = (total + *(ptr[i]+l));
             }
             s[i].average = total;
             total = (total / (numTests));//+0.0);
             s[i].average = (s[i].average / numTests);//+0.0);
             
            // *_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*
            // TEST TO SEE IF I CAN GET DOUBLES TO OUTPUT
            // COUT NUMBERS ARE FLAGS TO SEE WHERE THE PROCESS FAILES
            // STATIC CASTING THE DOUBLE AS AN INT (THIS SEEMS TO WORK)
             cout << static_cast<int>(s[i].average) << endl;
             cout << "1"<< endl;
             cout << static_cast<int>(total)<< endl;
             cout << "2"<< endl;
             // TRY TO OUTPUT A DEREFFERENCED POINTER TO THE DOUBLE HOLDING THE AVERAGE TEST SCORE (DOESNT SEEM TO WORK)
             cout << *average<< endl;
             cout << "3"<< endl;
             // TRY TO OUTPUT THE DOUBLES ON THEIR OWN (DOESNT WORK EITHER)
             cout << s[i].average<< endl;
             cout << "4"<< endl;
             cout << total<< endl;
             cout << "5"<< endl;
             // *_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*


             // Assign a letter grade.
             if(s[i].average <= 100 && s[i].average >= 90)
                 s[i].grade = 'A';
             else if(s[i].average < 90 && s[i].average >= 80)
                 s[i].grade = 'B';
             else if(s[i].average < 80 && s[i].average >= 70)
                 s[i].grade = 'C';
             else if(s[i].average < 70 && s[i].average >= 60)
                 s[i].grade = 'D';
             else if(s[i].average < 60)
                 s[i].grade = 'E';
     }
}

any help would be greatly appreciated.
Last edited on
Are you sure the logic for this calculation is correct?
1
2
3
             s[i].average = total;
             total = (total / (numTests));//+0.0);
             s[i].average = (s[i].average / numTests);//+0.0); 

I changed it to this

 
s[i].average = (total / (2.0)); // used 2.0 for testing 


it still crashes once it gets to the cout of any of the doubles.
Last edited on
1
2
3
s[i].average = total;
 total = (total / (numTests));//+0.0);
 s[i].average = (s[i].average / numTests);//+0.0) 

OR
s[i].average = (total / (2.0)); // used 2.0 for testing

why don't u dereference this?
*(s[i].average) = (total / (2.0)); // used 2.0 for testing

where is int *tests defined?
*tests is the only pointer, s[i].average is a double, so derefrencing it wouldn't do any good

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
// this is where *tests is defined
struct Student
{
       char name[NAME_SIZE]; // Student name
       int idNum; // Student ID number
       int *tests; // Pointer to array of test scores
       double average; // Average test score
       char grade; // Course grade
};


// this is where the array for the tests to be held is created
Student *initArrays(int s, int t)
{
        Student *ptr;

        // Allocate the array of Student structures.
        ptr = new Student[s];

        // Allocate an array of ints (to hold test scores)
        // for each element of the array of Student structures.
        for (int count = 0; count < s; count++)
        {
            ptr[count].tests = new int[t];
        }
        // Return a pointer to the array of structures.
        return ptr;
}
does anyone have an idea? I need to have this finished by tonight.
Topic archived. No new replies allowed.