Problem With Random Number Generator

I am a college student taking a C++ class, and was given this assignment:
Goal: Write a program that will make use of your knowledge of pointers to build a sorted linked list. The program should include the
following:
1. Create a struct with the following items: name (string), score (int) and grade (char).
2. Create a node and fill it by doing the following:
a. read names from in.dat data file (attached)
b. Generate a score for each node from 0 to 100. This field of the struct should be randomly generated by using the
rand function.
c. Assign a grade to each node as the following: 0-59: F; 60-69: D; 70-79: C; 80-89: B and 90-100: A.
3. Build a linked list of the nodes according to ascending order of score.
4. Print the listing of students/score/grade to the screen in row/column format. There should be proper t so that reader can tell
what each column representing for.

I'm not really having a problem. The function is able to assign a random grade to each student, however when I run the program, it assigns the same grade to each student. How can I make the program assign a different grade to each student?

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//main.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <time.h> 
#include "P4-linkedList_Elliott.h"

using namespace std;

//Default constructor
linkedList::linkedList()
{
   head = NULL;
} 

//Function to create node
node* linkedList::createNode(string name)
{
   node* newNode = new node;
   newNode->name = name;
   newNode->score = getScore();
   newNode->grade = getGrade(newNode->score);
   return newNode;
}

//Function to get student's letter grade
char linkedList::getGrade(int score)
{
   if (0 <= score && score <= 59)
       return 'F';
   else if (60 <= score && score <= 69)
       return 'D';
   else if (70 <= score && score <= 79)
       return 'C';
   else if (80 <= score && score <= 89)
       return 'B';
   else if (90 <= score && score <=100)
       return 'A';
}

//Function to generate random number for student's number grade
int linkedList::getScore()
{
	time_t secs;

	time(&secs);

	srand((unsigned int) secs);
  
	return rand() % 100+0;
}

//Function to create a line for each student
void linkedList::create(string name)
{
	node* newNode = createNode(name);   
	newNode->next = NULL;

if (head == NULL || head->score >= newNode->score )
	{
		newNode->next = head;
		head = newNode;
		return;
	}
	
else if (head->next != NULL && head->next->score >= newNode->score)
	{
		newNode->next = head->next;
		head->next = newNode;
		return;
	}
	
else
	{
		node * left;
		node * right = head;	
	
while(right != NULL && right->next->score <= newNode->score)
	{
		left = right;
		right = right->next;
	}
		left->next=newNode;
		newNode->next = right;
	}
}
//Function to print the list as a table
void linkedList::print()
{
	node *n = head;
	while (n != NULL)
{
			cout.width(30);
			cout << n->name;
			cout.width(25);
			cout << n->score;
			cout.width(25);
			cout << n->grade << endl;
			n = n->next;  
}
}		

int main()
{
	//Code to read from file
   string line;
   
   ifstream in_file;
   
   linkedList list;
   
   in_file.open( "infile.dat");
   
   while (std::getline(in_file, line))
   {  
       list.create(line); //Function call to create line for each student
   }
   //Header of the table
   			cout.width(30);
   			cout << left;
			cout << "Student Name";
			cout.width(25);
			cout << "Score";
			cout.width(25);
			cout << "Grade" << endl;
   list.print(); //Function call to print the table of student's names and grades
     
   return 0; 
}

// header file
#include<iostream>
using namespace std;

//Definition of the node
struct node
{
	string name; // Variable to hold the student's name
	int score; // Variable to hold the student's number score
	char grade; // Variable to hold the student's letter grade
	node *next; // Declaring pointer *next
};

//Definition of linkedList class to create iterators
//to objects of the linkedList class

class linkedList
{
   private:
       node *head; 
       node* createNode(string name);
       char getGrade(int score);
       int getScore();
   public:
       linkedList();
       void create(string name);
       void print();
  
};

This is what prints on the screen:
Student Name                  Score                    Grade
Woods Marcus                  99                       A
Wilson Delora                 99                       A
Thompson Fernando             99                       A
Smith Devin                   99                       A
Robinson Cordale              99                       A
Ricks Ezra                    99                       A
Price Gerald                  99                       A
Powell Kiara                  99                       A
Nagal Jimmy                   99                       A
Mirador GeoAlbert             99                       A
Longs Chatwaneice             99                       A
Jones Kobe                    99                       A
Johnlouis Ajayla              99                       A
Holley Deyana                 99                       A
Holden Tre                    99                       A
Harris Dontel                 99                       A
Hardy Ashley                  99                       A
Elliott Mysia                 99                       A
Chapman Thomas                99                       A
Carrion Antonio               99                       A
Andrews-Graham D'Kyra         99                       A 
You should only call srand() once, at the start of the program.
You need to seed the random number generator once instead of each time you call rand(). Just move lines 48-50 to line 113.
mysiarobin1987, my comment should have been to move lines 46-50, not lines 48-50.

Once you fix that, you'll find that linkedList::create() doesn't work. You didn't see this before because you were always inserting at the start of the list. At line 80, right might not be NULL, but right->next might.

But rather than fix it, you should rewrite create(), it's much more complex than necessary. Here is a more traditional way of doing it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Function to create a line for each student
void
linkedList::create(string name)
{
    node *newNode = createNode(name);

    node *cur, *prev = NULL;
    for (cur = head; cur; prev = cur, cur = cur->next) {
        if (cur->score >= newNode->score) {
            break;
        }
    }
    newNode->next = cur;
    if (prev) {
        prev->next = newNode;
    } else {
        head = newNode;
    }
}


The better way is to take advantage of the fact that C++ let's you take the address of a variable. Instead of a prev pointer, create a variable that points to head, or cur->next:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Function to create a line for each student
void
linkedList::create(string name)
{
    node *newNode = createNode(name);

    node **pp, *cur;
    for(pp = &head; (cur = *pp); pp = &(cur->next)) {
        if (cur->score >= newNode->score) {
            break;
        }
    }
    newNode->next = cur;
    *pp = newNode;
}
THANK YOU SO MUCH AGAIN!! THAT REALLY HELPED !! @DHAYDEN
Topic archived. No new replies allowed.