help please

Pages: 12
i have project and the time remaining to due is day and half and iam really stuck i would appreciate some help this is the project :


Write a menu driven program with the following options:
It opens and reads from a data file id’s and grade and name. The number of lines of input is unknown. It will store them in 3 parallel arrays (ids, grades, and names). Make the ids array of type long to handle large integer values.

Then it will show a menu of choices:  Add a grade (reads the id, grade and corresponding name from the user). It will add the grade and name only if the id read does not exist already among the id’s present in the array.
Find a grade given an id i.e. your program will read the id from the user and find the corresponding grade.
 Delete a user from the 3 arrays (id, grade, and name)
 Sort the 3 arrays according to the id (in increasing order)
 Sort the 3 arrays according to the grades from low to high
 Sort the 3 arrays according to the grades from high to low.  Finds the average
 Saves the id’s and grades in the file.
 Sort the 3 arrays according to names alphabetically (Bonus)
Practice modular programming by writing functions whenever possible. Follow the convention concerning variable names. Insert comments wherever needed.
What do you need help with? Def not doing your whole homework for you mate
how to read the names id grades when the number of studnts is unlimited ? when the loob well stop ?
#include<stdio.h>
#include<math.h>
#include<string.h>



int main (){
FILE *input , *output ;
int i;
input = fopen("input.txt","r");
output = fopen("output.txt","w");

if(input == NULL){
printf("No input file found ");
exit(1);
}
output = fopen("output.txt","w");


int id[],grade[] ;
char name[];

while(fscanf(output,"%d%d%c",&id[i],&grade[i],name[i]) !=EOF){


this is where i reached and iam confused in reading the data
Do you mean like you don't know the size of the file?

i think you can just read the whole file with

ifstream f;
f.open("");
while (!f.eof())

but this just keeps reading from the file until it has reached an end of flag in the text file
ifstream f;
f.open("");
while (!f.eof())

this writing is strange we didnt used it in the course and we use dev c++ and sections is aray data file strings loops but i didnt see this ifstream f;
well ifstream and ofstream are used to read in or write to respectively, so my loop opens your file and reads by line until it reaches the end

so you need to store the data in an array of strings?
iam not fimliar with (ifstream) we didnt use it in the course so icant use it in the project is there another way of to store arrays and string in data file with no specific number for the data that will be entered
1
2
int id[],grade[] ;
char name[];


Given your assignment requirements you are going to need to use dynamic arrays, which these are not. (In fact, arrays without specified sizes are not legal in this context in C and never legal in C++.)

http://stackoverflow.com/a/4352822
int id[80],grade[80] ;
char name[80];
for(i=0;fscanf(input," %s ",&name[i])!=EOF;i++){

printf(" %s ",&name[i]);

}

when i do it like that it works and the names comes as i put them in th file but when i do it like this

int id[80],grade[80] ;
char name[80];
for(i=0;fscanf(input,"%d %s",&id[i],&name[i])!=EOF;i++){

printf(" %d %s ",&id[i],&name[i]);

}



the id numbers that i entered do not come out instead comes numbers like that 2357849 but the names comes correct as i put them in the file so whats wrong ?
printf(" %d %s ",&id[i],&name[i]);

Get rid of the ampersand in front of id.
Last edited on
( It will store them in 3 parallel arrays (ids, grades, and names). )

it means that is store them in other file ? and how to store them as parallel array ?
Is that C or C++? It kinda looks like you are doing it all under C.
it is c++
Are you allowed to create your own custom classes or borrow classes from the Standard Template Library (I.E. #include vector)
Last edited on
sorry didnt understand the question how use classes or objects ?
So do you know the maximum number of IDs / Grades / Names read from the file? If so, that makes things simpler.

Will you ever have more than 80 grades?
Last edited on
This looks like C, not C++. I just finished my Operating Systems class, and the whole thing was in C. This is C, not C++.

What does your input file look like? Reading from files in C is not as graceful as it is in C++.
His class hasn't taught him about Object-Oriented Programming at all. I'm guessing they're calling it "C++" but they are excluding all OOP whatsoever.
That's fine. It's not really a C++ class without the OOP, but whatever.

This is my simple code to read from a file:

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
#include <string.h>
#include <math.h>
#include <stdio.h>

#define MAX_BUF 1024

int main()
{

	FILE *input = NULL;
	FILE *output = NULL;

	long ID = 0;
	int grade = 0;
	char name[MAX_BUF];

	input = fopen("file.txt", "r");

	if (input == NULL)
	{
		printf("UGHHH WHYYYYYY: %s", strerror(errno));
		exit(-3);
	}

	while (fscanf(input, "%li %d %s", &ID, &grade, &name) == 3)
		printf("\nID: %li, Grade: %d, Name: %s", ID, grade, name);
	fclose(input);

    return 0;
}


the text file looks like this:

6435435 45 Name1
6543565 92 Name2
1321321 75 Name3


There's something about writing C code in Visual Studio that upsets me... I really don't like it... I'd rather use Sublime Text and Make on my Linux machine than write C in Visual Studio.
Last edited on
Pages: 12