Program crashes towards the end

Hello, I can't seem to find why my program towards the end. It seems to be working correctly, but seconds after the program is supposed to be ending it stops working.

Here is the code ->
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;

//Function Prototypes//
string charString(string*, string*, string*, int, int, int);
string reverseString(string, string, string);
//------------------//

int main()
{	
	string linePtr1, linePtr2, linePtr3, line01, line02, line03;

	// Holds the strings for each line.
	string *line1 = &linePtr1;
	string *line2 = &linePtr2; 
	string *line3 = &linePtr3; 
	 // Holds the lenth of each of the lines.
	int length1 = 0; 
	int length2 = 0; 
	int length3 = 0;

	charString(line1, line2, line3, length1, length2, length3);

}

string charString(string *line1, string *line2, string *line3, int length1, int length2, int length3)
{
	ifstream inFile;
	
	inFile.open("testdata.txt"); //Opens test .txt file
	//Gets the full string line.
	getline(inFile, *line1);
	getline(inFile, *line2);	
	getline(inFile, *line3);

	string line01 = *line1;
	string line02 = *line2; 
	string line03 = *line3;
	//Gets the strings length.
	
    for(int i = 0; i < line01.length(); i++) //Determines isalpha string length for line 1
    {
    	if(isalpha(line01[i]))
    	{
    		length1++;
		}
	}
	 for(int i = 0; i < line02.length(); i++) //Determines isalpha string length for line 2
    {
    	if(isalpha(line02[i]))
    	{
    		length2++;
		}
	}
	 for(int i = 0; i < line03.length(); i++) //Determines isalpha string length for line 3
    {
    	if(isalpha(line03[i]))
    	{
    		length3++;
		}
	}
	cout << length1 << endl;
	cout << length2 << endl;
	cout << length3 << endl;
	cout << line01 << endl;
	cout << line02 << endl;
	cout << line03 << endl;

}

/*string reverseString() 
{
	string line1, line2, line3;
	
	cout << "Here is the String the correct way." << endl;
	cout << line1 << endl << line2 << endl << line3 << endl;
	cout << "Here is the String backwards." << endl;
	cout << "Nothing here yet :/" << endl;
	
} */


Thank you for any help or recommendations you can give!
@Torm04

I changed this line, (and the other two similar ones)
from for(int i = 0; i < line01.length(); i++)

to for(int i = 0; i != line01.length(); i++)

and the program worked fine.
I think the real problem here is that you're expected to return a string from charString() and you don't.

I suggest you either return a string as promised, or change charString to a void return type, or you will still have problems.


1
2
3
4
5
6
void charString(string*, string*, string*, int, int, int);
...
void charString(string *line1, string *line2, string *line3, int length1, int length2, int length3)
{
...
}
closed account (SECMoG1T)
Yeah i agree with @tipaye , it's a problem with some standard containers where if you promise to return a container object from a function but you fail to fulfill the promise your program will likely end up in such a mess, here are some of those containers {std::list,std::string,std::vector}whereas deques,forward_list,queue wont show such a problem, i really have never gotten an idea why till today.
Last edited on
Your indentation of lines 44-64 is really bad. The code below is the same but the statement blocks are a lot easier to decipher.
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    for(int i = 0; i < line01.length(); i++) //Determines isalpha string length for line 1
    {
        if(isalpha(line01[i]))
        {
            length1++;
        }
    }
    for(int i = 0; i < line02.length(); i++) //Determines isalpha string length for line 2
    {
        if(isalpha(line02[i]))
        {
            length2++;
        }
    }
    for(int i = 0; i < line03.length(); i++) //Determines isalpha string length for line 3
    {
        if(isalpha(line03[i]))
        {
            length3++;
        }
    }
Sorry about that, thank you all for your suggestions!
I was bored.

None of this is related to your problem, but here's some other [hopefully] helpful tidbits:


- Don't use pointers if you don't need them.

- Functions should generally do one thing -- and that thing should be obvious from the name. Example... isalpha does a very obvious simple thing, it tells you whether or not the given character is alphabetical. Your 'charString' function does not have a clear name, nor does it have a clear job. You basically just put your entire program inside that function. It's doing too much.

- Don't copy/paste code. If you find yourself naming things like line1, line2, etc... then you're probably doing something wrong. Consider using arrays /vectors and/or breaking up a job into smaller parts.

- If a parameter is not needed for input, don't have it as a parameter. IE, your 'length1', 'length2', etc parameters. There is no reason for those to exist in main. No reason for them to be passed as parameters to charString.


Example!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

// A simple function that does a singular job:  counts the number of alphabetical characters in a string
//   (notice how the name matches the job)
int countAlphaChars( const std::string& str )
{
    int count = 0;
    for(int i = 0; i < str.length(); ++i)
    {
        if( std::isalpha(str[i]) )
            ++count;
    }
    
    return count;
}
// Now that we have that function, we can reuse it for each line.  We don't have to copy/paste
//   that code 3 times.


// Another simple function which reads the given number of lines from a given file name
void readLinesFromFile( const char* filename, std::string lines[], int count )
{
    std::ifstream inFile;
    inFile.open( filename );
    
    // we're using an array, so no need to copy/paste getline 3 times
    for(int i = 0; i < count; ++i)
        getline(inFile, lines[i]);
}


// Now that we have those functions written, we can use them to make the main logic simple:
//   (note this probably could be broken down more, but whatever)
int main()
{
    static const int lineCount = 3;     // how many lines we want to process
    
    std::string lines[lineCount];
    int alphaCounts[lineCount];
    
    // read all the lines from the file
    readLinesFromFile( "testdata.txt", lines, lineCount );
    
    // count the alphabetic characters in each line
    for(int i = 0; i < lineCount; ++i)
    {
        alphaCounts[i] = countAlphaChars( lines[i] );
    }
    
    // Print our results
    for(int i = 0; i < lineCount; ++i)
    {
        std::cout << alphaCounts[i] << " - " << lines[i] << '\n';
    }
}
Topic archived. No new replies allowed.