Please Help, Sorting Program

I've created this program and it is wrong. It is written for this assignment.
(Sorting) Read a set of numerical grades from the keyboard into an array named grades. The maximum number of grades to be entered is 50, so the size of the array should be 50 elements, and data entry should be terminated when a negative number is entered. Have your program invoke a sort() function to sort the grades and then display the sorted grades in ascending order (lowest to highest). You are free to use any of the sort functions given in the textbook. Remember to declare your sort() function with a function prototype.


This is the code and the error occurs on lines 18 and 21:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

void sort(int grades[], int size);

int main()
{
int size;
int grades[50], flag = 0, num;
int i, count = 0, temp;

cout << "Please enter the grades to sort: " << endl;

for (i = 0; (i < size && flag != 1); i++)
{
cin >> size;
if (num < 0){
cout << "You entered a negative number." << "The program will now end." << endl;
flag = 1;

cin.ignore();
}
else
{
grades[i] = num;
count++;
}
sort(grades, count);
for (int i = 0; i< size; i++)
cout << grades[i] << " ";
}
cout << endl;

cin.get();
return 0;
}
void sort(int grades[], int size)
{
int i, j, temp;
for (i = 0; i < size; i++)
{
for (j = 0; j < size - i - 1; j++)
{
if (grades[j] > grades[j + 1])
{
temp = grades[j];
grades[j] = grades[j + 1];
grades[j + 1] = temp;
}
}
}
}
Please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/

the error occurs on lines 18 and 21

Which of the lines above are 18 and 21 and what are the exact error messages?

You code could look like:
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
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

void sort(int grades[], int size);

int main()
{
  int size;
  int grades[50], flag = 0, num;
  int i, count = 0, temp;

  cout << "Please enter the grades to sort: " << endl;

  for (i = 0; (i < size && flag != 1); i++) // size is uninitialized
  {
    cin >> size; // you overwrite the size
    if (num < 0){ // but check num, which is uninitialized
      cout << "You entered a negative number." << "The program will now end." << endl;
      flag = 1;

      cin.ignore();
    }
    else
    {
      grades[i] = num;
      count++; // why both 'i' and 'count'?
    }
    sort(grades, count); // you sort and show result inside the loop, before all data is in array
    for (int i = 0; i< size; i++)
      cout << grades[i] << " ";
  }
  cout << endl;

  cin.get();
  return 0;
}
void sort(int grades[], int size)
{
  int i, j, temp;
  for (i = 0; i < size; i++)
  {
    for (j = 0; j < size - i - 1; j++)
    {
      if (grades[j] > grades[j + 1])
      {
        temp = grades[j];
        grades[j] = grades[j + 1];
        grades[j + 1] = temp;
      }
    }
  }
}
Topic archived. No new replies allowed.