Removing an element from an array
I have .txt with this data :
5
Z 14
R 12
G 20
R 5
R 6
my goal is to add same letters into one total sum, while removing the used colors. So I should get :
3
Z 14
R 23
G 20
I know how to remove one element from an array but it for some reason only removes 1 R even tho I've looped the for loop.
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
|
void skaityti_Faila(int& n, veliavos V[])
{
ifstream in("duomenys.txt");
in >> n;
for(int i = 0; i < n; i++)
in >> V[i].spalva >> V[i].kiekis;
for(int i = 0; i < n - 1; i++)
for(int j = i + 1; j < n; j++)
if(V[i].spalva == V[j].spalva)
V[i].kiekis += V[j].kiekis;
for(int i = 0; i < n - 1; i++)
for(int j = i + 1; j < n; j++)
if(V[i].spalva == V[j].spalva)
{
n--;
for(int k = j; k < n; k++)
V[k].spalva = V[k + 1].spalva;
}
in.close();
}
| |
Edit : After watching tutorials, I found my mistake.
Last edited on
Use a std::map to accumulate similar values.
Topic archived. No new replies allowed.