C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
A problem with some pass by reference i think Posted by shonencd86 on 1 Jun 2006 at 5:18 PM
Hey guys, im trying to import an entire file that i have to a single string. Then take that string and search for the parts <td and </td> and with this code it compiles great......but it crashes once the second function is called. I have tested the first function and im pretty sure the other ones work as well. Do you guys see ANY mistakes or have any suggestions as to where i am supposed to go with this? thanks!


==============================================================

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;

void getfile(istream & in , string & theFile);

string getTable_entry(const string & theFile, int & pos);

string extractResult(string line);

bool containsUsefulinfo(string entry);




int main()
{
string input;
string filestring;
int pos;
cout << "Give me your input!";
cin >> input;
cout << input<< endl;
getfile(cin,input);
cout << input<<endl;

getTable_entry(input, pos=0);




return 0;
}
void getfile(istream & in , string & theFile)
{
ifstream fin(theFile.c_str());
string filestring;
while(getline(fin, theFile))
{
filestring -+= filestring + theFile;

}

}
string getTable_entry(const string & theFile, int & pos)
{
int x = theFile.find("<td ",pos);
cout << x;
int y = theFile.find("</td>",(x+1));
cout << y;
string z = theFile.substr(x,y);
cout << z;

string m = extractResult(z);

int a = theFile.find("DATE");
int b = theFile.find(" >" ,a+1);
string f = theFile.substr(b+1,b+4);

string c = f + " " + " \t " + " " + m;
pos = (y++);

return c;







}


string extractResult(string line)
{

if(line.find("Saw Shadow"))
{
string y = "Saw Shadow";
return y;
}
else
{
if (line.find("No Shadow"))
{
string y = "No Shadow";
return y;
}
else
{
string y = "Result Unknown";
return y;
}
}
}
bool containsUsefulinfo(string entry)
{
if((entry.find("1") || entry.find("2")) && (isdigit(entry[1])) && (isdigit(entry[2])) && (isdigit(entry[3])))
{
return true;
}
else
{
return false;
}
}

=====================================================================
Report
Re: A problem with some pass by reference i think Posted by stober on 1 Jun 2006 at 7:46 PM
This function is next to useless! It reads each line of the file, accumulates the contents in filestring, but then tosses it all into the bit bucket when getfile() returns to its caller!!
: 
:  void getfile(istream & in , string & theFile)
: {
:     ifstream fin(theFile.c_str());
: 	string filestring; 
: 	while(getline(fin, theFile)) 
:     {
:         filestring -+= filestring + theFile;
syntax is incorrect ^^^ in the above line.  Here is the correction.
         filestring += theFile;

: 		
:     }
: 	
: }

:

Report
Re: A problem with some pass by reference i think Posted by shonencd86 on 1 Jun 2006 at 7:59 PM
: This function is next to useless! It reads each line of the file, accumulates the contents in filestring, but then tosses it all into the bit bucket when getfile() returns to its caller!!
:
: 
: :  void getfile(istream & in , string & theFile)
: : {
: :     ifstream fin(theFile.c_str());
: : 	string filestring; 
: : 	while(getline(fin, theFile)) 
: :     {
: :         filestring -+= filestring + theFile;
: syntax is incorrect ^^^ in the above line.  Here is the correction.
:          filestring += theFile;
: 
: : 		
: :     }
: : 	
: : }
: 

: :
:
:


well i caught that mistake but the problem is that this is an assignment for school and after trying to get an answer from the TA and the professor, i decided to turn to the internet. The assignment requires me to use the void function. I'll post the website so that people can check out this lengthy assignment.


::: website :::

http://www.cs.drexel.edu/~mcs171/sp06/assignments/HW6/
Report
Re: A problem with some pass by reference i think Posted by stober on 2 Jun 2006 at 1:57 AM
This message was edited by stober at 2006-6-2 2:22:55

Interesting assignment that should be fun to write.

The file contents should be put into theFile before the function returns. Requirements also state you need to retain line breaks in the string.

: 
 void getfile(istream & in , string & theFile)
 {
     ifstream fin(theFile.c_str());
     string filestring; 
     theFile = "";
     while(getline(fin, filestring)) 
     {
         theFile += (filestring + "\n");
     }
   }




Next comment. std::string.find() does NOT return an integer -- it returns string::npos when not found. And it returns string::size_type when it is found.

