Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone,
I am trying to execute the file operation program in COODEBLOCKS editor. The program executes. The search function and display functions are going infinite loop OR something is wrong.
Could you please let me know what is wrong.
Thanks


What I have tried:

#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
class student
{
	public: char name[10];
		char usn[10];
		char age[5];
		char sem[5];
		char branch[5];
		char buffer[45];
};
fstream file;
student s;
void pack()
{
int k,n;
printf("how many records\n");
	scanf("%d",&n);
	for(k=0;k<n;k++)
	{
cout<<"\nenter the student name = ";
	cin>>s.name;
	cout<<"\nenter the usn = ";
	cin>>s.usn;
	cout<<"\nenter the age = ";
	cin>>s.age;
	cout<<"\nenter the sem = ";
	cin>>s.sem;
	cout<<"\nenter the branch = ";
	cin>>s.branch;
	strcpy(s.buffer,s.name);
	strcat(s.buffer,"|");
	strcat(s.buffer,s.usn);
	strcat(s.buffer,"|");
	strcat(s.buffer,s.age);
	strcat(s.buffer,"|");
	strcat(s.buffer,s.sem);
	strcat(s.buffer,"|");
	strcat(s.buffer,s.branch);
	int count=strlen(s.buffer);
for(int k=0;k<45-count;k++)
		strcat(s.buffer,"!");
	strcat(s.buffer,"\n");
file<<s.buffer;  
}
	file.close();
}
void writerecord()
{
	file.open("p2.txt",ios::out);
	if(!file)
	{
		cout<<"cannot open the file in out mode";
		exit(1);
	}
pack();
	
}
void unpack()
{
		file.getline(s.name,10,'|');
		file.getline(s.usn,10,'|');
		file.getline(s.age,5,'|');
		file.getline(s.sem,5,'|');
		file.getline(s.branch,5,'!');
}
void search()
{
	char usn[10];
	char extra[45];
	file.open("p2.txt",ios::in);
	if(!file)
	{
		cout<<"\nunable to open the file in read mode";
		exit(0);
	}
	cout<<"\nenter the record's usn you want to search = ";
	cin>>usn;
	while(!file.eof())
	{
		unpack();
		file.getline(extra,45,'\n');
		if(strcmp(s.usn,usn)==0)
		{
			cout<<"\nrecord found";
			cout<<"\n"<<s.name<<"\t"<<s.usn<<"\t"<<s.age<<"\t"<<s.sem<<"\t"<<s.branch;
			file.close();
			
			return;
		}
	}
	cout<<"\nrecord not found";
	file.close();
	
}
void displayFile()
{
	int i;
	char extra[45];
	file.open("p2.txt",ios::in);
	if(!file)
{
		cout<<"\ncannot open the file in read mode";
		exit(1);
	}
	i=0;
	cout<<"\n\nNAME\t\tUSN\t\tAGE\t\tSEM\t\tBRANCH\n";
	cout<<"----\t\t---\t\t---\t\t---\t\t------\n";
	while(!file.eof())
	{
		unpack();
		file.getline(extra,45,'\n');
		cout<<"\n"<<s.name<<"\t"<<s.usn<<"\t"<<s.age<<"\t"<<s.sem<<"\t"<<s.branch<<"\n";
	}
	file.close();
}
void modify()
{
	char usn[10];
	char buffer[45];
	char extra[45];
	int i;
	int j;
	student s[20];
	file.open("p2.txt",ios::in);
	if(!file)
	{
		cout<<"\nunable to open the file in input mode";
		exit(1);
	}
	cout<<"\nenter the usn of the record to be modified\n";
	cin>>usn;
	cout<<"\n";
	i=0;
	while(!file.eof())
	{
		file.getline(s[i].name,10,'|');
		file.getline(s[i].usn,10,'|');
		file.getline(s[i].age,5,'|');
		file.getline(s[i].sem,5,'|');
		file.getline(s[i].branch,5,'!');
		file.getline(extra,45,'\n');
		i++;
	}
	i--;
	for(j=0;j<i;j++)
	{
		if(strcmp(usn,s[j].usn)==0)
		{
			cout<<"\nthe old values of the record with usn"<<usn<<"are";
			cout<<"\n\nNAME\t\tUSN\t\tAGE\t\tSEM\t\tBRANCH\n";
			cout<<"----\t\t---\t\t---\t\t---\t\t------\n";
	cout<<s[j].name<<"\t"<<s[j].usn<<"\t"<<s[j].age<<"\t"<<s[j].sem<<"\t"<<s[j].branch<<"\n";
			cout<<"\n\nenter the new values\n";
			cout<<"\nname = ";
			cin>>s[j].name;
			cout<<"\nusn = ";
			cin>>s[j].usn;
			cout<<"\nage = ";
			cin>>s[j].age;
			cout<<"\nsem = ";
			cin>>s[j].sem;
			cout<<"\nbranch = ";
			cin>>s[j].branch;
			break;
		}
	}
	if(j==i)
	{
		cout<<"\nthe record with usn "<<usn<<"is not present ";
		return;
	}
	file.close();
	file.open("p2.txt",ios::out);
	if(!file)
	{
		cout<<"\nunable to open the file in output mode";
		return;
	}
	for(j=0;j<i;j++)
	{
strcpy(buffer,s[j].name);
		strcat(buffer,"|");
		strcat(buffer,s[j].usn);
		strcat(buffer,"|");
		strcat(buffer,s[j].age);
		strcat(buffer,"|");
		strcat(buffer,s[j].sem);
		strcat(buffer,"|");
		strcat(buffer,s[j].branch);
		int count=strlen(buffer);
		for(int k=0;k<45-count;k++)
			strcat(buffer,"!");
		strcat(buffer,"\n");
		file<<buffer;
	}
	file.close();
}
int main()
{
	int choice;
	while(1)
	{
		cout<<"\n 0 : exit \n 1 : write to file\n 2 : display the file\n 3 : modify the file\n 4 : search \n\n enter the choice : ";
		cin>>choice;
		switch(choice)
		{
			case 1: writerecord();
				break;
			case 2: displayFile();
				break;
			case 3: modify();
				break;
			case 4: search();
				break;
			case 0: exit(0);
			default:cout<<"\ninvalid input...";
				break;
		}
	}
return 0;
}
Posted
Updated 17-May-21 12:49pm
Comments
Rick York 17-May-21 1:45am    
This is a good time to use a debugger since this is why they were invented. If you don't have access to one then starting putting in printf statements to show you what's going on. In fact, there's a whole style of diagnosing problems called "printf debugging." Sometimes you have to do what you have to do.

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
private int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
It is debugger time for you. So start the debugging process with reading some debugger documentation.

My first guess is that the k in pack() hasnt a proper value, because it is uninitialized. Fix it and watch out for the next bug. ;-)
 
Share this answer
 
You should not use char arrays in C++ when you dont know the length and dont use it with cin. When you have already a class; why not using a method to change membervariables?

This is not a good way to make it working:
C++
class student {
public: 
  char name[10];
  ...
};

student s;

cout << "\nenter the student name = ";
cin >> s.name;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900