Hi everyone, I'm new to C++ programming. I got a task to sort students mark in ascending order using bubble sorting. The marks are given in the text file. I had try to solve it but error still occurred and i don't know how to solve the errors. Hoping anyone can help me. Thanks for your kindness.
Here is the text file. (first column indicate matricNum, second column marks and third are gender)
001 65.5 2
002 56.7 2
003 35.8 1
004 42 2
005 49.4 1
006 55.1 1
007 87.5 2
#include "stdafx.h"
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
double numb[7];
int i, j;
double temp;
int matricNum;
double marks;
int gender;
// store data into array.
{
// open the file for input
ifstream inputFile("myFile.txt");
// the input file was opened
for( int i = 0; i < 7; i++ )
inputFile >> matricNum[i] >> marks[i] >> gender[i];
// the file is closed
}
//sort marks in ascending order using bubble sort
for (i=0;i<=7;i++)
{
for (j=i+1;j<=6;j++)
{
if (marks[i] > marks[j])
{
temp = marks[i];
marks[i] = marks[j];
marks[j] = temp;
}
}
}
for (i=0;i<=6;i++)
{
cout << matricNum << marks[i] << gender << endl;
}
system("pause");
}
In function 'int main()':
24:33: error: invalid types 'int[int]' for array subscript
24:45: error: invalid types 'double[int]' for array subscript
24:58: error: invalid types 'int[int]' for array subscript
36:15: error: invalid types 'double[int]' for array subscript
36:26: error: invalid types 'double[int]' for array subscript
38:19: error: invalid types 'double[int]' for array subscript
39:12: error: invalid types 'double[int]' for array subscript
39:23: error: invalid types 'double[int]' for array subscript
40:12: error: invalid types 'double[int]' for array subscript
47:31: error: invalid types 'double[int]' for array subscript
10:9: warning: unused variable 'numb' [-Wunused-variable]
I believe your issue is that you never actually use your numb array.
Your error is that you are attempting to use matricNum as an array (by accessing it as matricNum[i]), but matricNum is simple an int.
Perhaps you meant to use numb instead of matricNum?
Similarly, marksis not an array.
If you want it to be an array, declare it as such,
perhaps double marks[7];
Last, for (i=0;i<=7;i++)
Please note that if your array has 7 elements in it, accessing my_array[7] is out of bounds of the array, because arrays start at 0.
I would change the <= to just <.