string getTable_entry(const string & theFile, int & pos)
{
	string::size_type x = theFile.find("<td ",pos);
	if(x == string::npos) 
		return "";



Report
Re: A problem with some pass by reference i think Posted by shonencd86 on 2 Jun 2006 at 1:41 PM
: This message was edited by stober at 2006-6-2 2:22:55

: Interesting assignment that should be fun to write.
:
: The file contents should be put into theFile before the function returns. Requirements also state you need to retain line breaks in the string.
:
:
: 
:  void getfile(istream & in , string & theFile)
:  {
:      ifstream fin(theFile.c_str());
:      string filestring; 
:      theFile = "";
:      while(getline(fin, filestring)) 
:      {
:          theFile += (filestring + "\n");
:      }
:    }
: 

:
:
:
: Next comment. std::string.find() does NOT return an integer -- it returns string::npos when not found. And it returns string::size_type when it is found.
:
:
: string getTable_entry(const string & theFile, int & pos)
: {
: 	string::size_type x = theFile.find("<td ",pos);
: 	if(x == string::npos) 
: 		return "";
: 

:
:
:



My program still crashes and i still dont know WHERE im supposed to go. Im completely lost and just frustrated at this point and it's due tonight. If anyone can help me work through this and get this right it would be extremely appreciated!! Thanks!!


#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;

getfile(istream & in , string & theFile);

string getTable_entry(const string & theFile, int & pos);

string extractResult(string line);

bool containsUsefulinfo(string entry);




int main()
{
    string input;
    string filestring;
    int pos;
    cout << "Give me your input!";
    cin >> input;

	ifstream fin(input.c_str());
   
	getfile(fin,input);

    getTable_entry(input,pos = 0);
	

	ofstream fout("out.txt");
	
	return 0;
}
 getfile(istream & in , string & theFile)
{
                  
    string filestring;

    while(getline(in, theFile)) 
    {
        theFile+=filestring + "\n";
		cout << theFile;
		
	}
	
	
}
string getTable_entry(const string & theFile, int & pos)
{
	
		
    int x = theFile.find("</td ",pos);
	if( x > 0)
	{
    int y = theFile.find(">",(x+1));
    string z = theFile.substr(x,y);
	cout << z;

    if (containsUsefulinfo(z))
    {
        string m = extractResult(z);

        int a = theFile.find("DATE");
        int b = theFile.find(" >" ,a+1);
        string z = theFile.substr(b+1,b+4);
       
    string c = z + " " + " \t " + " " + m;
	return c;
	}
	}
}

string extractResult(string line)
{
   
    if(line.find("Saw Shadow"))
    {
        string y = "Saw Shadow";
        return y;
    }
    else
    {
        if (line.find("No Shadow"))
        {
            string y = "No Shadow";
            return y;
        }
        else
        {
            string y = "Result Unknown";
            return y;
        }
    }
}
bool containsUsefulinfo(string entry)
{
	if((entry.find("1") || entry.find("2")) && (isdigit(entry[1])) && (isdigit(entry[2])) && (isdigit(entry[3])))
	{
        return true;
	}
    else
	{
        return false;
	}	
}




Report
Re: A problem with some pass by reference i think Posted by stober on 2 Jun 2006 at 4:48 PM
for one thing, the loop in getfile() is not like the one I posted. you are passing the wrong string to getline() so the result theFile string will be an empty string.
Report
Re: A problem with some pass by reference i think Posted by stober on 2 Jun 2006 at 4:58 PM
This message was edited by stober at 2006-6-2 16:59:6

you need to correct all those compile errors before you can even think about testing the program. Here is a version that compiles without errors.

For testing, you need to learn how to use your compiler's debugger.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;

void getfile(istream & in , string & theFile);

string getTable_entry(const string& theFile, int& pos);

string extractResult(string line);

bool containsUsefulinfo(string entry);




int main()
{
    string input;
    string filestring;
    int pos;
    cout << "Give me your input!";
    cin >> input;

	ifstream fin(input.c_str());
   
	getfile(fin,input);

    getTable_entry(input,pos = 0);
	

	ofstream fout("out.txt");
	
	return 0;
}
void getfile(istream & in , string & theFile)
{
                  
    string filestring;

    while(getline(in, theFile)) 
    {
        theFile+=filestring + "\n";
		cout << theFile;
		
	}
	
	
}
string getTable_entry(const string & theFile, int & pos)
{
	string c;
	string::size_type x = theFile.find("</td ",pos);
	if( x > 0)
	{
		string::size_type y = theFile.find(">",(x+1));
		string z = theFile.substr(x,y);
		cout << z;

		if (containsUsefulinfo(z))
		{
			string m = extractResult(z);
			string::size_type a = theFile.find("DATE");
			string::size_type b = theFile.find(" >" ,a+1);
			string z = theFile.substr(b+1,b+4);
       
			c = z + " " + " \t " + " " + m;
		}
	}
	return c;
}

string extractResult(string line)
{
   
    if(line.find("Saw Shadow"))
    {
        string y = "Saw Shadow";
        return y;
    }
    else
    {
        if (line.find("No Shadow"))
        {
            string y = "No Shadow";
            return y;
        }
        else
        {
            string y = "Result Unknown";
            return y;
        }
    }
}
bool containsUsefulinfo(string entry)
{
	if((entry.find("1") || entry.find("2")) && (isdigit(entry[1])) && (isdigit(entry[2])) && (isdigit(entry[3])))
	{
        return true;
	}
    else
	{
        return false;
	}	
}








 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.