What would be the possible reason why my code display two reasults whenever i search for a record that is in the second row or higher, first it display the first records and then it display the record i am searching.

example output:

->Enter ISBN : A200

then the output would become:

->Book Number : A100
->Book Title : The_Book_of_life
->Author : Me
->Book Number : A200
->Book Title : My_House
->Author : You

then with an experiment I add put a value for X which is X=5, then it works! but i don't know the reason why, i just need some explanation... thanks in advance..

hears my record data... and my code...

A100 The_Book_of_life Me
A200 My_House You
A400 The_jurney_to_the_west Philip
A300 Ghost Matrical

# Heading Here #
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>

FILE * txtFile;

char ISBN[50], Data[50];
int txtScan=1, recScan;
int x, rate;

int main()
{
    clrscr();
    cout << "\n\n\tEnter ISBN : ";
    cin >> ISBN;
    txtFile = fopen("Book.txt","r+");
    if (txtFile != NULL)
    {  do {
            txtScan = fscanf(txtFile, "%s", Data);
            recScan = strcmp(Data,ISBN);
            if (recScan == 0) { x=1; }
            if (x==1)
                { cout << "\nBook Number : " << Data; }
            else if (x==2)
                { cout << "\nBook Title : " << Data; }
            else if (x==3)
                { cout << "\nAuthor : " << Data;}
            x = x + 1;
        }
        while(txtScan != EOF);
    }
    fclose(txtFile);
    getch();
}

another thing, how could i display an output like the one below without altering the textfile...

sample output:

->Book Title : My_House
->Book Number : A200
->Author : You

it will display first the book title before the ISBN number.

Your paraphrasing of the output is incorrect, and that hides a critical piece of information in figuring out what's wrong. Specifically "Book Number : A100" would not be part of the output. The output for A200 would actually be:

Enter ISBN : A200
Book Number : The_Book_of_life
Book Title : Me
Book Number : A200
Book Title : My_House
Author : You

Notice how the number of the first "match" is actually the name, and the title is the author. This suggests that your process for moving through the file is resulting in false positives.

Following that logic, it's easy to see that by constantly incrementing x (when it's clearly intialized to 0), eventually you'll hit a number that places execution inside the conditional statements regardless of the current value of Data. The solution is to use direct comparison in all cases and not try to be clever with a global counter. For example:

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

vector<string> split(const string& s, char delim)
{
    vector<string> result;
    istringstream iss(s);
    string part;

    while (getline(iss, part, delim))
        result.push_back(part);

    return result;
}

int main()
{
    ifstream in("test.txt");

    if (in) {
        string line;
        string isbn;

        cout << "\n\n\tEnter ISBN: ";
        cin >> isbn;

        while (getline(in, line)) {
            vector<string> parts = split(line, ' ');

            if (parts.at(0) == isbn) {
                cout << "Book Number: " << parts.at(0) << '\n'
                     << "Book Title: " << parts.at(1) << '\n'
                     << "Book Author: " << parts.at(2) << endl;
            }
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.