Help with error checking code

Reply

Join Date: Aug 2004
Posts: 5
Reputation: Frogstar is an unknown quantity at this point 
Solved Threads: 0
Frogstar Frogstar is offline Offline
Newbie Poster

Help with error checking code

 
0
  #1
Aug 16th, 2004
Hi,

I'm trying to compare two text files (outputs from a database and corresponding spatial table in a GIS) to check for errors. Basically, i'm assuming that the output from the database is correct and any missing/repeat numbers in the spatial table will be errors and should be reported. I've written the part of the program that searches for a specific number in the spatial output and returns the errors, but i can't work out how to change the number it's searching for.

Here's what i've done so far:

  1. #include <string>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <stdlib.h>
  5. using namespace std;
  6.  
  7. fstream DBinput("DB.txt", ios::in);
  8. fstream SPinput("SP.txt", ios::in);
  9. fstream output("Errors.txt", ios::out);
  10.  
  11. int i = 0;
  12. string correct;
  13. string word;
  14.  
  15.  
  16. void count(void);
  17. void SPcompare(void);
  18.  
  19. void main(void)
  20. {
  21. while(DBinput >> correct)
  22. {
  23. //////// Presumably this is where the 'find number to search for' part should come in ////////
  24. SPcompare();
  25. }
  26.  
  27. }
  28.  
  29. void SPcompare(void)
  30. {
  31. while(SPinput >> word)
  32. {
  33. count();
  34. }
  35.  
  36. if(i == 0)
  37. output << "Error - '" << word << "' has no matches in spatial database" << '\n';
  38. else if(i > 1)
  39. output << "Error - '" << word << "' has " << i << " matches in spatial database" << '\n';
  40. }
  41.  
  42.  
  43. void count(void)
  44. {
  45. if(word == "5")
  46. i++;
  47. }

As you can see, so far it only searches for how many times the specific number 5 appears in the SP.text file.
What i'd like it to do is read the DB.txt file, find the first number to search for, then find out how many times it appears in SP.txt, then move on to the next number in DB and repeat until the end, generating a txt file output of all the errors. The SP.txt file has every number containted within inverted commas, and the DB.txt file has each number at the beginning of a new line follow by a lot of " delineated text/

Any help would be greatly appreciated, as would any 'you're making this a lot more complicated that it needs to be, here's how you can solve your problem in fifteen seconds'

P.S apologies for length
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 5
Reputation: Frogstar is an unknown quantity at this point 
Solved Threads: 0
Frogstar Frogstar is offline Offline
Newbie Poster

Re: Help with error checking code

 
0
  #2
Aug 16th, 2004
On the subject of an easier solution, is there some way that i can use string compare... ie something like:

  1. if(strcmp(correct, word) == 0)
  2. cout << "Number has no errors" << '\n';

Or will this try and compare the entire document or line or something? How can i narrow it down to only comparing individual values within the file?
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Help with error checking code

 
0
  #3
Aug 16th, 2004
Can we see a couple of lines of example for each file? Sounds like you want to
  1. open db
  2. while lines left in db
  3. read next line of db
  4. trim off everything after the leading number
  5. open sp
  6. while lines left in sp
  7. read next line of sp
  8. (is there one number per line? several?)
  9. parse next/only number from that line
  10. COMPARE NUMBER FROM SP WITH NUMBER FROM DB
  11. (go on to next number in the same line?)
  12. end
  13. close sp
  14. end
  15. close db
  16. print totals
seems like the easiest part of this is the compare! :-)
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 5
Reputation: Frogstar is an unknown quantity at this point 
Solved Threads: 0
Frogstar Frogstar is offline Offline
Newbie Poster

Re: Help with error checking code

 
0
  #4
Aug 17th, 2004
That's pretty much exactly what i want to do, except there's not guarantee that the two numbers that need comparing will be on the same line, so using the DB file as a master it should:
  1. while lines left in db
  2. read next line of db
  3. trim off everything after the leading number
  4. open sp
  5. while lines left in sp
  6. search SP for leading number in DB
  7. if number appears only once then proceed to find next number in DB, otherwise output an error to a txt file
  8. end

SP file is in the form:

  1. "6853"
  2. "6854"
  3. "6855"
  4. "6856"
  5. "6857"
  6. "6858"
  7. "6859"
  8. "6860"
  9. "6861"
  10. "6862"
  11. "6863"
  12. "6864"
  13. "6865"
  14. "6866"
  15. "6867"
  16. "6868"
  17. "6869"
  18. "6870"
  19. "6871"

