Char comparison in C ++

This is my text file, which I take from

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

I have to find a product with the same name and add all their quantities, so the final result should be tree = 19; gold = 21; silver = 15. This is what I have done 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;
}

The problem so far is that it name[i]==name[d]doesn’t even respond, for example, name[i]="wood"andname[d]="wood"

+6
source share
4 answers

In C ++, we usually use std::stringthrough char[]. In the first case, the equality operator is overloaded, so your code will work. With the latter you need strcmp()to achieve your goal.

( std::vector, , ):

#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;
}

, std::pair, , .


, main() return 0;, , 1.

PS: :

#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;
}

:

Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall -std=c++0x main.cpp 
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out 
RESULTS:
wood 19
gold 21
silver 15
+6

, gcc , , ++, :

char name[10][n]; // 10 symbols are given for items name

.

++ , , std::vector.

, raw char, char == ( ), name[i]==name[d] , . , if (&name[i][0] == &name[d][0) .

strcmp char ( C) std::string, ==.

+1

char [] ==, , , . .. .

As a side note, char name [10] [n]; is invalid; since this n must be a compile-time constant. I would suggest std :: vector as a replacement.

0
source

If you just want to add a number, you can use unordered_map . It looks like a hash table in java.

0
source

All Articles