Having trouble with InFile.open

This is a discussion on Having trouble with InFile.open within the C++ Programming forums, part of the General Programming Boards category; I need help to open a text file and to search for an ID number and search by name. #ifndef ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    Having trouble with InFile.open

    I need help to open a text file and to search for an ID number and search by name.



    #ifndef _LIST_H
    #define _LIST_H

    #include <iostream>
    #include <string>
    #include <stdlib.h>
    #include <fstream>

    using namespace std;




    class List
    {
    public:
    List(); //default constructor
    //List(int ID, char *LN, char *FN, char *T, int Q, int A); //constructor
    ~List(); //deconstructor
    void Display();
    void SearchID ();
    void SearchName();


    private:
    int IdNumber;
    char LastName;
    char *FirstName;
    char *Type;
    int Quantity;
    int Amount;
    };
    #endif






    #include "List.h"


    /*--------------------------------------------------------------
    Precondition:
    Postcondition:
    --------------------------------------------------------------*/
    List :: List()
    {

    }
    /*--------------------------------------------------------------
    Precondition:
    Postcondition:
    --------------------------------------------------------------*/
    //List :: List(int ID, char *LN, char *FN, char *T, int Q, int A);
    //{

    //}
    /*--------------------------------------------------------------
    Precondition:
    Postcondition:
    --------------------------------------------------------------*/
    List ::~List()
    {

    }
    /*--------------------------------------------------------------
    Precondition:
    Postcondition:
    --------------------------------------------------------------*/
    void List :: Display()
    {
    int num;
    cout<<"Please choose from the following menu:"<<endl<<endl;
    cout<<"Enter 1 to search by Account Number."<<endl;
    cout<<"Enter 2 to search by Name."<<endl;
    cin>>num;

    if (num == 1)
    {
    SearchID();
    }
    else
    {
    SearchName();
    }


    }
    /*--------------------------------------------------------------
    Precondition: The txt file is opened
    Postcondition: Check for Error
    --------------------------------------------------------------*/

    /*--------------------------------------------------------------
    Precondition:
    Postcondition:
    --------------------------------------------------------------*/
    [CODE]void List :: SearchID()
    {
    int SearchValue, Account;
    ifstream InFile;


    InFile.open("UsersFile-hw.txt");
    cout<<"Enter an Id number:"<<endl;
    cin>>SearchValue;

    while(InFile)
    {
    InFile >> Account;

    if (InFile)
    {
    if (Account == SearchValue)
    {
    cout <<endl<<Account;
    }
    }
    else
    {
    cout<<"Error opening output file."<<endl;
    }
    }
    InFile.close();
    }/*--------------------------------------------------------------
    Precondition:
    Postcondition:
    --------------------------------------------------------------*/
    void List :: SearchName()
    {

    char buffer, IdName;
    ifstream InFile;


    InFile.open("UsersFile-hw.txt");
    cout<<"Enter Last Name, First Name:"<<endl;
    cin>>buffer;

    while(InFile)
    {
    InFile >> buffer;

    if (InFile)
    {
    if (buffer == IdName)
    {
    cout <<endl<<buffer;
    }
    }
    else
    {
    cout<<"Error opening output file."<<endl;
    }
    }
    InFile.close();
    }




    #include "List.h"

    int main()
    {
    List list;

    list.Display();

    return 0;

    }



    Down here is the text file:



    10101 Miltgen, Joseph
    moe
    750 380.81
    10102 Small, Isaac
    large
    650 598.84
    10103 Snyder, Samuel
    r2d2
    250 193.74
    10104 Edmundsen, Edmund
    abcde
    250 177.93
    10105 Braunschweiger, Christopher
    brown
    850 191.91
    10106 Pizzula, Norma
    pizza
    350 223.95
    10107 VanderVan, Henry
    dutch
    750 168.59
    10108 Freeloader, Freddie
    red
    450 76.61
    10109 Alexander, Alvin
    great
    650 405.04
    10110 Mouse, Michael
    ears
    50 42.57
    10111 Lukasewicz, Zzyzk
    rpn
    350 73.50
    10112 Christmas, Mary
    noel
    850 33.28
    10113 Adam, Joe
    eve
    750 327.53
    10114 Nijhoff, Laran
    kkid
    550 382.03
    10115 Liestma, Stan
    saab
    650 38.36
    10116 Zwier, Apollos
    PJ
    350 249.48
    10117 Jaeger, Tim
    biker
    250 246.73
    10118 VanZwalenberg, Jorge
    egypt
    850 466.95
    10119 Jester, Courtney
    joker
    450 281.16
    10120 McDonald, Ronald
    fries
    250 35.00
    10121 Nederlander, Benaut
    dutch
    550 28.82
    10122 Haybailer, Homer
    farm
    850 37.32
    10123 Spear, Bill
    shake
    450 337.01
    10124 Romeo, Juliet
    XOXOX
    150 100.19
    10125 Greek, Jimmy
    wager
    250 0.03
    10126 Virus, Vera
    worm
    750 67.35
    10127 Beech, Rocky
    wreck
    950 256.18
    10128 Engel, Angel
    wings
    150 16.39
    10129 Abner, Lil
    daisy
    950 89.57
    10130 Tracy, Richard
    crime
    550 392.00
    10131 McGee, Fibber
    molly
    750 332.12
    10132 Bell, Alex
    phone
    850 337.43
    20101 Cobb, Tyrus
    bball
    50 32.81
    20102 George, Ruth
    babe
    350 269.93
    20103 Descartes, Ronald
    horse
    250 109.34
    0104 Euclid, Ian
    greek
    350 63.63
    20105 Daniels, Ezekiel
    lions
    350 128.69
    20106 Tarzan, Jane
    apes
    150 100.31
    20107 Habbakuk, Jonah
    whale
    950 183.93
    20108 Columbus, Chris
    pinta
    850 202.24
    20109 Byrd, Dick
    north
    550 168.49
    20110 Bunyan, Pauline
    babe
    550 333.47
    20111 Chaucer, Jeff
    poem
    950 37.02
    20112 Stotle, Ari
    logic
    750 337.74
    20113 Harrison, Ben
    pres
    550 262.97
    20114 James, Jesse
    guns
    250 58.81
    20115 Scott, Francine
    flag
    350 168.11
    20116 Phillips, Phyllis
    gas66
    650 322.22
    20117 Doll, Barbara
    ken
    350 26.34
    20118 Finn, Huck
    tom
    350 22.86
    20119 Sawyer, Tom
    huck
    950 460.30
    20120 Newmann, Alfred
    mad
    450 116.00
    20121 Simple, Simon
    pie
    550 486.05
    20122 Schmidt, Messer
    plane
    250 35.31
    20124 Luther, Calvin
    ref
    777 666.66
    20125 Yale, Harvard
    ivy
    150 127.70

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    << !! Posting Code? Read this First !! >>

    i can't tell where one file ends and another begins...

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You should go and Edit the post. Delete current code, insert code tags for each file, paste the code again (hopefully it is indented), describe the problem more closely, submit.

    I don't know what your problem is, but seeing that you use char*, and a single character called buffer and idName, I would guess things go wrong there.

    Since this is C++, you should be using the standard std::string for strings. A half-an-hour reading of some basic tutorial and a one-minute search to find a reference of std::string methods and usage could gain you days and months of debugging char*-related problems.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. Replies: 6
    Last Post: 01-03-2007, 02:02 PM
  3. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 03:10 PM
  4. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 11:58 AM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21