Input Output with dictionary

How do you convert the string to a char???
I need this program to delete unwanted lines. But I have to convert string to char...

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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
using namespace std;

int main()
{
    char line[80];
	string bybys;
	ifstream in("en-GB.dic");  
	
	if( !in.is_open())
		{
		cout << "Input file failed to open" << endl;
		return 1;
		}

	ofstream out("en-GB2.dic");
	while (!in.eof())
		{        
		getline (in, bybys);
		line = bybys;
		int length = strlen(line);
		}
	while( getline(in, bybys) )
		{
		if(length < 8 && length > 2)
		out << line << endl;
		}
in.close();
out.close();

remove("en-GB.dic");
rename("en-GB2.dic","en-GB.dic");
return 0;
}
Last edited on
Why don you call in.getline(line,80) ?
Or use only the string bybys?
Line 20: Don't loop on eof(). Loop on good() instead. You can combine lines 20 and 22 to do that:
while (getline (in, ybbys))

Line 26: This loop will never execute, since you have already read to EOF using the loop starting at line 20.

Line 23: You cannot assign to an array like that. You would have to use strcpy(). That said, why are you using a char array anyway? You don't need it. The string class will tell you the length of your string.

What exactly are you trying to do? Only keep lines with (2 < length < 8)? Reconsider how your loops work.

I think that if you were to delete lines 25, 26, and 27 you might be much closer to what you want.

Also, be careful to use indentation consistently:
1
2
3
4
5
6
string line;
while (getline (in, line))
        {
        if ((line.length() < 8) && (line.length() > 2))
                out << line << endl;
        }

Hope this helps.
When I use this one the output is just squares and two letters...
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
using namespace std;

int main()
{
    
	string line;
	ifstream in("en-GB.dic");  

	if( !in.is_open())
		{
		cout << "Input file failed to open" << endl;
		return 1;
		}

	ofstream out("en-GB2.dic");
	while (!in.eof())
		{        		
		getline (in, line);		
			while (getline (in, line))
        {
        if ((line.length() < 8) && (line.length() > 2))
                out << line << endl;
        }

		}

in.close();
out.close();

remove("en-GB.dic");
rename("en-GB2.dic","en-GB.dic");
return 0;
}


And if I use this one the output everything gets deleted...
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
using namespace std;

int main()
{
    
	string line;
	ifstream in("en-GB.dic");  

	if( !in.is_open())
		{
		cout << "Input file failed to open" << endl;
		return 1;
		}

	ofstream out("en-GB2.dic");
	while (!in.eof())
		{        		
		getline (in, line);		
	

		}
			while (getline (in, line))
        {
        if ((line.length() < 8) && (line.length() > 2))
                out << line << endl;
        }

in.close();
out.close();

remove("en-GB.dic");
rename("en-GB2.dic","en-GB.dic");
return 0;
}


What should I do? i want to keep the letters with 3, 4, 5, 6 and 7 letters
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
using namespace std;

int main()
{
	string line;
	ifstream in("data.txt");

	if( !in.is_open() ) {
		cout << "Input file failed to open" << endl;
		return 1;
    }
	ofstream out("data.new");

	while( !in.eof() ) {
		getline(in, line);
		if(line.length() < 8 && line.length() > 2)
            out << line << endl;
    }
    in.close();
    out.close();

    remove("data.txt");
    rename("data.new","data.txt");
    return 0;
}


this code uses string class instead of traditional c-string char*.
and make your code easier to read. improve your indentions.

-hope this helps
I also have another source and there is a char variable and i need to compare it to the string value...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main() {
    //initialize variables
    char cString[80];
    string strObj;

    //get input
    cin.getline(cString, 80, '\n');
    getline(cin, strObj);

    //convert char array to string
    string newStr(cString);

    //compare the strings
    if( strObj.compare(newStr)==0 )
        cout << "both strings are equal\n";
    else
        cout << "the strings are not equal\n";

    return 0;
}


dont know what you're trying to do but here take a look.
Topic archived. No new replies allowed.