Solved Why isnt my code working??

October 18, 2018 at 04:22:57
Specs: Windows 7
I had aimed to create a simple program for students to enter their class and search for them as well as delete them from a data file. I had also made arrangements for the data file to store a list of tasks inputted by the student. The access to each student's personal classes ad tasks will be through the entry of their username and password. However no matter how many times I try .. the program still refuses to work . I use TURBO C++ because we were required to use it . PLEASE HELP!!

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
#include<string.h>
#include<process.h>
#include<graphics.h>

void frontpage()
{
cout<<"\n\n\n\n";
cout<<"\n\t\t |||||||| ||||||||| || || ||||||| || || " ;
cout<<"\n\t\t || ||| || || || || || ||";
cout<<"\n\t\t |||||||| ||| || || || || ||";
cout<<"\n\t\t || ||| || || || || || ";
cout<<"\n\t\t |||||||| ||| |||||| ||||||| || ";

cout<<"\n \n";
cout<<"\n\t\t\t || || |||||||| ||||||||";
cout<<"\n\t\t\t || || || || ";
cout<<"\n\t\t\t || || |||| |||||| ";
cout<<"\n\t\t\t || || || || ";
cout<<"\n\t\t\t |||||||| || || |||||||| ";
cout<<"\n\n\n \t\t\t\t Press ENTER to start";
getch();

}
void welcome()
{ clrscr();
cout<<"\n\n\n\n\n\t\t\t\t\n\t\n\t\t\t\t ";
cout<<"\n\t || || |||||||| || ||||||| |||||| |||| |||| |||||||| " ;
cout<<"\n\t || || || || || || || || |||| || || ";
cout<<"\n\t || || || |||||| || || || || || || || ||||| ";
cout<<"\n\t |||||| || || || || || || || || ";
cout<<"\n\t || || |||||||| |||||| ||||||| |||||| || || |||||||| ";


}
int checkuname();

int checkuser(int a);
int newaccount(int a);
int checkpass();
class student {
int code;
char sub[50],times[10],date[10],prof[20];
char task[100],duedate[50];
int tasknumber;
int rollno,age;
char name[50],user[50],pass[20];
public:

int roll()
{
return rollno;
}
void userdets();
char* passn()
{
return pass;
}
char* usern()
{
return user;
}
void viewclass(int a);
void addclass(int a);
void deleteclass(int a);
void getclass();
void putclass();
int classn()
{
return code;
}
void addtask(int a);
void viewtask(int a);
void deletetask(int a);
void gettask();
void puttask();
int tasknumb()
{
return tasknumber;

}
};
student s1[10];
int checkuname()
{
ifstream fin("studt.dat",ios::in|ios::binary);
char user[50];
int a=0;
cout<< "\n Enter username";
gets(user);
while (!fin.eof())
{
fin.read((char*)&s1[a],sizeof(s1[a]));
if (fin.eof())
break;
if (strcmpi(user,s1[a].usern())==0)
cout << "\n Correct username";
a++;
}
getch();
fin.close();
return a;
}
void student :: userdets()
{
cout << "\n Enter your rollno";
cin >> rollno;
cout << "\n Enter user details";
cout << "\n Enter your name";
gets(name);
cout << "\n Enter your age";
cin >>age;
cout << "\n Enter your username";
gets(user);
cout << "\n Enter your password";
cin >> pass;
}
void student :: getclass()
{
cout<<"\n ENTER CLASS DETAILS";
cout<<"\nENTER CLASS CODE";
cin>> code;
cout<<"\nENTER SUBJECT";
gets(sub);
cout<<"\nENTER TEACHER";
gets(prof);
cout<<"\nENTER TIMING";
gets(times);
cout << "\n ENTER DATES";
gets(date);
}
void student :: putclass()
{
cout<<"\n \t THE CLASS DETAILS:" ;
cout<<"\n \n \t CLASS CODE:"<<" "<<code;
cout<<"\n \n \t SUBJECT:";
puts(sub);
cout<< "\n \n \t TEACHER:"<<" ";
puts(prof);
cout<<"\n \n \t TIMING:"<<" ";
puts(times);
cout << "\n DATES";
puts(date);
}
void student :: gettask()
{
cout << "\n ENTER TASK NUMBER";
cin>>tasknumber;
cout<<"\n ENTER NEW TASK";
gets(task);
cout<<"\n ENTER DUEDATE";
gets(duedate);
}
void student :: puttask()
{
cout << "\n The task number" <<tasknumber;
cout << "\n The task";
puts (task);
cout << "\n The due date";
puts (duedate);
}
int checkuser(int a)

{
int f;
int ch;
cout<< "\n\n\n\n";
cout<<"\n \t\t 1. CREATE NEW ACCOUNT ";
cout<<"\n \t\t 2. SIGN IN ";
cout<<"\n \n \t\t\t \t ENTER CHOICE : ";
cin>>ch;
switch(ch)
{
case 1 : f = newaccount(a);
break;
case 2 : f= checkpass();
break;
default:exit(0);

}
return f;

}


