Thread: can you help me figure this out?

  1. #1
    Registered User
    Join Date
    Jan 2009
    Location
    Staten Island New York
    Posts
    2

    Post can you help me figure this out?

    I'm a new programmer, I just was curious and wanted to write some code that would compare two .txt files char by char and check if they are identicle.
    this is the code that I wrote, Can someone help me figure out why it doesn't work?
    The compiler i'm using is microsoft visual C++ ver.6.0
    my compiler shows 0 errors 0 warnings, but the .exe crashes
    Code:
    // alfred J. Denigris
    // write a program that tests if two text files are identicle.
    // 1_27_2009
    
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    ifstream getA;
    ifstream getB;
    
    bool test(char a, char b);
    
    int main()
    {
    
    int num=0;
    char chA, chB;
    
    ifstream getA;
    ifstream getB;
    
    getA.open("fileA.txt");
    getB.open("fileB.txt");
    
    
    
    while (!getA.eof() || !getB.eof())
    {
    
    	getA.get(chA);
    	getB.get(chB);
    
    
    	if (test(chA, chB) == true)
    		cout <<" char number "<<num<<"  is "<< chA << "for file a and "<< chB << "  For file b.  TRUE" <<endl;
    		else
    		cout <<" char number "<<num<<"  is "<< chA << "for file a and "<< chB << "  For file b.  FALSE" <<endl;
    	
    num++;
    }
    	
    	
    
    getA.close();
    getB.close();
    	cout << "end";
    	return 0;
    }
    
    
    	bool test(char a, char b)
    	{	if(a==b)
    	return true;
    	else 
    		return false;	}

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you start by writing a program that reads a file and prints its contents. A few things to note while you are at it:
    • Avoid the use of global variables.
    • Indent your code consistently.
    • Do not use eof() to control the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2009
    Location
    Staten Island New York
    Posts
    2

    Found the problem

    well, besides the obvious problem of declaring the ifstream variables twice, once globaly and another time local to main. I found out the problem after reading your reply.

    The problem was that...

    are you ready for this??

    I wrote the entire thing on my 16 GB made in Hong Kong jump drive, which i now declare to be worthless.

    The data was never there to begin with and was crashing for reasons relating to that.

    Luckily i posted this code into the forum and after i restarted my computer( and lost that bit of code that i was writting) I was able to retrieve it from this forum.

    Therefore... Thank you Cprogramming.com for the ability of this forum service and your members for helping me to "solve this problem"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3 dimensional figure volume
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2009, 05:22 PM
  2. can't figure out what's with this code
    By silverstein101 in forum C++ Programming
    Replies: 8
    Last Post: 04-16-2008, 12:45 AM
  3. trying to figure out someone's code for a plugin
    By paulpars in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:57 AM
  4. I can't figure this out!
    By paulntysmom in forum C Programming
    Replies: 8
    Last Post: 04-07-2006, 03:52 AM
  5. newb to C, cant figure something out.
    By Phate4219 in forum C Programming
    Replies: 16
    Last Post: 03-06-2006, 01:47 AM