Hey all, i hope you can help me, ive just started a new course and im into my second term and the C++ is getting harder and harder (ive never done it before). Please dont critisize my code its a mixture of mine and my lecturers and he loves C so theres a bit of C in it (which i just about understand!) He's adamant C is better but im finding it hard to pick up both so im trying to stick to C++ for the time being.
Anyway, the question is given at the top of the code, it all compiles and runs but when i enter the function to determine the highest and lowest value they keep giving me the wrong answer.
Code:
/*Write a program which reads in names and marks from a file,
the program should read names until a sentinel value (e.g. "****") is read.
The marks will have a maximum of 100.
Write functions to calculate the average mark and to print the name of the
student with the best mark in the class.
*/
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
char **FirstArray;
int **MarkArray;
const int value = 10;
int i = 0;
int counter = 0;
int lowest, k;
int minimum(int *marks, int size);
int maximum(int *marks, int size);
float average(int *marks, int size);
int main()
{
char name[20];
char markStr[20];
int mark;
char *names[20];
int marks[20];
FILE *myFile;
myFile = fopen("Names.txt","r");
if(!myFile)
{
cout << "File Not Found"; // simple check to show file has been found
}
fscanf(myFile,"%s",name); //Reads data from the stream and stores them according to the parameter
//format into the locations pointed by the additional arguments.
while (strcmp(name,"****") )
{
fscanf(myFile,"%d",&mark);
//fgets(markStr, 20, myFile); // string with no /0 and a /n at the end
//sscanf(markStr,"%d",&mark); // string with a /0 at the end
printf("%s \n",name);
printf("%d \n",mark);
//put these into arrays
names[i] = new char[20];
strcpy(names[i],name);
marks[i] = mark;
i++;
counter++;
fscanf(myFile,"%s",name);
}
cout <<"\nThe average value of this array is: "<< average(marks, counter)<<endl;
cout << names[minimum(marks, counter)] << " Had the Lowest mark with " << marks[minimum(marks, counter)] <<endl;
cout << names[maximum(marks, counter)] << " Had the Highest mark with " << marks[maximum(marks, counter)] <<endl;
for(int j=0;j<i;j++)
{
delete(names[j]);
}
fclose(myFile);
return 0;
}
float average(int *marks, int size)
{
float sum = 0.0f; // Accumulate total in here
for(int k = 0; k < size; k++)
{
sum += marks[k]; // Sum array elements
}
return sum/size; // Return average
}
int minimum(int *marks, int size)
{
int lowest= marks[0];
for(int k = 0; k < size; k++)
{
if(lowest > marks[k])
lowest = marks[k];
}
return k;
}
int maximum(int *marks, int size)
{
int highest = marks[0];
for(int k = 0; k < size; k++)
{
if(marks[k]>lowest)
highest = marks[k];
}
return k;
}
Then all thats in the .txt file is:
Code:
Sion
60
Fred
56
Bill
65
Ted
45
Joe
78
****
Please someone help me before i go insane!! I just dont see the problem because ive used the functions before for another assignment question!!
Any help is appreciated also as i said im new to this so can you talk simply to me!