7 Replies - 715 Views - Last Post: 01 September 2013 - 02:19 PM Rate Topic: ***-- 1 Votes

#1 akt_vampire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-August 13

Error in simple c++ to search record.

Posted 30 August 2013 - 09:53 PM

i'm stuck for about 1 day...with simple c++ oop starter program to search ..if anyone can help me...it would be real thankful..
made in turbo c++

here is my program

#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
class student
{int rollno,marks;
char name[20];
public:
void getdata()
{cout<<"\nenter name . rollno . marks of student ";
cin>>name>>rollno>>marks;
}
void putdata()
{cout<<name<<endl;
cout<<marks<<endl;
cout<<rollno<<endl;
}
int returnrollno()
{return rollno;
}
};
void main()
{student s;
int rn;
char found='y';
ofstream ofile;
s.getdata();
ofile.open("file1.dat",ios::binary);
ofile.write((char *)&s,sizeof(s));
ofile.close();
ifstream ifile("file1.dat",ios::binary);
cout<<"\nenter the rollno to search ";
cin>>rn;
while(ifile)
{ifile.read((char *)&s,sizeof(s));
if(s.returnrollno()==rn)
{s.putdata();
found='Y';
break;
}
}
if(found=='y')
{cout<<"Not found ";

}
ifile.close();
}



sorry for replying to myself
its taking input but ....lets me enter search rollno...but doesnt gives output..

Is This A Good Question/Topic? 0
  • +

Replies To: Error in simple c++ to search record.

#2 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: Error in simple c++ to search record.

Posted 30 August 2013 - 09:57 PM

Nightmare.In.Code ;)
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,123
  • Joined: 05-May 12

Re: Error in simple c++ to search record.

Posted 30 August 2013 - 10:09 PM

Assuming that the number you are looking for is in the file, you set found to 'Y' on line 37, but your check on line 41 is checking for 'y'. 'Y' is not equal to 'y'.

Please, please, please, pick a indent style and apply it consistently. It will make reading your code much easier... specially in the future as your code gets more and more complex.

And remember that in C++, it is int main() not void main(). If you are being taught to use 'void', demand your money back and go find another teacher.
Was This Post Helpful? 1
  • +
  • -

#4 akt_vampire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-August 13

Re: Error in simple c++ to search record.

Posted 01 September 2013 - 12:45 AM

still not able to get it ...if u dont mind...can u please elaborate
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,123
  • Joined: 05-May 12

Re: Error in simple c++ to search record.

Posted 01 September 2013 - 01:41 AM

See line 37: found='Y';

See line 41: if(found=='y')

You are comparing to see if 'Y' is equal to 'y'. An upper case 'Y' is not equal to lower case 'y'.

Anyway, if you are not getting any output, the chances are that your file doesn't contain the roll number you are looking for. You could change your code to call putdata() for each of the records read in to verify that the expected record is even in the file.
Was This Post Helpful? 0
  • +
  • -

#6 akt_vampire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-August 13

Re: Error in simple c++ to search record.

Posted 01 September 2013 - 03:39 AM

i am taking 2 points in found when its equal to its initial value.ie found='y' i will use it for invalidation and found='Y' its imaginary value when its wont be small y..then it will return while loop value...am i wrong??
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,123
  • Joined: 05-May 12

Re: Error in simple c++ to search record.

Posted 01 September 2013 - 02:11 PM

Oh, I see. It's like you have:
int primeNumber = 878;

while (haveNumbersToCheck)
{
    if (numberIsPrime)
    {
        primeNumber = 877;
        break;
    }
}

if (primeNumber ==878)
{
    cout << "Number is not prime";
}



I highly recommend using better named variables and values that are more distinctive.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,123
  • Joined: 05-May 12

Re: Error in simple c++ to search record.

Posted 01 September 2013 - 02:19 PM

Anyway, here is your code better formatted:
#include<iostream>
#include<fstream>

using namespace std;

class student
{
    int rollno, marks;
    char name[20];

public:
    void getdata()
    {
        cout << "\nenter name . rollno . marks of student ";
        cin >> name >> rollno >> marks;
    }

    void putdata()
    {
        cout << name << endl;
        cout << marks << endl;
        cout << rollno << endl;
    }

    int returnrollno()
    {
        return rollno;
    }
};

int main()
{
    student s;
    int rn;
    char found='y';

    ofstream ofile;

    s.getdata();

    ofile.open("file1.dat",ios::binary);
    ofile.write((char *)&s,sizeof(s));
    ofile.close();

    ifstream ifile("file1.dat",ios::binary);

    cout << "\nenter the rollno to search ";
    cin >> rn;

    while (ifile)
    {
        ifile.read((char *)&s,sizeof(s));
        if (s.returnrollno()==rn)
        {
            s.putdata();
            found='Y';
            break;
        }
    }

    if (found=='y')
    {
        cout<<"Not found ";
    }

    ifile.close();
}



My advice was for you to either step through the while loop on lines 50-59 with a debugger, OR to put in a call to s.putdata() between lines 52 and 53 so that you can see what data is being read back into the file. I'm thinking that data you are reading back from the file is not matching the number you are looking for.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1