File I/O not recognizing contents correctly

Hello, so I have a program that's not doing what I want it to do. This is the assignment:

The nth term of the sequence of triangle numbers is given by, tn = 1/2 n (n+1); so the first ten triangle numbers are:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55,...
By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we shall call the word a triangle word.

the 'words.txt' has a bunch of words in this format:
"ABSTRACT"
"YOUTH"
there's about 1000 words in that format.
And for any triangle words I find, I put it into another text file which I called triangle.txt.
Now for my program, it pulls the 'words.txt' file just fine but it doesn't recognize any of the words in the file as triangle words.
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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cmath>
#include <cstdlib>

int getCharValue(char a);

double sum1(std::string name);

int main() {

	int a[500];
	int n;
	int t = 0;
	std::string value[500];
	std::string words;
	std::string word[500];
	int count = 0;
	std::ifstream fread;
	std::ofstream fwrite;
	std::cout << std::endl;
	int i = 0;
	char c[50];
	fread.open("words.txt");
	fwrite.open("triangle.txt");

	if (!fread.is_open())

		std::cout << "Unable to open a file." << std::endl;

	if (!fwrite.is_open())

		std::cout << "Unable to open a file." << std::endl;

	while (fread.good())

	{

		getline(fread, words, ' ');

		if (words != "")
		{
			word[i] = words;
			std::cout << words << std::endl;
			double wordsum = (sqrt(1 + 8 * sum1(word[i])) - 1.0) / 2.0;
			if (wordsum == (int) wordsum)
			{
				fwrite << words;
				fwrite << "\n";
				count++;
			}
			i++;
		}
	}
	std::cout << "Total triangle words are: " << count++ << std::endl;
	fread.close();
	fwrite.close();
	system("pause");
	return 0;
}

double sum1(std::string name){
	int result = 0;
	for (int i = 0; i < name.length(); i++)
	{
		result += (int) name[i] - 64;
	}
	return result;
}
Write small functions; each of it doing one small thing, and doing it well.

Test each function before moving on to the next one.

Avoid Americanisms like (int) name[i] - 64 (which isn't always right even in America).

Try putting something like this (caveat: untested) into your own 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
#include <iostream>
#include <string>
#include <cctype>
#include <cmath>
#include <sstream>

int letter_value( char letter ) 
{
    static const std::string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
    const auto pos = alphabet.find( std::toupper(letter) ) ; // convert lower case to upper case
    return pos == std::string::npos ? 0 : pos+1 ;
}

int letter_sum( std::string word )
{
    int sum = 0 ;
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( char letter : word ) sum += letter_value(letter) ;
    return sum ;
}

// a number n is triangular if and only if 8n+1 is an odd perfect square.
bool is_triangular_number( int n )
{
    if( n < 1 ) return false ;

    int square = 8*n + 1 ; // 8n+1
    int root = std::sqrt(square) ; // sqrt(8n+1) >= root
    if( root%2 == 0 ) ++root ; // if even, add one to make odd

    return root*root == square ; // odd perfect square?
}

bool is_triangular( std::string word )
{
    return is_triangular_number( letter_sum(word) ) ;
}

int main()
{
    std::istringstream file( "ABSTRACT \n YOUTH \n SKY \n a \n bunch \n of \n words \n in \n this \n format \n"
                             "By \n converting \n each \n letter \n in \n a \n word \n to \n a \n number \n"
                             "corresponding \n to \n its \n alphabetical \n position \n and \n adding \n these \n"
                             "values \n we \n form \n a \n word \n value. \n sky \n Sky \n sKy \n WE \n We \n" ) ;

    // std::string line ;
    // while( std::getline( file, line ) ) // canonical loop to read each line in the file till eof

    std::string word ;
    while( file >> word ) // canonical loop to read each 'word' in the file till eof
        if( is_triangular(word) ) std::cout << word << '\n' ;
}

http://coliru.stacked-crooked.com/a/60d4e554bed337e8
Yeah, making small functions help a lot. Thanks!
Topic archived. No new replies allowed.