int checkpass()
{
ifstream fin( "studt.dat",ios::in|ios::binary);
int f,d=0,dun=0;
f= checkuname();
char pass1[20];
correct:
cout<<"\n ENTER PASSWORD : ";
gets(pass1);
while(!fin.eof())
{
fin.read((char*)&s1[f],sizeof(s1[f]));
if (fin.eof())
break;
if(strcmpi(s1[f].passn(), pass1)==0)
{
d=1;
cout<<"\n LOGIN SUCCESFUL : ";
break;

}

}
if (d==0)
{
cout << "\n INCORRECT PASSWORD";
dun++;
if (dun < 5)
goto correct;
else
{ cout<< "\n Too many attempts";
exit(0);
}
}
getch();
fin.close();
return f;
}
int newaccount(int a)
{
ofstream fin( "studt.dat",ios::app|ios::binary);
if (a<10)
{
cout << "\n Your rollno is "<< a;
s1[a].userdets();
fin.write((char*)&s1[a],sizeof(s1[a]));
}
else
{
cout << "\n Sorry ! User overload!";
exit(0);
}
fin.close();
return a;
}
void student :: addclass(int a)
{
ofstream fin1( "studt1.dat",ios::app|ios::binary);
char ch ='y';
while (ch =='y')
{
s1[a].getclass();
fin1.write((char*)&s1[a],sizeof(s1[a]));
cout << "\n Class added. Want to add more? (y/n)";
cin >> ch;
}
fin1.close();

}

void student :: viewclass(int a)
{
ifstream fin1( "studt1.dat",ios::in|ios::binary);
int cod;
char found='n';
cout<<" ENTER CLASS CODE";
cin>>cod;
while(!fin1.eof())
{
fin1.read((char*)&s1[a],sizeof(s1[a]));
if(s1[a].classn()==cod)
{
s1[a].putclass();
found ='y';
break;

}

}
if (found=='n')
cout<< "\n Incorrect class code";
fin1.close();
}
void student :: deleteclass(int a)

{
ifstream fin1( "studt1.dat",ios::in|ios::binary);
ofstream file("temprt1.dat",ios::out|ios::binary);
int cod;
char found='f',confirm='n';
cout<<" \n \n \t ENTER CLASS CODE TO BE DELETED";
cin>>cod;
while(!fin1.eof())
{
fin1.read((char*)&s1[a],sizeof(s1[a]));
if(s1[a].classn()==cod)
{
s1[a].putclass();
found='t';
cout<<" \n \n ARE YOU SURE YOU WANT TO DELETE THIS CLASS(Y/N)";
cin>>confirm;
if(confirm=='n')
file.write((char*)&s1[a],sizeof(s1[a]));
}
else
file.write((char*)&s1[a],sizeof(s1[a]));
}

if(found=='f')
cout<<"\n \n \t CLASS NOT FOUND";
file.close();
fin1.close();
remove("studt1.dat");
rename("temprt1.dat","studt1.dat");
fin1.open("studt1.dat",ios::in);
while (!fin1.eof())
{
fin1.read((char*)&s1[a],sizeof(s1[a]));
if (fin1.eof())
break;
s1[a].putclass();
}
fin1.close();
}


void student :: addtask(int a)
{
ofstream fin2( "studt2.dat",ios::app|ios::binary);
char ch='y';
while (ch=='y')
{
s1[a].gettask();
fin2.write((char*)&s1[a],sizeof(s1[a]));
cout<<"\n DO YOU WANT TO ENTER MORE TASKS (Y\N)";
cin>>ch;
}
fin2.close();

}


