Mahig Yahok Mahig Yahok -4 years ago 92
C++ Question

Comparing char in c++

This is my text file that I'm taking data from

10
wood 8
gold 7
silver 5
gold 9
wood 1
silver 1
silver 9
wood 3
gold 5
wood 7


I'm supposed to find goods with the same name and add all of their amounts, so final result should be wood=19; gold=21; silver=15. This is what I did so far

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream read("data.txt");
int n;
read >> n;
char name[10][n]; // 10 symbols are given for items name
int amount[n];
for(int i=0; i<n; i++)
{
read.ignore(80, '\n');
read.get(name[i], 10);
read >> amount[i];
}

for(int i=0; i<n; i++)
{
for(int d=1; d<n; d++)
{
if(name[i]==name[d] && i!=d)
{

}
}
}
return 1;
}


Problem so far is that
name[i]==name[d]
doesn't react even is for example
name[i]="wood"
and
name[d]="wood"

Answer Source

In C++, we tend to use std::string over char[]. The first has the equality operator overloaded, thus your code shall work. With the latter, you need strcmp() to achieve your goal.

Now your code could like this (I used std::vector, but you can use an array of string, but I do not recommend it):

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    ifstream infile("data.txt");
    int n;
    infile >> n;
    vector<string> name(n);
    int amount[n], i = 0;
    while (infile >> name[i] >> amount[i])
    {
        cout << name[i] << " " << amount[i] << endl;
        i++;
    }
    // do your logic
    return 0;
}

By the way, you could use std::pair, to make your code more readable, where the first member would be the name and the second the amount.


Unrelated to your problem, main() tends to return 0; when everything is fine, whereas you return 1.

PS: Here is a working example:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <utility>

using namespace std;

int main()
{
    ifstream infile("data.txt");
    int n;
    infile >> n;
    vector<string> name(n);
    int amount[n], i = 0;
    while (infile >> name[i] >> amount[i])
    {
//        cout << name[i] << " " << amount[i] << endl;
        i++;
    }


    vector< pair<string, int> > result;
    bool found;
    for(int i = 0; i < name.size(); ++i)
    {
        found = false;
        for(int j = 0; j < result.size(); ++j)
        {
            if(name[i] == result[j].first)
            {
                result[j].second += amount[i];
                found = true;
            }
        }
        if(!found)
        {
            result.push_back({name[i], amount[i]});
        }
    }

    cout << "RESULTS:\n";
    for(int i = 0; i < result.size(); ++i)
        cout << result[i].first << " " << result[i].second << endl;
    return 0;
}

Output:

Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall -std=c++0x main.cpp 
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out 
RESULTS:
wood 19
gold 21
silver 15
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download