Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

A problem with some pass by reference i think

shonencd86shonencd86 Posts: 3Member
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 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
#include
#include
#include
#include

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 + " " + " " + " " + 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;
}
}

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

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    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!!
    [code]:
    : void getfile(istream & in , string & theFile)
    : {
    : ifstream fin(theFile.c_str());
    : string filestring;
    : while(getline(fin, theFile))
    : {
    : filestring -+= filestring + theFile;
    [red]syntax is incorrect ^^^ in the above line. Here is the correction.[/red]
    [blue] filestring += theFile;[/blue]

    :
    : }
    :
    : }
    [/code]
    :

  • shonencd86shonencd86 Posts: 3Member
    : 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!!
    : [code]:
    : : void getfile(istream & in , string & theFile)
    : : {
    : : ifstream fin(theFile.c_str());
    : : string filestring;
    : : while(getline(fin, theFile))
    : : {
    : : filestring -+= filestring + theFile;
    : [red]syntax is incorrect ^^^ in the above line. Here is the correction.[/red]
    : [blue] filestring += theFile;[/blue]
    :
    : :
    : : }
    : :
    : : }
    : [/code]
    : :
    :
    :


    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/
  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2006-6-2 2:22:55[/red][/b][hr]
    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.

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



    [blue]Next comment[/blue]. 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.

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


  • shonencd86shonencd86 Posts: 3Member
    : [b][red]This message was edited by stober at 2006-6-2 2:22:55[/red][/b][hr]
    : 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.
    :
    : [code]:
    : void getfile(istream & in , string & theFile)
    : {
    : ifstream fin(theFile.c_str());
    : string filestring;
    : theFile = "";
    : while(getline(fin, filestring))
    : {
    : theFile += (filestring + "
    ");
    : }
    : }
    : [/code]
    :
    :
    :
    : [blue]Next comment[/blue]. 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.
    :
    : [code]
    : string getTable_entry(const string & theFile, int & pos)
    : {
    : string::size_type x = theFile.find("
    #include
    #include
    #include
    #include

    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 + "
    ";
    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 + " " + " " + " " + 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;
    }
    }

    [/code]


  • stoberstober Posts: 9,765Member ✭✭✭
    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.
  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2006-6-2 16:59:6[/red][/b][hr]
    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.

    [code]
    #include
    #include
    #include
    #include
    #include

    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 + "
    ";
    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 + " " + " " + " " + 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;
    }
    }

    [/code]



Sign In or Register to comment.