I'm helping a friend out with some code and I'm having the hardest time figuring out how to do this. I know it is simple, but for the life of me I can't remember how to do it. Any help or advice would be welcomed. Thanks for the help in advance.

Problem: I need to open a file first, then have a user input a 6 digit number then check the file and if it finds it then it will get the name by it and if it doesn't exist in the file then it will say so. I'm having trouble having it run through and check the file. I can have it run through the whole document and output the data and I can check the very first line of data, but after that I can't get it to work at all. It will get the first three data values from the file if it isn't the first number of the file, but it won't get the name or check. I haven't got to the part about output if the number doesn't exit in the file, but I'm not concerned about that part yet. Here is my code. Thanks again for the help.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std; //helps run the program
int main()
{
       double sticker;
       string Fname;
       double first;
       string second;
       second = "not";

       ifstream dataIn;  //for the data input
       dataIn.open("HWK5.txt");

       cout << "sticker:  ";
       cin >> sticker;

       while(dataIn)
       {
                    dataIn >> first;
                    cout << first << endl;;
                    if (first == sticker)
                    {
                     dataIn >> second;
                     cout << second << endl;
                     Fname = second;
                     }
                     else if (first != sticker){
                        dataIn >> first;
                        cout << first << endl; }

       }


        cout << "here: " << Fname << endl;
       dataIn.close();
      system("Pause");
      return 0;
       }


/*

       while(dataIn)
       {
                    dataIn >> first;
                    cout << first << endl;

                      if (first != sticker){
                        dataIn >> first;
                        cout << first << endl; }
  else if (first == sticker)
                    {
                     dataIn >> second;
                     cout << second << endl;
                     Fname = second;
                     }
       }
       */

Sincerely yours;

jdm

Edited 3 Years Ago by mike_2000_17: Fixed formatting

For checking every line of the data, assuming each number has its own line, try this:

string sticker;
string line;

dataIn.open("HWK5.txt");

if (dataIn.is_open())
{
    while (! dataIn.eof() )
    {
        getline (dataIn,line);
        stickCheck = (atoi(line.c_str()));

        if(stickCheck == sticker)
        {
            //once numbers match perform your function
        }
    }
    dataIn.close();
}

Maybe I misread your question, but I gathered that you couldn't get your inputted number to check against all the values in your file.

For checking whether the number is found, you might include something like:

string sticker;
string line;

bool numFound = false;

dataIn.open("HWK5.txt");

if (dataIn.is_open())
{
    while (! dataIn.eof() )
    {
        getline (dataIn,line);
        stickCheck = (atoi(line.c_str()));

        if(stickCheck == sticker)
        {
            //once numbers match perform your function
            numFound = true;
        }
    }
    if(numFound == false)
    {
        cout << "Number not found!" << endl;
    }
    dataIn.close();
}
else
{
    cout << "Unable to open file!";
}

Hope this helps.

Thank you for your help. I will give that a try and let you know how it work.

Sincerely yours;

jdm


11

For checking every line of the data, assuming each number has its own line, try this:

string sticker;
string line;

dataIn.open("HWK5.txt");

if (dataIn.is_open())
{
    while (! dataIn.eof() )
    {
        getline (dataIn,line);
        stickCheck = (atoi(line.c_str()));

        if(stickCheck == sticker)
        {
            //once numbers match perform your function
        }
    }
    dataIn.close();
}

Maybe I misread your question, but I gathered that you couldn't get your inputted number to check against all the values in your file.

For checking whether the number is found, you might include something like:

string sticker;
string line;

bool numFound = false;

dataIn.open("HWK5.txt");

if (dataIn.is_open())
{
    while (! dataIn.eof() )
    {
        getline (dataIn,line);
        stickCheck = (atoi(line.c_str()));

        if(stickCheck == sticker)
        {
            //once numbers match perform your function
            numFound = true;
        }
    }
    if(numFound == false)
    {
        cout << "Number not found!" << endl;
    }
    dataIn.close();
}
else
{
    cout << "Unable to open file!";
}

Hope this helps.

I couldn't get it to work. Also the file looks something like this:

111111 Hill
222222 Sam

The numbers are on the same line as the name. Thanks for the help.

Sincerely yours;

jdm

This article has been dead for over six months. Start a new discussion instead.