Data entry displaying

Write your question here.
Guys is there a chance i can get help with displaying my database i have done inputing in file grades.dat?
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
  #include<stdio.h>
#include<conio.h>
#include<iostream.h>
Exit(){}
inpt(){

		char 	name[30],add[50]/*,
			sect[1],trck[20]*/;
		int 	Mid,Fin;
		int 	Gave;
		FILE * File1;
		File1 = fopen("Grades.Dat","a");
		clrscr();


	gotoxy(33,2);printf("Rizal High School");
	gotoxy(36,3);printf("Senior High");
	gotoxy(12,4);printf("__________________________________________________________");
	gotoxy(34,8);cout <<"Grading System";
	gotoxy(25,9);cout <<"(Multiple Relational Operator)";
	gotoxy(10,11);cout <<"Student Name: ";cin >>name;
	gotoxy(10,12);cout <<"Student Address: ";cin >>add;
	gotoxy(10,13);cout <<"Enter Midterm Grade: ";cin >> Mid;
	gotoxy(10,14);cout <<"Enter Final Grade: ";cin >> Fin;

	  Gave = (Mid + Fin)/2;
	  fprintf(File1,"\n %s %s %d %d %d",name,add,Mid,Fin,Gave);
	gotoxy(10,15);cout <<"Final Grade: ";cout << Gave;

	if(Gave <= 74) {
		gotoxy(10,16);cout <<"Failed!";
		}
	else if(Gave <= 80) {
		gotoxy(10,16);cout <<"Passed!";
		}
	else if(Gave <= 85) {
		gotoxy(10,16);cout <<"Good!";
		}
	else if(Gave <= 90) {
		gotoxy(10,16);cout <<"Very Good!";
		}
	else if(Gave <= 95) {
		gotoxy(10,16);cout <<"Excellent!";
		}
	else if(Gave <= 100) {
		gotoxy(10,16);cout <<"Outstanding!";
		}

	else {
		gotoxy(10,16);cout <<"You're A Legend!";
	  }

	  gotoxy(28,20);printf("Press any key to Continue");
	  getch();
	  }
display(){
}
reset(){
	int ans;
	FILE * File1;
	cout<<"Reset Databases 1-YES 2-NO: ";cin>>ans;
	if(ans == 1){
		File1 = fopen("Grades.Dat","w");
		cout<<"\nDatabase has been DELETED";
		cout<<"\nPress Any Key to Continue";
		getch();
		return 0;
	}
	else
		cout<<"Press 1-Yes 2-No";
		fclose(File1);
		return 0;


}
main()
{ int opt;
  do {
  clrscr();
  cout<<"\n Grading System Database";
  cout<<"\n 1 - Input";
  cout<<"\n 2 - Display";
  cout<<"\n 3 - Reset Database";
  cout<<"\n 0 - Exit Program";
  cout<<"\n Enter Number: ";
  cin>> opt;


  switch(opt){
	case 1: inpt();
		break;

	case 2: display();
		break;

	case 3: reset();
		break;

	case 0: Exit();
		break;
	default:
		cout <<" Invalid Number!\n";
		getch();
		break;
  }
  }
  while(opt!=0);

	cout <<" Thankyou!";
	getch();
	return 0;
	}

This is my code and the function display() is empty. I dont know what to do with that so that the data entry can be displayed
I suppose the first question is, are you trying to learn C or C++?
Because this mix-and-match approach you have at the moment won't work in the long run.

The next question is, just how old is your compiler?
> #include<conio.h>
Was rendered obsolete ~1990 when Microsoft gave up on DOS.

> #include<iostream.h>
Was rendered obsolete in 1998 when ANSI/ISO ratified the first C++ standard.

Do you have a modern compiler and OS, or are you stuck with something like TurboC?

All those "implicit return types" on your functions were never valid C++, although it was valid in archaic 'C'.

As a general tip for the future, leave all the eye candy 'gotoxy()' until the end.
Making it look pretty is a simple step done last.
Keeping it pretty when you're still developing just wastes your time.

Pick an indentation style and stick to it.
https://en.wikipedia.org/wiki/Indentation_style

I fixed it so it will at least compile with a modern compiler, without caring about which OS you're on.
But you do need to decide for yourself which language you're programming in.
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
//#include<stdio.h>
//!!#include<conio.h> !! OBSOLETE and unportable
//!!#include<iostream.h>!! OBSOLETE and unportable
#include <cstdio>  // The modern C++ name for the C header file stdio.h
#include <iostream> // The modern C++ name for the archaic C++ iostream.h
using namespace std;

// Porting wrappers
void gotoxy(int,int){}
int getch(){return 0;}
void clrscr(){}

void Exit()
{
}

