string doesn't compare to an array

So i have a problem with my program, i enter the band's name and i want to count ow many times it was used in the given list (.txt file). I do it with getline and then compare it with an array. any help is appreciated

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

struct Daina{
    string gr;
    string pav;
    double m;
    double s;
};
 void skaitymas (Daina D[],int &n);
 void skaiciavimas (Daina D[],int n,string pavadinimas);
int main()
{
    int n;
    Daina D[100];
    string pavadinimas;
    cout << "Enter bands name: " << endl;
    getline(cin,pavadinimas); // maybe it reads with space or smth

    skaitymas(D,n);
    skaiciavimas(D,n,pavadinimas);

    return 0;
}
//-------
    void skaitymas (Daina D[],int &n)
    {
        ifstream fd ("list.txt");
        fd >> n;
        for (int i = 1;i<=n;i++)
        {
            getline(fd,D[i].gr,'-');//there is a file with band's name until '-' 
            getline(fd,D[i].pav,',');//then in the same line song until ',' sign
            fd >> D[i].m >> D[i].s;
        }
    }
    //-----
        void skaiciavimas (Daina D[],int n,string pavadinimas)
        {
           int k = 0;
           int trukme = 0;
           int sekundes = 0;

            for (int i =1;i<=n;i++)
            {

                if (pavadinimas == D[i].gr) // this part doen't work
                {
                    k++;
                    trukme = D[i].m + trukme;
                    sekundes = D[i].s + sekundes;
                    if (sekundes >= 60)
                    {
                        sekundes = sekundes - 60;
                        trukme = trukme + 1;
                    }
                }
            }
            cout << k; //this should count how many times it was used but it's always 0
        }
Last edited on
Time to start thinking about how to solve problems for yourself.

if (pavadinimas == D[i].gr) // this part doen't work

What is the value of pavadinimas at that point?
What is the value of D[i].gr at that point?
If you don't know, is there some way you can make the program tell you? Perhaps you could output those values on screen so you can inspect them
There a couple of different possibilities why your program is malfunctioning.

First you need to check to insure that your input file actually opened.

Second depending on the file format you may be having problems with dangling end of line characters left in the input stream. When you switch between the extraction operator>> and getline() you can have problems because of these dangling characters. If your file structure looks something like:

1
2
3
2
band name-song name, 10 10
band name-song name,20 20

You will have problems. Your first input would be retrieved, but it would leave the end of line character in the input buffer so the next input with getline would retrieve that character along with "band name", remember by default getline() doesn't always skip leading whitespace. So the resulting string would look like "\nband name", notice that the end of line character is at the beginning of the string so if you compare "band name" to "\nband name" the comparison would fail. To solve the problem you will need to extract and discard the leading whitespace characters. For this program you can extract the whitespace inside the getline() call getline(fd >> ws, D[i].gr,'-');

There are other issues with your code that you should also fix, even though they don't "break" this program the issues should still be addressed.

First arrays in C++ start at zero and stop at size - 1. Your arrays are starting at one.

Second you should validate the variable "n" to insure it is less than or equal to the size of the array.

Third you really should use meaningful variable names. In a program of this size using single letter and non-meaningful variable names is not a big issue but as your programs grow, and they tend to grow quickly, your current variable names will get very confusing.

Fourth your indentation could use some work, consistent indentation makes reading your program much easier.

Last edited on
thanks a lot man, the whitespace was causing the problem fd >> ws worked
Topic archived. No new replies allowed.