Reputation: 87
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"
Upvotes: 6
Views: 20999
Reputation: 621
If you just wish to add the number then you can use an unordered_map. It is similar to the hash table in java.
Upvotes: 0
Reputation: 73366
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
Upvotes: 6
Reputation: 148910
Ok, gcc is know to accept it but Variable Length Arrays are not supported in C++, so this line:
char name[10][n]; // 10 symbols are given for items name
is not conformant and should have given at least a warning.
The C++ way to deal with array whose dimension is only known at run time is to use std::vector
.
But your real problem is that neither a raw char array nor a pointer to char have an overriden ==
operator (it is not possible for arrays or pointers), so in your name[i]==name[d]
you are actually comparing the addresses because the arrays decay to pointers to their first element when used in expressions. So your test is the same as if (&name[i][0] == &name[d][0)
and cannot give expected result.
You can use strcmp
to compare null terminated char arrays (also known as C strings) or better use std::string
which has an overidden ==
operator.
Upvotes: 1
Reputation: 6983
The char[] == operator you're using is comparing pointer values, rather than string comparison values. ie you're comparing the position of the first characters in memory.
As a side note, char name[10][n]; is invalid; as that n would have to be a compile time constant. I'd suggest std::vector as a replacement.
Upvotes: 0