Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

like image 977
Mahig Yahok Avatar asked Jun 14 '17 18:06

Mahig Yahok


People also ask

Can you compare chars in C?

We can compare the characters in C using 2 different ways: Comparison using ASCII values. Using the built-in function.

Can I use == to compare char?

Yes, char is just like any other primitive type, you can just compare them by == .

Can you compare char?

We can compare the char values numerically by using compare(char x, char y) method of Character class. Note: Comparing char primitive values using Character. compare(char x, char y) method returns an integer value.


1 Answers

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
like image 104
gsamaras Avatar answered Sep 30 '22 17:09

gsamaras