I had been out of school for about ten years and decided to return to college and major in programming. Good choice but I've been in over my head since day one. In any event I need help in accessing the text file then compairing user input with text files for verification. In the exact words of my assigment, "prompt user for store id and if valid print store's information to screen. Here's what I have so far. Please don't laugh at me..I've just started and at the point where i'm trying everything.

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

struct Store{                //Store Structure
    int storeid;             //Structure members
    char storename[20];
    char storeaddress[30];
    char citystate[30];
    int zipcode;
};

int main()
{
    
    cout <<"         WELCOME TO NORMAN'S CONSUMER CLEARINGHOUSE         "<<endl;
    cout <<"____________________________________________________________"<<endl;

    cout<<"Enter your store identification to procede:"<<endl;
    cin>>storeid;


ifstream input;
input.open("stores.txt"); //open store file

if (input.fail())
{
   cout <<"File does not exist."<<endl;
   cout <<"Exit Program."<<endl;
   return 0;
}



while (!input.eof()) //continue if not end of file
{
input.getline(storeid, 7, "#");
cout <<storeid <<endl;
}

input.close();







      system("PAUSE");
      return 0;
}

Recommended Answers

All 5 Replies

lines 1, 2 and 3. Current c++ standards do not allow for .h extension for system files. Unless you are using a very old compiler such as TurboC++ then it should be this:

#include <istream>
#include <cstdlib>
#include <fstream>
using namespace std; // optional
using std::cout;
using std::fstream;
using std::ifstream;
// etc

line 35: that's now how eof() works. You don't have to use eof() at all. Note that storeid is undefind in your program. Since storeid in your structure is an int, not a char array, use the >> operator to extract it from the file.

int storeid;
while( input >> storeid )
{

}

Thank you. I'll get on it now and let you know how it goes. :icon_smile:

Ok..i'm still stuck..i can't find a way to validate (code) that user input matches the store id numbers in the text file or that it doesn't. Here is my current coding, as well as the actual text file that I'm accessing. I need to read this text file and determine if the user has keyed in a valid number. Thanks for any help you can provide. My head hurts..:confused:

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>


struct Store{                //Store Structure
    int storeid;             //Structure members
    char storename[20];
    char storeaddress[30];
    char citystate[30];
    int zipcode;
}; Store your;

int main()
{
    int storeid;
    cout <<"         WELCOME TO NORMAN'S CONSUMER CLEARINGHOUSE         "<<endl;
    cout <<"____________________________________________________________"<<endl;

    cout<<"Enter your store identification to procede:"<<endl;
    cin>>storeid;


ifstream input;
input.open("stores.txt"); //open store file

if (input.fail())
{
   cout <<"File does not exist."<<endl;
   cout <<"Exit Program."<<endl;
   return 0;
}


while(input >> storeid)
{
cout <<storeid <<endl;
}

system("PAUSE");
return 0;
}

This is a copy of the stores.txt file.

123456 Cleveland Clearinghouse 123 Cotton Row Cleveland MS 38732 6621234567
123457 Oxford Outlet Store 456 Jackson Ave. Oxford MS 38655 6628019001
123458 Memphis MegaMart 789 Elvis Presley Bl Memphis TN 98765 5012340987

1) in line 36 you have to use a different variable than you did on line 22 so that you can compare the two numbers. input >> storeid only reads the store id from the file. In order to read the second store id you have to read all the rest of the line so that the file pointer is moved to the beginning of the next line. getline() will do that for you.

string line;
int num;
while(input >> num)
{
      getline(input, line); // read the rest of the line
      cout << num <<endl;
}

Ok...either I'm too tired or mentally lacking. Going to try to get some input from my professor in the morning on what I have so far. Most likely will wake up later tonight to do a bit more. Perhaps with meditation the solutions become clearer to me. Just wanted to say thank you for taking the time to help me and be well. I'll certainly visit this forum on a regular basis with the hope that I can eventually help someone as well.

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.