void student :: viewtask(int a)
{
ifstream fin2( "studt2.dat",ios::in|ios::binary);
int rn;
char found ='n';
cout << "\n Enter task number";
cin >> rn;
while(!fin2.eof())
{
fin2.read((char*)&s1[a],sizeof (s1[a]));
if (rn == s1[a].tasknumb())
{
s1[a].puttask();
found ='y';
break;
}

}
if (found == 'n')
cout<< "\n Task number not found ";
fin2.close();
}
void student :: deletetask(int a)
{
ifstream fin2( "studt2.dat",ios::in|ios::binary);
char confirm ='y',found='f';
int tasknumber1;
cout<<"\n ENTER TASK NUMBER OF TASK TO BE DELETED : ";
cin>>tasknumber1;
ofstream file("temprt2.dat",ios ::out|ios :: binary);
while(!fin2.eof())
{
fin2.read((char*)&s1[a],sizeof(s1[a]));
if(s1[a].tasknumb()==tasknumber1)
{
s1[a].puttask();
found = 't';
cout<<"\n ARE YOU SURE YOU WANT TO DELETE? : ";
cin>>confirm;
if(confirm=='n')
file.write((char*)&s1[a],sizeof(s1[a]));
}
else
file.write((char*)&s1[a],sizeof(s1[a]));
}
if(found=='f')
cout << "\n Record not found";
fin2.close();
file.close();
remove("studt2.dat");
rename("temprt1.dat","studt2.dat");
fin2.open("studt2.dat",ios::in | ios :: binary);
while (!fin2.eof())
{

fin2.read((char*)&s1[a],sizeof(s1[a]));
if (fin2.eof())
break;
s1[a].puttask();
}
fin2.close();
}
void main()
{
clrscr();
ifstream fin( "studt.dat",ios::in|ios::binary);
frontpage();
welcome();
int i=0,rno=0,ch;
if (!fin.eof())
{
while (!fin.eof())
{
fin.read((char*)&s1[i],sizeof(s1[i]));
if (fin.eof())
break;
else
i++;
}
}
rno=checkuser(i);
start:
cout << "\n \n \n MENU ";
cout << "\n \n 1. MY CLASS : ";
cout << "\n \n 2. MY TO-DO LIST : ";
cout << "\n \n 3. Exit program";
cout<<"\n ENTER CHOICE : ";
cin>>ch;
switch(ch)
{
case 1 :
{
int ch1;
start1:
cout<<"\n \t 1. VIEW CLASS ";
cout<<"\n \t 2. ADD CLASES ";
cout<<"\n \t 3. DELETE CLASS ";
cout<<"\n \t 4. GO BACK ";
cout<<"\n \n ENTER CHOICE : ";
cin>>ch1;
switch(ch1)
{
case 1 : s1[rno].viewclass(rno);
goto start1;


case 2 : s1[rno].addclass(rno);
goto start1;


case 3 : s1[rno].deleteclass(rno);
goto start1;


case 4 : goto start;


default : cout << "\n Entered incorrect command";
goto start1;

}
}
case 2 :
{
int ch2;
start2:

cout<<"\n\t 1.ADD TASK ";
cout<<"\n\t 2.VIEW TASK ";
cout<<"\n\t 3.DELETE TASK ";
cout<<"\n\t 4.GO BACK ";
cout<<"\n\n ENTER CHOICE ";
cin>>ch2;

switch(ch2)
{
case 1 : s1[rno].addtask(rno);
goto start2;


case 2 : s1[rno].viewtask(rno);
goto start2;


case 3 : s1[rno].deletetask(rno);
goto start2;


case 4 : goto start;


default:"\n Error..wrong command!";
goto start2;


}

}
}
getch();
}



See More: Why isnt my code working??

Reply ↓  Report •

✔ Best Answer
October 20, 2018 at 00:18:45
I'm afraid that I haven't got time to debug your code, but I can offer a couple of observations:

1. You are mixing C++ and C functions (e.g. using "<<" and "getch()" this is a little confusing and is not good practice.

2. You shouldn't need to use "goto"s.

3. Turbo C++ is very old. I would recommend a more recent compiler such as Microsoft's Visual Studio.

I think you really need to start with very simple programs in conjunction with a good textbook, and take things slowly. Alternatively, if you are learning C++ as part of a course, discuss your problems with your tutor. It's not really a subject that can be taught via forum posts.



#1
October 18, 2018 at 06:06:41
"Doesn't work" is a little vague.

Does it compile?
Does it run?
What results do you expect?
What results do you get?


Reply ↓  Report •

#2
October 18, 2018 at 06:11:53
It does compile successfully...
However it doesn't run properly
I can create a new account and then access the classes and tasks. But sometimes even after I add classes , I am unable to view them. The delete function occurs twice in each run even though it should occur once. I cannot sign in since any password I enter is deemed incorrect . The program sometimes crashes even if there is no syntax error. I am sure that the data files aren't working right .

Reply ↓  Report •

#3
October 18, 2018 at 06:13:41
Also when I opened the data file...the information is disorganised and scattered. I am a beginner using c++ which is why I simply can't seem to find the problem with the code even after multiple debug attempts .

Reply ↓  Report •

Related Solutions

#4
October 20, 2018 at 00:18:45
✔ Best Answer
I'm afraid that I haven't got time to debug your code, but I can offer a couple of observations:

1. You are mixing C++ and C functions (e.g. using "<<" and "getch()" this is a little confusing and is not good practice.

2. You shouldn't need to use "goto"s.

3. Turbo C++ is very old. I would recommend a more recent compiler such as Microsoft's Visual Studio.

I think you really need to start with very simple programs in conjunction with a good textbook, and take things slowly. Alternatively, if you are learning C++ as part of a course, discuss your problems with your tutor. It's not really a subject that can be taught via forum posts.


Reply ↓  Report •

#5
October 20, 2018 at 00:27:56
Thank you for the advice.. I am currently consulting my teacher on how to fix the code .

Reply ↓  Report •

Ask Question