Reading Lines from Input String

Feb 17, 2020 at 3:30pm
I'm trying to figure out how can I read the lines from a string of a user input.

I have a function outside of int main(). The way I want to do this is that for the first option, the code will read a text file and do the rest from there. I already got that cover. Now, I want to be able to allow the user to choose user input instead of fstream.

Somehow, the code below doesn't do anything. Not sure why but nothing is working. The code below will take the input string and count how many lines and words are there.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void count(int& w, int& z, int& y, int& z, string& input) {
	w= 0, x= 0, y = 0, z = 0;
	string str;
	while (getline(cin, input)) {
		w++;
		stringstream ss(str);
		while (getline(ss, input, ' ')) {
			x++;
		}
		for (int i = 0; i < str.length(); i++) {
			if (isdigit(str[i])) {
				z++;
			}
			else {
				y++;
			}
		}
	}
}


1
2
3
4
5
6
7
8
9
10
11
12
Input String Example:
Hello World 
2020

Output Examle:
cout << lines = " << w << endl;
cout << "words = " << x << endl;
cout << "digits = " << z << endl;

lines = 2
words = 2
digits = 4 

Last edited on Feb 17, 2020 at 3:33pm
Feb 17, 2020 at 4:27pm
Line 6/10: str is always empty. Most likely you want to use input.
On line 7 (depending on what you want) it is probably better to use str.
Feb 17, 2020 at 8:52pm
I made the change, but nothing.

To make it more clear of what I'm doing. I have another exact piece of code as the one I provided.

Basically, I have two void functions that do the same. One is for ifstream and the other is for user input from the terminal. So, the problem is that the one for the user input is not working. I already managed to get the ifstream working.
Last edited on Feb 17, 2020 at 8:58pm
Feb 17, 2020 at 9:26pm
and what coder is trying to tell you is that you have scrambled your variables a little. you read into input on line 4, and then try to use str which is still empty. Check each line and each variable and it should clear up. Which all credit goes to him; I am just repeating it...
Last edited on Feb 17, 2020 at 9:27pm
Feb 17, 2020 at 9:58pm
As jonnin said, read again what coder777 told you:
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
void count(int& w, int& z, int& y, int& z, string& input) {
    w= 0, x= 0, y = 0, z = 0;

    string str;     // 'str' here is empty

    while (getline(cin, input)) {   // input goes into 'input'
        w++;

        // 'str' here is still empty:
        stringstream ss(str);

        // 'ss' here contains nothing:
        while (getline(ss, input, ' ')) {
            x++;
        }
        for (int i = 0; i < str.length(); i++) {
            if (isdigit(str[i])) {
                z++;
            }
            else {
                y++;
            }
        }
    }
}

Can you see the problem?

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
#include <cctype>
#include <iostream>
#include <sstream>
#include <string>


void count(int& lines, int& words, int& charas, int& digits)
{
    lines = words = charas = digits = 0;

    std::cout << "Please type something and press ENTER (Q to exit): ";
    for (std::string line; std::getline(std::cin, line) && line != "Q"; /**/) {
        ++lines;

        std::stringstream ss(line);
        // Count number of words:
        for (std::string tmp; std::getline(ss, tmp, ' '); /**/) {
            ++words;
        }

        // Count characters and digits:
        for (char c: line) {
            if ( std::isdigit(c) ) {
                ++digits;
            } else {
                ++charas;
            }
        }
        std::cout << "Please type something and press ENTER (Q to exit): ";
    }
}


int main()
{
    int lines {};
    int words {};
    int charas {};
    int digits {};
    count(lines, words, charas, digits);

    std::cout << "You've typed:\n- " << lines << " lines\n- "
              << words << " words\n- "
              << charas << " characters\n- "
              << digits << " digits\n";
}


Output:
Please type something and press ENTER (Q to exit): Hello word!
Please type something and press ENTER (Q to exit): I've nearly typed 2 rows...
Please type something and press ENTER (Q to exit): 1 last word
Please type something and press ENTER (Q to exit): goodby!
Please type something and press ENTER (Q to exit): Q
You've typed:
- 4 lines
- 11 words
- 54 characters
- 2 digits


Please, get into the habit of asking about compilable code.
For example, there must be a main() to compile it.
Feb 18, 2020 at 9:18am
I made the change, but nothing.
Please show your modified code and like already asked as compilable with main().

Basically, I have two void functions that do the same. One is for ifstream and the other is for user input from the terminal. So, the problem is that the one for the user input is not working. I already managed to get the ifstream working.
I don't see how that is possible.

We can only see what happening when you provide the code. Hopefully compilable.
Topic archived. No new replies allowed.