Program to open large text document and find a city name.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • midknight5
    New Member
    • Oct 2007
    • 25

    Program to open large text document and find a city name.

    This is my first program to take advantage of functions. For this program I am required to open up a text document called "station.tx t" which has numbers and names in various rows scattered about it. the user is allowed to input a name of a city and the program should look for the city throughout the entire file. Now since the text file has a header, I know that I must get rid of this header file to continue looking through the file. So here is my function thus far:

    Code:
    // *******************************************************************
    //                                                                   *
    // This function is for cityreport.                                  *
    //                                                                   *
    // *******************************************************************
    
    void cityreport()
    {
     char cityname[MAX], line[MAX];
     int number;
     fstream indata;
     
     cout << "Please enter a U.S. city: ";
     cin.ignore ();
     cin.getline (cityname, MAX);
     cout << endl;
     cout << "You entered " << cityname << endl;
     
     indata.open("station.txt");
     indata.getline(line, MAX, '\n');
     
     indata >> number;
     cout << number << endl;
     for (int i = 0; i < 5; i++)
     {
      indata.getline(line, MAX, '\t');   
     }
     indata.ignore();
     indata.getline(cityname, MAX, ',');
     
     indata.ignore(MAX, '\n');
     cout << cityname << endl;
     
     return;    
    }
    Now when I test, the output for cityname i get is: "LAMAR
    I dont want the quotations there, is there any way to get rid of them?
    Last edited by midknight5; Oct 23 '07, 12:10 AM. Reason: Update
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    I don't see any quotes in the code, so I assume the quote is in the file. If so, then you have to deal with it like any other character.

    You code is parsing on commas and <end of line> so why is the quote character in the file? Strings in files don't need quotes around them. If you added the quotes to the file, then get rid of them in the file.

    Comment

    • midknight5
      New Member
      • Oct 2007
      • 25

      #3
      Originally posted by RRick
      I don't see any quotes in the code, so I assume the quote is in the file. If so, then you have to deal with it like any other character.

      You code is parsing on commas and <end of line> so why is the quote character in the file? Strings in files don't need quotes around them. If you added the quotes to the file, then get rid of them in the file.
      I wish it were my choice to get rid of the quote in the code but it isnt, I did however get it work.

      Ok, now that I have the name of the city in cityname, I have to have the code repeat the process untill if finds a match, so I figured a do/while would be fine.

      Code:
      void cityreport()
      {
       char cityname[MAX], line[MAX];
       char city;
       int number;
       fstream indata;
       
       cout << "Please enter a U.S. city: ";
       cin.ignore ();
       cin.getline (cityname, MAX);
       
       cout << endl;
       cout << "You entered " << cityname << endl;
       
       indata.open("station.txt");
       indata.getline(line, MAX, '\n');
       
       do
       {
        indata >> number;
        cout << number << endl;
        for (int i = 0; i < 5; i++)
        {
         indata.getline(line, MAX, '\t');   
        }
        indata.ignore();
        indata.ignore();
        indata.getline(cityname, MAX, ',');
        city = cityname;
        indata.ignore(MAX, '\n');
        cout << cityname << endl;
        cout << endl;
       } 
       while(strcmp(city, cityname));
       
       return;    
      }
      However, I get this error that I have never seen before:
      Line29 - city = cityname; invalid conversion from `char*' to `char'.

      What does this mean?
      Last edited by midknight5; Oct 23 '07, 08:47 PM. Reason: Update

      Comment

      • midknight5
        New Member
        • Oct 2007
        • 25

        #4
        Ok, I fixed it to a point, it now successfully compiles but now it is staying in the do-while loop forever and im not sure why! It successfully places the id number that relates to a city in the variable number, and puts the proper name of the city in cityname, but it seems like they arent comparing correctly.

        Code:
        void cityreport()
        {
         char city[MAX], line[MAX], cityname[MAX];
         ifstream indata;
         int number;
         
         cout << "Please enter the name of a U.S. city: ";
         cin.ignore();
         cin.getline(city, MAX);
         cout << city;
         cout << endl;
         
         indata.open("station.txt");
         indata.getline(line, MAX, '\n');
         
         do
         {
         indata >> number;
         for (int i = 0; i < SKIP; i++)
         {
           indata.getline(line, MAX, '\t');
         }
         indata.ignore();
         indata.ignore();
         indata.getline(cityname, MAX, ',');
         indata.ignore(MAX, '\n');
         }
         while(strcmp(city, cityname) ); 
         
           
         
         
         return;    
        }

        Comment

        • RRick
          Recognized Expert Contributor
          • Feb 2007
          • 463

          #5
          What's with the while loop at line 28? You'll get stuck in that loop forever, when the two strings are the same.

          Wouldn't a 'if' statement work instead?

          Comment

          • midknight5
            New Member
            • Oct 2007
            • 25

            #6
            Actually ive made some good progress, I noticed that error in the while loop and changed it, so now im working on the second portion and I get this error:

            ISO C++ forbids comparison between pointer and integer

            Code:
            if (!strcmp(city, cityname))
             {
              cout << "City Found" << endl;
              
              indata.close();
              indata.clear();
              
              indata.open("200001hourly.txt");
              indata.getline(line, MAX, '\n');
              
              while(indata >> wNum)
              {
               cout << wNum << endl;  
               cout << number << endl;
               if (wNum == number)
               {
                for (int i=0; i < STATESKIP; i++)
                {
                 indata.ignore(MAX, ',');   
                }
               indata.getline(temp, MAX, ',');
               
               if(temp == '-')
               {
                count++;
                sum+=atof(temp);
                cout << temp << " " << sum << endl;
               }         
               }
               indata.ignore(MAX,'\n');           
              }
                       
             }
            The variable temp is a character array set at 10 so char temp[10];
            When the program reaches line 23 i need to test if there is either a number in temp, or if there is a dash in temp, if there is a dash then I need to not count++ it or try to add it into the sum, and if it is not a dash then do the opposite.

            Comment

            • RRick
              Recognized Expert Contributor
              • Feb 2007
              • 463

              #7
              [code=cpp]if(temp == '-')[/code]

              You're close, but you are comparing a pointer to char[10] with a char. The compiler won't like that.

              Since you want to compare the first character in temp, then do that.
              [code=cpp]if(temp[0] == '-')[/code]
              The compiler will accept this.

              Comment

              Working...