0

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

4
  • 3
    "It doesn't work - it gives me some errors" This is very vague. Compiler errors? Runtime errors? Wrong output? Can you narrow down where the errors are coming from by removing irrelevant code? Commented Oct 25, 2018 at 18:26
  • why are you using char[] over std::string? Commented Oct 25, 2018 at 18:26
  • @RyanHaining I edited my original post with the errors. I'm using char because that's what I learnt in school for now. Commented Oct 25, 2018 at 18:28
  • Please show the code and/or state the errors. The text on the picture is too small for some people to read. In addition, the text on the image cannot be indexed by search engines for future visitors. Commented Oct 25, 2018 at 19:39

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

10 Comments

I'm checking if they are not the same, if they aren't it should copy the "societate" field into that array. At first it is empty, but then it should add them one by one. (a[k], k++ - that's why I put it there).
My mistake is that I didn't realise the for loop will never be executed because j will never be less than k. So I think in this case the second for loop should be replaced with a do - while. Am I right?
strcmp returns 0 if the two arguments are the same if (!strcmp(...)) is true only if they are equal.
I don't know what you mean "add them one by one" are you trying to accumulate a list of all the strings which don't match the one before them in the array?
if you want an array of 100 strings you'd want char a[100][30]
|
0

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

1 Comment

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.