void inpt()
{
  char name[30], add[50]        /*,
                                   sect[1],trck[20] */ ;
  int Mid, Fin;
  int Gave;
  FILE *File1;
  File1 = fopen("Grades.Dat", "a");
  clrscr();

  gotoxy(33, 2);
  printf("Rizal High School");
  gotoxy(36, 3);
  printf("Senior High");
  gotoxy(12, 4);
  printf("__________________________________________________________");
  gotoxy(34, 8);
  cout << "Grading System";
  gotoxy(25, 9);
  cout << "(Multiple Relational Operator)";
  gotoxy(10, 11);
  cout << "Student Name: ";
  cin >> name;
  gotoxy(10, 12);
  cout << "Student Address: ";
  cin >> add;
  gotoxy(10, 13);
  cout << "Enter Midterm Grade: ";
  cin >> Mid;
  gotoxy(10, 14);
  cout << "Enter Final Grade: ";
  cin >> Fin;

  Gave = (Mid + Fin) / 2;
  fprintf(File1, "\n %s %s %d %d %d", name, add, Mid, Fin, Gave);
  gotoxy(10, 15);
  cout << "Final Grade: ";
  cout << Gave;

  if (Gave <= 74) {
    gotoxy(10, 16);
    cout << "Failed!";
  } else if (Gave <= 80) {
    gotoxy(10, 16);
    cout << "Passed!";
  } else if (Gave <= 85) {
    gotoxy(10, 16);
    cout << "Good!";
  } else if (Gave <= 90) {
    gotoxy(10, 16);
    cout << "Very Good!";
  } else if (Gave <= 95) {
    gotoxy(10, 16);
    cout << "Excellent!";
  } else if (Gave <= 100) {
    gotoxy(10, 16);
    cout << "Outstanding!";
  }
  else {
    gotoxy(10, 16);
    cout << "You're A Legend!";
  }

  gotoxy(28, 20);
  printf("Press any key to Continue");
  getch();
}

void display()
{
}

void reset()
{
  int ans;
  FILE *File1;
  cout << "Reset Databases 1-YES 2-NO: ";
  cin >> ans;
  if (ans == 1) {
    File1 = fopen("Grades.Dat", "w");
    cout << "\nDatabase has been DELETED";
    cout << "\nPress Any Key to Continue";
    getch();
    //!! return 0;
  } else
    cout << "Press 1-Yes 2-No";
  fclose(File1);
  //!! return 0; you ignored it anyway
}

int main()
{
  int opt;
  do {
    clrscr();
    cout << "\n Grading System Database";
    cout << "\n 1 - Input";
    cout << "\n 2 - Display";
    cout << "\n 3 - Reset Database";
    cout << "\n 0 - Exit Program";
    cout << "\n Enter Number: ";
    cin >> opt;
    switch (opt) {
    case 1:
      inpt();
      break;
    case 2:
      display();
      break;
    case 3:
      reset();
      break;
    case 0:
      Exit();
      break;
    default:
      cout << " Invalid Number!\n";
      getch();
      break;
    }
  }
  while (opt != 0);

  cout << " Thankyou!";
  getch();
  return 0;
}



> This is my code and the function display() is empty.
> I dont know what to do with that so that the data entry can be displayed
Well you used this to write to the file.
> fprintf(File1, "\n %s %s %d %d %d", name, add, Mid, Fin, Gave);
So perhaps something involving fscanf might be in order.


First some other comments:
- Line 71 is in the wrong place. It should be under the if, not the else.

I always find it best to create matching read() and write() functions that read/write a single record. Then use these where necessary. That way if there's a problem reading and writing, you'll know exactly where to look.

Now in your case, I can see a problem right away. Your program can't handle spaces in the name or the address.

Perhaps more inportant, you should be using strings instead of arrays of chars for name and address. Also, if you're learned about struct's, you should create a struct for name, addr, mid, final and avg).

Assuming that you have NOT learned about string and struct, here are possible (untested) versions of code to read and write your data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool read(FILE* fp, const char *name, const char *addr, int mid, int final, int avg)
{
    fprintf(fp, "%s\n", name);  // write name and address on separate lines so they can have spaces
    fprintf(fp, "%s\n", addr);
    fprintf(fp, "%d %d %d\n", mid, final, avg);
    return ferror(fp) == 0;
}

bool write(FILE*fp, char *name, unsigned nameSize, char *addr, unsigned addrSize, int &mid, int &final, int &avg)
{
    fgets(name, nameSize, fp);
    fgets(addr, addrSize, fp);
    fscanf(fp, "%d %d %d\n", &mid, &final, &avg);
    return ferror(fp) == 0;
}
Definitely TurboC++ (sigh).

salem c already gave the answer (fscanf, most likely), but I just wanted to say: When you're having trouble, it helps to figure out how to simplify the problem.

You're not sure what to display? Okay, so what do you need to display?
The contents in the file? Okay, so do you know how to open a file for reading?
That's step 1. Forget everything else.

Step 2 is, once you've opened a file for reading, simplifying the question into
"given a file that contains

Name Addr 40 60 50
OtherName Addr2 80 70 75
...

How do you print that data to to the user?"
Last edited on
Definitely TurboC++

India seems dedicated to it.
Topic archived. No new replies allowed.