I have a problem with this code. Why this shows random symbols when i give view data ! ?

#include<iostream>
#include<conio.h>
#include<windows.h>
#include "gotoxy.h"
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<fstream.h>

using namespace std;

//function declarations
void out(char *);
void matrix(int,int);


class student
{

public:
//class variable declaration in public part
int roll,fee,ht,wt;
char adminno[10],name[30],yn,imark[30],save,blood[10],n[30],age[3],sch[300],clas[10],s[7],address[50],fn[30],mn[30],
joinclass[10],secondlang[10],primlang[10],ph[15],mobil[11],fo[20],mo[20],brosis[30],date[9],time[9],dob[9],fpos;

//constructor
       student()
       {
         roll=ht=wt=0;       
         strcpy(primlang,"English");
       }
       
// Getting student information from user
void adform()
{

top:   
fflush(stdin);
system("cls");
out ("\n Admission Number     : ");
cin >> adminno;
fflush(stdin);
out (" Student's name       : ");
gets(n);
fflush(stdin);
out(" Class Joining        : ");
gets(joinclass);
out(" Primary Language     :  English\n");
fflush(stdin);
out("Secondary Language   : ");
gets(secondlang);
out (" Father's  name       : ");
gets(fn);
out (" Mother's  name       : ");
gets(mn);
out (" Date of Birth        : ");
gets(dob);
out (" Address              : ");
gets(address);
out (" Father's Occupation  : ");
gets(fo);
out (" Mother's Occupation  : ");
gets(mo);
out (" Age                  : ");
gets(age);
out (" Sex                  : ");
gets(s);
out (" Phone Number         : ");
gets(ph);
out (" Mobile Number        : ");
gets(mobil);
fflush(stdin);
out (" Height               : ");
cin >> ht;
fflush(stdin);
out (" Weight               : ");
cin >> wt;
fflush(stdin);
out (" Blood Group          : ");
gets(blood);
fflush(stdin);
out (" Identification Marks : ");
gets(imark);
cout <<"\n";
} 
//end of user get info function

//saves the data to file
void filesave()
{
FILE *fp;
fp=fopen(adminno,"w");
fprintf(fp,"%s",adminno);
fprintf(fp," ");
fputs(n,fp);
fprintf(fp," ");
fputs(joinclass,fp);
fprintf(fp," ");
fputs(primlang,fp);
fprintf(fp," ");
fputs(secondlang,fp);
fprintf(fp," ");
fputs(fn,fp);
fprintf(fp," ");
fputs(mn,fp);
fprintf(fp," ");
fputs(dob,fp);
fprintf(fp," ");
fputs(address,fp);
fprintf(fp," ");
fputs(fo,fp);
fprintf(fp," ");
fputs(mo,fp);
fprintf(fp," ");
fputs(age,fp);
fprintf(fp," ");
fputs(s,fp);
fprintf(fp," ");
fputs(ph,fp);
fprintf(fp," ");
fputs(mobil,fp);
fprintf(fp," ");
fprintf(fp, "%d",ht);
fprintf(fp," ");
fprintf(fp, "%d",wt);
fprintf(fp," ");
fputs(blood,fp);
fprintf(fp," ");
fputs(imark,fp);
fprintf(fp," ");
fclose(fp);
}
}; //end of class

//intro screen
void welcome()
{
system("cls");
system("color 0E");
gotoxy(35,13);
out ("Student Management");
Sleep(500);
gotoxy(33,14);
out(" (C) MAD Groups 2011");
Sleep(3000);
}


//credits
void thank()
{
system("cls");
system("color 0a");
gotoxy(35,4);
out ("Manoj Kumar K R");
gotoxy(35,6);
out ("Arun Shankar T S");
gotoxy(35,8);
out ("Dinesh Kumar K");
gotoxy(35,10);
out ("GopiNath P");
gotoxy(35,12);
out ("Deepak R");
gotoxy(35,14);
out ("Praveen Karthikeyan A S");
gotoxy(8,20);
system("color 02");
out ("Thank you for using a Geniune Product of MAD Groups,Infinite Ltd.");
Sleep(5000);
}

//global constant variables
const long int clas = 32;
const long int students = 50;

//fancy cout function for printing
void out(char s[100])
{
for(int i=0;i<=strlen(s);i++)
{
Sleep(50);
cout << s[i];

}
}