DB file is in the form:

  1. 299 "Planning" "Land Use" "Urban Land Uses" "Industry & Commerce" "Retailing" "n/a" "1" "n/a" "n/a" "n/a" 0 0 "n/a" "n/a" "" "" "n/a" "28/04/2003" "" 0 0 0 0 0 0 "Land use baseline report"
  2. 300 "Planning" "Land Use" "Urban Land Uses" "Industry & Commerce" "Industry" "n/a" "2" "n/a" "n/a" "n/a" 0 0 "n/a" "n/a" "" "Associated carpark" "n/a" "28/04/2003" "" 0 0 0 0 0 0 "Land use baseline report"
  3. etc
And i'm trying to extract the 299, 300, etc numbers to search for in the other one (real files contain many thousands of entries to be compared). Also the SP numbers will not neccessarily be in order (almost certainly won't be, actually). That's pretty much the nature of the beast...

Hope that made sense...
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Help with error checking code

 
0
  #5
Aug 17th, 2004
since the numbers in the db file are at the BEGINNING of the line, you could just use the library function 'atoi()' to get the number:

string lineFromDBFile;
. . .
int numberToLookFor = atoi( lineFromDBFile.c_str() );

For the SP file, if you KNOW that the numbers are always at the start of the line, AND they are always within quotes like shown, you could also use atoi():

int numberInSPFile = atoi( lineFromSPFile.c_str() + 1 ); // +1 skips the leading quote

Then you can say
[code]
bool numberFoundAlready = false;
...
<fetch next line from SP file>
...
if (numberInSPFile == numberToLookFor)
{
if (numberFoundAlready)
{
// do what you want when a duplicate is found
}
else
{
numberFoundAlready = true;
}
}
<go back for more SP lines>
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Help with error checking code

 
0
  #6
Aug 17th, 2004
RATS! Forgot the closing code bracket so it looks awful. Sorry.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 5
Reputation: Frogstar is an unknown quantity at this point 
Solved Threads: 0
Frogstar Frogstar is offline Offline
Newbie Poster

Re: Help with error checking code

 
0
  #7
Aug 18th, 2004
Cheers for that, i'll give it a whirl this evening when i'm near a c compiler (or i sort it out at work...).

In the meantime, i've made some changes to the database so it outputs to this program a lot more neatly, so now both text files contain numbers in the form:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. etc

with no uselesse data or " marks in it.

With that in mind, does anyone have any ideas why this doesn't work;

  1. #include <string>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <stdlib.h>
  5. using namespace std;
  6.  
  7. fstream DBinput("DB.txt", ios::in);
  8. fstream SPinput("SP.txt", ios::in);
  9. fstream output("Errors.txt", ios::out);
  10.  
  11. int i = 0;
  12. string correct;
  13. string word;
  14.  
  15. void main(void)
  16. {
  17. while(DBinput >> correct)
  18. {
  19. while(SPinput >> word)
  20. {
  21. if(correct == word)
  22. i++;
  23. }
  24.  
  25.  
  26. output << "There are "<< i << " matches for " << correct << "." << '\n';
  27. i = 0;
  28. }
  29.  
  30. }

When it runs it makes an output file something like:

  1. There are 1 matches for 1.
  2. There are 0 matches for 2.
  3. There are 0 matches for 3.
  4. There are 0 matches for 4.
  5. There are 0 matches for 5.
  6. There are 0 matches for 6.
  7. There are 0 matches for 7.
  8. etc

So it's only counting the first number from DB.txt, ie not updating the 'correct' string in the comparison if statement, and i don't understand why not, seeing as it's updating it in the cout statement.

Any ideas?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,306
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 228
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Help with error checking code

 
0
  #8
Aug 18th, 2004
Originally Posted by Frogstar
So it's only counting the first number from DB.txt, ie not updating the 'correct' string in the comparison if statement, and i don't understand why not, seeing as it's updating it in the cout statement.

Any ideas?
It looks to me like you are finding a value correct from DBinput, and then going inputting all words into word from SPinput. When you get to the end of SPinput, you stay at the end, but then grab the next word and put it into correct. Since SPinput is still at the end, nothing will be put into word, and the inner while loop will never execute -- only the initial time. I think you need to rewind SPinput after you've looked through it.

I might try something like this.
  1. #include <string>
  2. #include <fstream>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main(void)
  8. {
  9. ifstream DBinput("DB.txt");
  10. ofstream output("Errors.txt");
  11.  
  12. string correct;
  13. while ( DBinput >> correct )
  14. {
  15. ifstream SPinput("SP.txt");
  16. int i = 0;
  17. string word;
  18. while ( SPinput >> word )
  19. {
  20. if ( correct == word )
  21. i++;
  22. }
  23. cout << correct << ": " << i << endl; // for debugging
  24. output << "There are "<< i << " matches for " << correct << "." << '\n';
  25. }
  26. return 0;
  27. }
[edit]And don't use void main().
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC