c++ please help

This is the text file:
1
2
zero;two;five;seven;eight;four
three;seven;eight;nine;two


The output is:
0257847892

The output is supposed to be:
1
2
025784
37892



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

int main (){
	ifstream file ("input.txt"); 
	string line;
        while(getline(file, line, ';')){
		string x;
	        stringstream ss(line);
		ss>>x;

		if (x == "zero")
			cout <<"0";
		else if (x == "one")
			cout <<"1";
		else if (x == "two")
			cout <<"2";
		else if (x == "three")
			cout <<"3";	
		else if (x == "four")
			cout <<"4";
		else if (x == "five")
			cout <<"5";
		else if (x == "six")
			cout <<"6";	
		else if (x == "seven")
			cout <<"7";
		else if (x == "eight")
			cout <<"8";
		else if (x == "nine")
			cout <<"9";
	}
	return 0;
}
You would have exactly the same result if you got rid of lines 12 through 14 and changed all the references to x to refer to line in lines 16 through 34. Does that shed any light on what's happening?
Last edited on
If I do that I get:
025787892

without the 4 and 3, still confused.
In your outer loop, you are reading each line, delimited by semi-colons. This mean you only read one word at a time, and do not catch when the new line occurs. Read the lines in the outer loop, so you can put in the new-line in the output. The inner loop then reads with a delimiter of semi-colon to get each word.
Last edited on
The output is supposed to be:
1
2
025784
37892


Okay, so this outputs the second line "37892"

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

int main (){
	ifstream file ("input.txt"); 
	string line;
	while(getline(file, line)){
		while (getline(file, line, ';')){
			string x;
	                stringstream ss(line);
			ss>>x;

			if (x == "zero")
				cout <<"0";
			else if (x == "one")
				cout <<"1";
			else if (x == "two")
				cout <<"2";
			else if (x == "three")
				cout <<"3";	
			else if (x == "four")
				cout <<"4";
			else if (x == "five")
				cout <<"5";
			else if (x == "six")
				cout <<"6";	
			else if (x == "seven")
				cout <<"7";
			else if (x == "eight")
				cout <<"8";
			else if (x == "nine")
				cout <<"9";
	         }
        }
	return 0;
}
Last edited on
Anyone please help.
Since you are asking for nothing else, but already showed some code, here is a working answer. If you have questions about the code, feel free to ask.

By the way, this is for certain not the "best" way to do it, but it is rather simple (dem If-statements though...).
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
int main()
{
    std::ifstream file("input.txt");
    if (!file) {
        std::cout << "Could not open file 'input.txt'\n";
        return 1;
    }
    std::string line;
    while (std::getline(file, line)) {
        std::istringstream stream(line);
        while (std::getline(stream, line, ';')) {    // just 'recycle' line
            if (line == "zero") std::cout << 0;
            if (line == "one") std::cout << 1;
            if (line == "two") std::cout << 2;
            if (line == "three") std::cout << 3;
            if (line == "four") std::cout << 4;
            if (line == "five") std::cout << 5;
            if (line == "six") std::cout << 6;
            if (line == "seven") std::cout << 7;
            if (line == "eight") std::cout << 8;
            if (line == "nine") std::cout << 9;
        }
        std::cout << '\n';
    }
}
Last edited on
Thank you!
Topic archived. No new replies allowed.