Thread: char string problem

  1. #1
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470

    char string problem

    Hi!

    Please check out the code below.

    Code:
    #include<iostream>
    #include<string>
    #include<fstream>
    using namespace std;
    
    void main()
    {
    	char input[20]="\0\0";
    	cout<<"English word?: ";
    	cin>>input;
    	ifstream fs;
    	char let[2]="\0";
    	char wrd[20]="\0\0";
    	int i=0;
    	fs.open("lib.txt", ios::in);
    	while(fs.peek()!='.') //check word end
    	{
    		fs.read(let,1);
    		wrd[i] = let[0]; //create word
    		i++;
    	}
    	if(input==wrd)
    	{
    		cout<<wrd;
    	}
    }
    The problem is that the if-statement doesn’t detect it when input and wrd
    seems to contains the same string. Please show me my fault.

    Thanks in advance.

    -geek@02

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by geek@02
    Hi!

    Please check out the code below.

    Code:
    #include<iostream>
    #include<string>
    #include<fstream>
    using namespace std;
    
    void main()
    {
    	char input[20]="\0\0";
    	cout<<"English word?: ";
    	cin>>input;
    	ifstream fs;
    	char let[2]="\0";
    	char wrd[20]="\0\0";
    	int i=0;
    	fs.open("lib.txt", ios::in);
    	while(fs.peek()!='.') //check word end
    	{
    		fs.read(let,1);
    		wrd[i] = let[0]; //create word
    		i++;
    	}
    	if(input==wrd)
    	{
    		cout<<wrd;
    	}
    }
    The problem is that the if-statement doesn’t detect it when input and wrd
    seems to contains the same string. Please show me my fault.

    Thanks in advance.

    -geek@02
    1) This is wrong. Read the FAQ.
    2) You cannot compare character arrays with the == operator. You'll have to use something like strcmp.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Better yet, since you already include the <string> header anyway, you should use the std::string class.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470
    Thanks for that.

    Yet another problem cropped up. I changed the above code a bit:
    Code:
    #include<iostream>
    #include<string>
    #include<fstream>
    using namespace std;
    
    void main()
    {
    	char input[20]="\0";
    	cout<<"English word: ";
    	cin>>input;
    	ifstream fs;
    	char let='\0';
    	char wrd[20]="\0";
    	int i=0;
    	fs.open("lib.txt", ios::in);
    	
    	while(!fs.eof())
    	{
    		while(fs.peek()!=':') //check word end
    		{
    			let = fs.get();
    			wrd[i] = let; //create word
    			i++;
    		}
    		if(!strcmp(input, wrd))
    		{
    			cout<<wrd;
    		}
    		fs.seekg(1);
    		i=0;
    		for(int k=0; k<=20; k++)
    		{
    			wrd[k]='\0';
    		}
    	}
    }
    Everything works fine during the 1st go of the outer while loop. But in the 2nd time looping, after get() is executed, the file pointer seems to jump 2 positions backwards. Why is this happening please?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by geek@02
    Everything works fine during the 1st go of the outer while loop. But in the 2nd time looping, after get() is executed, the file pointer seems to jump 2 positions backwards. Why is this happening please?
    Code:
    while(!fs.eof())
    {
        while(fs.peek()!=':') //check word end
        {
            let = fs.get();
            wrd[i] = let; //create word
            i++;
        }
        if(!strcmp(input, wrd))
        {
            cout<<wrd;
        }
        fs.seekg(1);
        i=0;
        for(int k=0; k<=20; k++)
        {
            wrd[k]='\0';
        }
    }
    Just a guess, but... because you told it to (see code highlighted in red above). Of course it would help to see what your input file looks like to verify that.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    You should never loop on eof(), if you want to read untill you run out of things to read you loop untill you cannot read anymore, then check eof() to see if the reason you stopped was because you ran out of things to read. My guess is that you are using seekg to skip a position, and as hk_mp5kpdw pointed out you are actually moving to the second char in the file.

    fortunately if you use std::string this becomes a good bit easer, I think this is what you are trying to do
    Code:
    int main() {
        using namespace std;
        cout << "Enter a word to search for in file\n:";
        string input;
        if(cin >> input) {
            ifstream fs("lib.txt");
            int wordcount=0;
            string word;
            while(getline(fs,word,':')) {
                if(input == word) {
                    cout << "word #" << wordcount << " == " << word << endl;
                }
                ++wordcount;
            }
        } else {
            cerr << "standard input failure, giving up" << endl;
            return 2;
        }
        return 0;
    }
    This version requres that there be no whitespace in lib.txt, and is case sensitive. Input ignores whitespace. Thus if lib.txt contains "pear: apple:grape" and the user enters " apple" input will be equal to "apple" and word will be " apple".

    getline consumes it's terminator. The string returned does not contain the terminator. If the file ends before a terminator, but characters were read then eof() is true but so is good() If you loop on eof() you will exit before handling trailing characters. If the final character is a terminator then eof() is false and you will expect more data where none exists.

  7. #7
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470
    Yes I’m using seekg() to skip ‘:’. Now I replaced it with another get().
    Thanks for the eof() explanation and all, guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  5. String Processing Problem
    By muffin in forum C Programming
    Replies: 0
    Last Post: 08-24-2001, 10:13 AM