//function to scan and print data
void scandata(char *);
void scandata(char a[10])
{
student s1;
FILE *fp;
fp=fopen(a,"r");
//scans data
while(!EOF)
{
fflush(stdin);
fscanf(fp,"%s",s1.adminno);
fscanf(fp,"%s",s1.name);
fscanf(fp,"%s",s1.joinclass);
fscanf(fp,"%s",s1.primlang);
fscanf(fp,"%s",s1.secondlang);
fscanf(fp,"%s",s1.fn);
fscanf(fp,"%s",s1.mn);
fscanf(fp,"%s",s1.dob);
fscanf(fp,"%s",s1.address);
fscanf(fp,"%s",s1.fo);
fscanf(fp,"%s",s1.mo);
fscanf(fp,"%s",s1.age);
fscanf(fp,"%s",s1.s);
fscanf(fp,"%s",s1.ph);
fscanf(fp,"%s",s1.mobil);
fscanf(fp,"%d",s1.ht);
fscanf(fp,"%d",s1.wt);
fscanf(fp,"%s",s1.blood);
fscanf(fp,"%s",s1.imark);
fflush(stdin);
fclose(fp);
}
//prints data
system("cls");
printf("\nAdmission Number : %s",s1.adminno);
printf("\nname : %s ",s1.name);
printf("\nClass Joining : %s ",s1.joinclass);
printf("\nPrimary Language : %s ",s1.primlang);
printf("\nSecondary Language : %s ",s1.secondlang);
printf("\nFather's name : %s",s1.fn);
printf("\nMother's name : %s",s1.mn);
printf("\nDate of Birth : %s",s1.dob);
printf("\nAddress : %s",s1.address);
printf("\nFather's Occupation %s",s1.fo);
printf("\nMother's Occupation %s",s1.mo);
printf("\nAge : %s",s1.age);
printf("\nSex : %s",s1.s);
printf("\nPhone Number : %s",s1.ph);
printf("\nMobile Number : %s",s1.mobil);
printf("\nHeight : %d",s1.ht);
printf("\nWeight : %d",s1.wt);
printf("\nBlood Group : %s",s1.blood);
printf("\nIdentification Marks : %s",s1.imark);
getch();
}//end of file reading,printing function

//main function
int main()
{
int i=1;
unsigned long int n;
char yn,save,admin[10];
student s2;

welcome();

//main menu
start:
system("cls");
system("color 0B");
out("\n\n\n\n		Enter an option : \n");
out("\n		   1. Enter  Data ");
out("\n		   2. View   Data ");
out("\n		   3. Edit   Data ");
out("\n		   4. Exit \n");
out("            ");
cin >> n;

//menu options - switch case
switch(n)
{

case 1:
	 s2.adform();
	 out (" Press 's' to save and continue\n");
     out ("Press 'c' to clear details\n");
     out ("Press 'v' to save and quit\n");
     out ("Press 'q' to quit without saving\n");
     enter:
     cin >> save;
     switch(save)
     {
      case 's':s2.filesave();s2.adform();
      case 'c':s2.adform();
      case 'v':s2.filesave();break;
      case 'q':break;
      default: out("Invalid option...\n");goto enter;
     }
	 break;

case 2:out ("Enter the admission number : ");
	cin >> admin;
	scandata(admin);
	break;

case 3:out ("Enter the admission number : ");
	cin >> admin;
	break;

case 4:goto exit;

default : out ("\n\a\a Try Again");for(int k=1;k<=3;k++){Sleep(1000);cout << ".";}Sleep(1000);;goto start;

}

goto start;

//end of prog with credits
exit:
thank();
return(0);

}

Recommended Answers

All 10 Replies

When, where, what, etc. More details are needed. Random characters how? What's the exact input file? What's the exact output? What's it supposed to be? Etc. Point to a line number. conio.h and gotoxy.h are problems. I for one can't run a program that uses them because I don't have them.

This is gotoxy.h

#ifdef WIN32
#include <windows.h>
void gotoxy(int x, int y)
{
  COORD cur = {x, y};
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cur);
} 
#else
void gotoxy(int x, int y)
{
  printf("\033[%dG\033[%dd", x+1, y+1);
}
#endif

This is a student management project where i need to get info about the student and store in a file. Everything stores correctly but while retrieving data the output displays random symbols rather than the contents of a file

This is conio.h

