Question

I have a txt file which contains the name and roll number of students. I want to read and display a particular roll number from his file. It shows only the first roll number, but I want to read the roll number of the 2nd person.

That is, if I want to read the roll number of "ss", it shows the roll number of the first person

The program is

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

void student_read()
{
    clrscr();
    char name[30], n[30], temp[30];
    int i, roll_no, code, count=0;

    ifstream fin("tt.txt",ios::in|ios::beg);
    if(!fin)
    {
        cout << "cannot open for read ";
        return;
    }
cout << "Enter the name of student" << "\n";
        cin >> n;

    while(fin >> name >> roll_no)
    {
        cout << roll_no << endl;
    }



    if(string[name] == string[n])
    {
        cout << "roll no" << "\n" << roll_no;
    }
    else
        cout << "Not found";
}

void main()
{
    clrscr();
    cout << "Students details is" << "\n";
    student_read();
    getch();
}

The txt file contains this data:

sourav 123 ss 33

Was it helpful?

Solution

Does you have end of each line in your text file? Does you have sourav 123 ss 33 or sourav 123\nss 33?And this if(n[30]==name[30]) compare only 1 character in string.

OTHER TIPS

You're doing the output of what is in the file already before you even input the name to search for.

Reorder your statements, like this:

cout<<"Enter the name of student"<<"\n";
cin>>n;
while(fin>>name>>roll_no)
{
    //...

Also, if you only want to output one name and roll_no, in your loop, you have to check some kind of condition whether to print or not. At the moment, your code should actually print the roll_no of all rows in the file, and possibly sometimes the last one twice.

So the condition you have after the input belongs into the loop.

Additionally, however, you're only comparing the 31st character of the char array (which is actually already out of the bounds of your array variables! Their indices go from 0..29, i.e. even if you allocated a 30 characters array, the ). That means, your condition will be true if the next to last character matches. This place will most likely not be initialized yet, so you compare basically gargabe values and will get unexpected/random results.

If you want to, as the description suggests, want to compare the whole char array, that works differently by the way (not with the == operator, that would only compare pointer addresses), you'd need to use the strcmp function. But even better would be to use std::string instead of char *.

void student_read()
{
    clrscr();
    std::string name, n, temp;
    int i, roll_no, code, count = 0;

    std::ifstream fin("tt.txt", ios::in | ios::beg);

    if (!fin)
    {
        std::cout << "cannot open for read ";
        return;
    }

    std::cout << "Enter the name of student" << "\n";
    std::cin >> n;

    while (fin >> name >> roll_no)
    {
        std::cout << roll_no << std::endl;
    }

    if (name == n)
    {
        std::cout << "roll no" << "\n" << roll_no;
    }
    else
        std::cout << "Not found";
}

int main()
{
    clrscr();
    std::cout << "Students details is\n";
    student_read();
    getch();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top