Trouble with bubble sort?
In this program i am writing to a file, reading the values to a array and then sort it based on id which is a char. Got it to read and write but need help with sorting it.
Here is the code:
Code:
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>
class person
{
public: char id[5];
/* char name[15];
char age[5];
char address[15];*/
char buffer[50];
};
fstream val,file;
person p;
char id[5];
void write()
{
val.open("person4.txt",ios::in);
if(!val)
{
cout << "Cannot open file" << endl;
exit(1);
}
cout << "Enter ID: " ;
cin >> id;
while(!val.eof())
{
val.getline(p.id,5,'|');
if(strcmp(id,p.id)==0)
{
cout << "ID already exists" << endl;
val.close();
getch();
return;
}
}
val.close();
strcpy(p.id,id);
file.open("person4.txt",ios::app);
if(!file)
{
cout << "Cannot open file" << endl;
exit(1);
}
/* cout << "Enter name: " ;
cin >> p.name;
cout << "Enter age: ";
cin >> p.age;
cout << "Enter address: ";
cin >> p.address;*/
strcpy(p.buffer,p.id);
strcat(p.buffer,"|");
/* strcat(p.buffer,p.name);
strcat(p.buffer,"|");
strcat(p.buffer,p.age);
strcat(p.buffer,"|");
strcat(p.buffer,p.address);
strcat(p.buffer,"!"); */
strcat(p.buffer,"\n");
file << p.buffer;
file.close();
}
void display()
{
int i,n;
person p[20];
char* temp;//temp[20];
file.open("person4.txt",ios::in);
if(!file)
{
cout << "Cannot open file!" << endl;
getch();
exit(1);
}
n=0;
i=0;
cout << "ID\t\tName\t\tAge\t\tAddress" << endl;
cout << "....\t\t..\t\t...\t\t..." << endl;
while(!file.eof())
{
file.getline(p[i].id,5,'|');
cout<<"p[i] id and i value"<<i<<p[i].id<<endl;
//file.getline(p[i].name,15,'|');
//file.getline(p[i].age,5,'|');
//file.getline(p[i].address,15,'!');
i++;
n++;
}
file.close();
for(int j=0;j<n;j++)
{
for(int k=j+1;k<n;k++)
{
if(strcmp(p[j].id,p[k].id)>= 0)
{
strcpy(temp,p[k].id);//(temp[j],p[k].id);
strcpy(p[k].id,p[j].id);
strcpy(p[j].id,temp);//(p[j].id,temp[k]);
}
}
}
for(i=0;i<n;i++)
cout<<p[i].id<<endl;
// file.close();
getch();
}
void main()
{
int choice;
while(1)
{
clrscr();
cout << "0: Exit: " << endl;
cout << "1: Write: " << endl;
cout << "2: Display: " << endl;
cin >> choice;
switch(choice)
{
case 0: exit(0);
break;
case 1: write();
break;
case 2: display();
break;
default: cout << "invalid";
break;
}
}
}
Re: Trouble with bubble sort?
Code:
char* temp;//temp[20];
*temp is not correct here as no memory allocation is done so the contents of temp are unknown and data is copied to this address! char temp[20] provides the required memory.
Why are you using iostream.h and fstream.h? This is not standard c++. Also why are you using c-style strings instead of the c++ string class?
Is this a homework exercise?
Re: Trouble with bubble sort?
Using turbo c++ and yes this is a homework where they said i have to use turbo or else this would be much easier on visual c++
Re: Trouble with bubble sort?
You are not being taught the right way in using c++. :thumbd: When learning c++, you should be taught the correct c++ way of doing things. Your code is not even standard c++.
Re: Trouble with bubble sort?
Quote:
Got it to read and write
The read function is not correct. When you write, you write a newline at the end of each line. When you read, you are reading using
Code:
file.getline(p[i].id, 5, '|');
which will read chars until either | is read or eof is reached. As a line ends with a newline, this is read as the first part of the next read chars - which is incorrect. If you want to read like this, you need to remove the newline from the beginning of the read chars if one is present.
Re: Trouble with bubble sort?
For one way this could be written as c++, consider the code below. Note how it looks quite different to the code in post #1. IMO this is how you should be being taught to code with c++.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <map>
using namespace std;
const char delim = '|';
const string filname = "person4.txt";
class person
{
private:
string id;
string name;
string age;
string address;
public:
person() {}
person(const string& aid, const string& aname, const string& aage, const string& aaddress) : id(aid), name(aname), age(aage), address(aaddress) {}
string getid() const
{
return id;
}
void display() const;
friend bool operator<(const person& lp, const person& rp);
friend istream& operator>> (istream& is, person& per);
friend ostream& operator<<(ostream& os, const person& per);
};
class people {
private:
map<string, person> mp;
public:
bool read(const string& fnam);
void display() const;
bool insert(const person& per);
bool write(const string& fnam) const;
};
bool operator<(const person& lp, const person& rp)
{
return lp.id < rp.id;
}
void person::display() const
{
cout << id << " " << name << " " << age << " " << address << endl;
}
istream& operator>> (istream& is, person& per)
{
string line;
if (getline(is, line)) {
stringstream ss(line);
string str;
getline(ss, str, delim);
per.id = str;
getline(ss, str, delim);
per.name = str;
getline(ss, str, delim);
per.age = str;
getline(ss, str, delim);
per.address = str;
}
return is;
}
ostream& operator<< (ostream& os, const person& per)
{
return os << per.id << delim << per.name << delim << per.age << delim << per.address << delim;
}
bool people::insert(const person& per)
{
return mp.insert(pair<string, person>(per.getid(), per)).second;
}
bool people::write(const string& fnam) const
{
ofstream ofs(fnam);
if (!ofs.is_open())
return false;
for (const auto& p : mp)
ofs << p.second << endl;
bool ret = ofs.good();
ofs.close();
return ret;
}
bool people::read(const string& fnam)
{
mp.clear();
ifstream ifs(fnam);
if (!ifs.is_open())
return false;
person per;
while (ifs >> per)
mp.insert(pair<string, person> (per.getid(), per));
bool ret = ifs.eof();
ifs.close();
return ret;
}
void people::display() const
{
for (const auto& p : mp)
p.second.display();
}
void main()
{
people pep;
while (1) {
int choice;
cout << "0: Exit: " << endl;
cout << "1: Add: " << endl;
cout << "2: Display: " << endl;
cout << "3: Read: " << endl;
cout << "4. Write: " << endl;
cin >> choice;
switch (choice) {
case 0:
exit(0);
break;
case 1:
{
string id, nam, age, addr;
cout << "Enter id: ";
cin >> id;
cout << "Enter name: ";
cin >> nam;
cout << "Enter age: ";
cin >> age;
cout << "Enter address: ";
cin >> addr;
if (!pep.insert(person(id, nam, age, addr)))
cout << "Already exists" << endl;
}
break;
case 2:
pep.display();
break;
case 3:
if (!pep.read(filname))
cout << "Problem reading file" << endl;
break;
case 4:
if (!pep.write(filname))
cout << "Problem writing file" << endl;
break;
default:
cout << "Invalid option" << endl;
break;
}
}
}
PS Don't be tempted to submit this for the assignment as the teacher should spot this!
Re: Trouble with bubble sort?
Much as i like to use that code it won't be accepted in turbo c++ as it doesn't recognize String and i know this isn't the right way of doing c++
but the compiler they are making us use is quite old and have to do it with the unethical way of using c++.
But is there a way of doing bubble sorting in the unethical way as i have tried this code works where if you enter 5 and then 4 it would organize it
but if i then enter 6 the 6 would come on top basically taking the latest input.
Re: Trouble with bubble sort?
If you fix the read issue (in both places you read from the file) and the temp variable problem (have it as a char array) then it seems to work as expected. Note that the sort ordering is based on char ordering and not numeric ordering. So 123 comes before 2 etc.
Re: Trouble with bubble sort?
Quote:
the compiler they are making us use is quite old and have to do it with the unethical way of using c++
That's the problem. You shouldn't be using this compiler! :thumbd: You should be taught using an up-to-date compiler that supports c++14 (current version of c++).
Re: Trouble with bubble sort?
Quote:
Originally Posted by
2kaud
If you fix the read issue (in both places you read from the file) and the temp variable problem (have it as a char array) then it seems to work as expected. Note that the sort ordering is based on char ordering and not numeric ordering. So 123 comes before 2 etc.
I see that my reading has a issue where it would display a blank empty line after each record but not sure how to fix that issue and i did have temp as a char array but it wasn't doing much difference
hence the reason i had commented it out.
char* temp;//temp[20];
Quote:
So 123 comes before 2 etc
how to fix this issue then when using char
Re: Trouble with bubble sort?
For the read issue, consider using something like
Code:
char rd[20];
while (file.getline(rd, 5, '|'))
{
char *f = rd;
if (*rd == 10)
++f;
if (*f) {
strcpy(p[n].id, f);
cout << "p[i] id and i value" << n << p[n].id << endl;
//file.getline(p[i].name,15,'|');
//file.getline(p[i].age,5,'|');
//file.getline(p[i].address,15,'!');
//i++;
n++;
}
}
For the comparison issue, you need to compare as numbers. Consider
Code:
if (atoi(p[j].id) > atoi(p[k].id))
where atoi() converts a number as a string into an integer number.
Re: Trouble with bubble sort?
Quote:
Originally Posted by
2kaud
For the read issue, consider using something like
Code:
char rd[20];
while (file.getline(rd, 5, '|'))
{
char *f = rd;
if (*rd == 10)
++f;
if (*f) {
strcpy(p[n].id, f);
cout << "p[i] id and i value" << n << p[n].id << endl;
//file.getline(p[i].name,15,'|');
//file.getline(p[i].age,5,'|');
//file.getline(p[i].address,15,'!');
//i++;
n++;
}
}
For the comparison issue, you need to compare as numbers. Consider
Code:
if (atoi(p[j].id) > atoi(p[k].id))
where atoi() converts a number as a string into an integer number.
Sorry for the late reply had no internet for one week but tys for the answer it worked perfectly well