String and a loop

Hello! I want to get all the names that do not end with 's' and print them out AFTER I print their count. With the test that I am trying this program with out, for some reason, it only prints only 2 names although it should be 3. I think it might be the problem with how I handle the loops.
I am given:
int n (the overall count of names);
string s(getting a new name with every loop).
Everything is fine with the program, I should just fix the loops. I am putting each char of of each string into a mutual char array, which I later print out.
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
# include <iostream>
# include <fstream>
using namespace std;

int main()
{
    ifstream fd ("given.txt");
    ofstream fr ("results.txt");
    int n, k = 0, sl, b = 0, a = 0, f = 0;
    string s;
    char m[100];
    fd >> n;  //getting the count of names given
    for(int i = 0; i < n; i++){
           getline(fd, s); //getting every individual string
           sl = s.length();
           if(s[sl-1] != 's'){ //checking, if it does not end with 's'
               k++;  //the count of names that do not end with 's'
               a += sl;
               for(int j = b; j < a; j++){ //putting the string into the arr.
                    m[j] = s[f];
                    f++;
               }

               f = 0;
               b = sl;  //changing the value of the start of the next loop to continue writing into the same loop.

            }
    }

    fr << k << endl;      //printing everything out.
    for(int i = 0; i < a; i++){
        fr << m[i];
    }

return 0;
}



So, I am meant to use this rather long looking technique of putting each char of each string into the mutual array. The problem is the loops :/
Last edited on
Change:
fd >> n; //getting the count of names given
to:
1
2
    fd >> n;  //getting the count of names given
    getline(fd, s);    // get the rest of the line up to the linefeed (and dump it) 



Also change
b = sl;
to
b = a;


This is a truly bizarre way of doing it. Why not just append to a master string (inserting a '\n' newline character before each new one)?

Also, you are allowed to have meaningful variable names longer than one or two characters. It makes code easier to decipher.
Last edited on
It does not work if I take the getline out of the loop. I need to check each string and print out only the selected ones. Otherwise it just reads the first line. The names that I am given are each written in a new line.
E.G.

7
banana
watermelon
christmas
lemons
citrus
dog
cat


I can't really use techniques which we were not taught about at school so
Last edited on
I didn't say take the getline out of the loop.

I said add ANOTHER getline straight after fd >> n and before the for loop starts; you won't do anything with this, it will probably be an empty string. However, most crucially, it will get beyond the linefeed before you start reading names proper. The alternatives are some fiddly use of fd.ignore(), or using getline to read the line containing n into a string and then stringstreaming it into n.

Your problem is the classic one of mixing use of the extraction operator >> (which doesn't absorb the linefeed) with getline (which does).

If all the data are going to be single words, then you could actually use the extraction operator >> for all of them, rather than using getline at all. However, that depends on the problem specification.

Make sure you also modify that b= statement.
Last edited on
Thank You, I have fixed it. Is there any chance of printing the names in an alphabetical order?
Only if you read them into an array of strings and sort them.
Last edited on
And is there any chance of printing each name in a newline when they are all put into an array with no gaps?
If you put them in an array (or vector) of strings then just output them one per line with either endl or '\n' between each output.

If you absolutely have to have one big char array then put '\n' between entries (and, ideally, null terminate it with '\0' at the end.)

Or you could write them one by one to file as you find them (though if you want a number first then you would have to process the original file twice).

Since your are using strings (and not just c-strings or char arrays) anyway, you would be better just using strings throughout.
Topic archived. No new replies allowed.