Octavian Niculescu
Octavian Niculescu

Reputation: 83

How to add a field from a struct to an array?

In my homework, I have to build a program using a struct with some fields. I have to output a list with the name of the "societate" field in an ascending order and the number of these fields.

So I tried to add all these fields into a new array while checking if the field is not already inside that array.

Here's how I tried to do it:

#include "stdafx.h"
#include <iostream>
#include <string.h>

using namespace std;

struct sponsorizari {
    char societate[30], localitate[30];
    int cod_sponsorizare, data, valoare;
};

int main()
{
    int n, k = 0;
    char a[100];
    sponsorizari x[100];
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin.get();
        cin.get(x[i].societate, 30);
        cin.get();
        cin.get(x[i].localitate, 30);
        cin.get();
        cin >> x[i].cod_sponsorizare >> x[i].data >> x[i].valoare;
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < k; j++)
        {
            if (!strcmp(a[j], x[i].societate))
            {
                strcpy(a[k], x[i].societate);
                k++;
            }
        }
    }
}

It doesn't work - it gives me some errors. How can I make it work?

Edit: compiler errors

Upvotes: 0

Views: 112

Answers (2)

Helping Bean
Helping Bean

Reputation: 147

Working solution for the problem illustrated by you:

#include <iostream>
#include <string.h>
#include <unordered_map>

using namespace std;

struct sponsorizari {
    string societate, localitate;
    int cod_sponsorizare, data, valoare;
};

int main()
{
    int n, count = 0;
    unordered_map<string,int> stringMap;
    cout<<"Enter Value of n"<<endl;
    cin >> n;
    sponsorizari x[n];
    for (int i = 0; i < n; i++)
    {
        cout<<"Enter societate "<<i<<endl;
        cin>>x[i].societate;
        cout<<"Enter localitate "<<i<<endl;
        cin>>x[i].localitate;
        cout<<"Enter cod_sponsorizare "<<i<<endl;
        cin>>x[i].cod_sponsorizare;
        cout<<"Enter data "<<i<<endl;
        cin>>x[i].data;
        cout<<"Enter valoare "<<i<<endl;
        cin>>x[i].valoare;
        stringMap[(x[i].societate)]++;
    }
    cout<<"Among the n input societate's, the unique societate's are"<<endl;
    for (auto x : stringMap) 
    {
        if(x.second==1)
        {
            cout<<x.first<<endl;
        }
    }
}

Upvotes: 0

Ryan Haining
Ryan Haining

Reputation: 36792

wandbox link for the curious

The problem comes down to the uses of a in these two lines

if (!strcmp(a[j], x[i].societate)) {
    strcpy(a[k], x[i].societate);
    //...

a is a char[], when you do a[j] that is giving you a single char back. You can't copy a char* (string) to a char. I suspect what you want is to simply use a here

if (!strcmp(a, x[i].societate)) {
    strcpy(a, x[i].societate);
    //...

Though I'm lost on most of this code. You are checking if they are the same, and then if they are the same copying from one to the other?

You never put anything in a to begin with.

You initialize k to 0 before starting your inner loop for (int j = 0; j < k; j++) - and since k is * here this will never run. I can't tell what the purpose of the inner loop is supposed to be but it looks like it doesn't belong

int count = 0;
for (int i = 0; i < n; i++) {
    if (!strcmp(a, x[i].societate)) {
        ++count;
    }
}

In response to your problem statement, here is an outline of what your approach should be

unique_strings = []  // initially empty

for (each sponsorizari sp in x) {
    unique = true; // assume s it's unique
    for (each str in unique_strings) {
        if (sp.societate == str)
            unique = false; // found a match, not unique
            break;
        }
    }
    if (unique) { // we got through all unique_strings without a match
       add sp.societate to unique_strings
    }
}

If this is for a class I seriously suggest you go to office hours because you are clearly very lost.

Upvotes: 1

Related Questions