Thread: Comparing

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    3

    Comparing

    Hi, I'm having trouble with comparing a character received from a file. I know, newbie problem. Anyway, I have this code:

    Code:
    	while(tf) {  
    		if (a_file.get() == '"') {
    			count++; cout <<"got one\n"; }
    
    		if (count == 3)
    			tf = 0;
    	}
    cout <<"now at first position\n";
    I'm not getting that last output though. I'm fairly sure that i'm comparing a_file.get() wrong.

    What i'm trying to do is count 3 "s and then break. But it's not counting them, so obviously i'm comparing wrong. Any help? Thanks. Oh i'm also having trouble comparing newlines aswell.

    I've tried == " \" " but that doesn't work.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    if (a_file.get() == '\"')
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Comparing

    Originally posted by pengu
    Hi, I'm having trouble with comparing a character received from a file. I know, newbie problem. Anyway, I have this code:

    Code:
    	while(tf) {  
    		if (a_file.get() == '"') {
    			count++; cout <<"got one\n"; }
    
    		if (count == 3)
    			tf = 0;
    	}
    cout <<"now at first position\n";
    I'm not getting that last output though. I'm fairly sure that i'm comparing a_file.get() wrong.

    What i'm trying to do is count 3 "s and then break. But it's not counting them, so obviously i'm comparing wrong. Any help? Thanks. Oh i'm also having trouble comparing newlines aswell.

    I've tried == " \" " but that doesn't work.
    You don't seem to be comparing wrong unless the return value of the function is not a char or int (\" is no different than " in this instance)

    I'd guess count is not initialized properly.

    Your code formatting is inconsistant and should also be corrected.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    3
    Yeah, I know my code is sloppy etc. I've just added crap in there so i can get it working and then i'll clean it up.

    count is initialised at the start as "int count = 0;"

    I've isolated the problem a little more. here's the code I have now

    Code:
    while (cont) {
    cout <<"in while loop!";
    	while(tf) {  
    	cout <<" .... in second while loop!\n";
    		if (a_file.get() == '\"') {
    			count++;
                cout <<"got one\n"; }
    
    		if (count == 3) {
    			tf = 0; }
    	}
    "in while loop" prints once, then "....in second while loop" keeps being printed infitately.

    I've checked that the file is opened, and it has been. I've used fstream a_file(filename, ios::ate, ios::in, ios:ut). The first line of the file contains "s. many of them. So i don't know what's going wrong.

  5. #5
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Methinks you are missing the last curly brace
    Code:
    while (cont) {
    cout <<"in while loop!";
    while(tf) {  
    cout <<" .... in second while loop!\n";
    if (a_file.get() == '\"') {
    count++;
                cout <<"got one\n"; }
    
    if (count == 3) {
    tf = 0; }
    }
    } //<<===== Missing this curly brace
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  6. #6
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Code:
    while (cont) 
    {
         cout <<"in while loop!";
         while(tf) 
         {  
              cout <<" .... in second while loop!\n";
              if (a_file.get() == '\"') 
              {
                   // What happens when we do find a quotation mark
                   count++;
                   cout <<"got one\n"; 
              }
              
              // But what happens when we dont?
    
              if (count == 3) 
              {
                   tf = 0; 
              }
         }
    }                    // The missing brace, which I think was there
    What happens if you dont get another " in the file? And whats 'tf'? If its just an integer, UNLESS count == 3 the loop will go forever. If tf is some sort of file handle I'm not so sure, you may want to check the FAQ on file handles. Make sure you have something in there on what to do when you dont find a ", like incrementing another counter and stopping after a few million loops?

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by pengu
    Yeah, I know my code is sloppy etc. I've just added crap in there so i can get it working and then i'll clean it up.
    How about clean it up now so you can see what the problems are!
    Your problem is being hidden in your sloppy style. Reformat and it will become obvious what the problem is, and believe me, taking 15 seconds to clean it up is not worth waiting for. Take more pride in your work.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Jan 2004
    Posts
    3
    Ok I figured out the problem. The cursor wasn't at the start of the file when being opened, so I had to place it there to work properly.

    I have one more question though... I'm having trouble with reading a file, line by line, and determining at the end of each line whether or not to go to the next one.

    I know there are 3 possibilities: (EOF, \n then EOF, or \n then the next line).

    I have a large program loop that loops through each line.

    it's while(cont) .... and at the end i'm trying to do these \n or EOF checks to set cont to 0.

    Any suggestions on how to do this? Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing data in two files
    By nynicue in forum C Programming
    Replies: 25
    Last Post: 06-18-2009, 07:35 PM
  2. bit vs bytes when comparing a file
    By Overworked_PhD in forum C Programming
    Replies: 6
    Last Post: 05-19-2007, 11:22 PM
  3. comparing bitmaps
    By bigSteve in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 10:40 AM
  4. comparing problem
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 03-05-2002, 06:19 AM
  5. comparing struct members
    By breed in forum C Programming
    Replies: 4
    Last Post: 11-22-2001, 12:27 PM