/*
 * conio.h
 * This file has no copyright assigned and is placed in the Public Domain.
 * This file is a part of the mingw-runtime package.
 * No warranty is given; refer to the file DISCLAIMER within the package.
 *
 * Low level console I/O functions. Pretty please try to use the ANSI
 * standard ones if you are writing new code.
 *
 */

#ifndef	_CONIO_H_
#define	_CONIO_H_

/* All the headers include this file. */
#include <_mingw.h>

#ifndef RC_INVOKED

#ifdef	__cplusplus
extern "C" {
#endif

_CRTIMP char* __cdecl	_cgets (char*);
_CRTIMP int __cdecl	_cprintf (const char*, ...);
_CRTIMP int __cdecl	_cputs (const char*);
_CRTIMP int __cdecl	_cscanf (char*, ...);

_CRTIMP int __cdecl	_getch (void);
_CRTIMP int __cdecl	_getche (void);
_CRTIMP int __cdecl	_kbhit (void);
_CRTIMP int __cdecl	_putch (int);
_CRTIMP int __cdecl	_ungetch (int);


#ifndef	_NO_OLDNAMES

_CRTIMP int __cdecl	getch (void);
_CRTIMP int __cdecl	getche (void);
_CRTIMP int __cdecl	kbhit (void);
_CRTIMP int __cdecl	putch (int);
_CRTIMP int __cdecl	ungetch (int);

#endif	/* Not _NO_OLDNAMES */


#ifdef	__cplusplus
}
#endif

#endif	/* Not RC_INVOKED */

#endif	/* Not _CONIO_H_ */

I enter the details of students like age ,admission number , name ,etc... when i try to get data from the stored file i get random symbols like smileys,underscore,blah blah blah ,etc...

I hope The problem exists on line 191-193.. My file is not opened correctly

I know what gotoxy and conio.h are. They aren't standard and my compiler doesn't have them. See lines 7 and 8.

* Low level console I/O functions. Pretty please try to use the ANSI
* standard ones if you are writing new code.

I don't see any need for your code to use conio, gotoxy, colors, etc. All they do is make it that much harder to debug. cstdio and iostream has what you need. Stick with them. No need to bounce all over the screen with gotoxy.


Line 195 doesn't make sense to me. "while(!EOF)". Usually you are COMPARING something to EOF. You have EOF as a constant as a while condition. How can that value ever change? Seems like you'll either never get into that while loop or it'll be an infinite loop.

I have a problem with this code.

Here's what I see as your major problems:
Lack of proper formatting makes the code difficult to read #include<conio.h> -- using non-standard header. It's not portable and (as you can see from VernonDozier) you can't get adequate help. #include "gotoxy.h" -- Same as conio.h fflush(stdin); -- See this system("cls"); -- Calling the system is not a good idea. Clearing the screen is annoying to the user at best. And it's not portable -- not all O/S's have the command CLS. system("color 0E"); -- Same as system("cls"); getch(); -- non-standard and not portable. getchar() is standard and portable. gets(n); -- Dangerous. See this fprintf(fp," "); -- Why call such a heavy, complicated function to output one space? How about fputc()? gotoxy(35,13); -- non-standard and not portable. goto start; -- C++ is designed to not need goto's which helps make poorly designed code. Use loops and other constructs.

Here's what I see as your major problems:
Lack of proper formatting makes the code difficult to read #include<conio.h> -- using non-standard header. It's not portable and (as you can see from VernonDozier) you can't get adequate help. #include "gotoxy.h" -- Same as conio.h fflush(stdin); -- See this system("cls"); -- Calling the system is not a good idea. Clearing the screen is annoying to the user at best. And it's not portable -- not all O/S's have the command CLS. system("color 0E"); -- Same as system("cls"); getch(); -- non-standard and not portable. getchar() is standard and portable. gets(n); -- Dangerous. See this fprintf(fp," "); -- Why call such a heavy, complicated function to output one space? How about fputc()? gotoxy(35,13); -- non-standard and not portable. goto start; -- C++ is designed to not need goto's which helps make poorly designed code. Use loops and other constructs.

Can you suggest alternates please ..

He did. They're in the links he linked and some are in his actual post...

getchar() is standard and portable.

How about fputc()?

Other alternatives... First get a new compiler or a new book or whatever. fstream.h, conio.h? No one uses them enymore. It's plain old "fstream" now. There are no alternatives necessary for all of your "system" commands and your gotoxy commands. All they do is clutter. Just delete them altogether and don't try to replace them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.