0

I'm trying to use a structure in my c++ project but I've run into a bit of a problem. In my project I create a create a dynamic array after asking the user how many people that have done sales and how many days they worked. After that the program goes through a for loop to in put the data. Here the user is asked to enter names of the people and it then asks the number of units pre day until the a second for loop reaches the max number of days that the user inputted therein lies my problem. I tried using a two dimensional dynamic array put the program kept crashing so I'm not sure what I'm doing wrong. Here is my code:

#include <iostream>
#include <string>
struct emp{
     string names;
     int *untsold;


}; 



int main(){
     int people;
     int days;
     emp *workers;
     cout<<"How many workers are we looking at?"<<endl;
     cin>>people;
     cout<<"How many days are we looking at?"<<endl;
     cin>>days
     workers = new emp[people,days];
     for(int index=0;index<people;index++){
          cin.ignore();
          cout<<"Enter the name of worker number "<<index+1<<"."<<endl;
          getline(cin,emp[index,0].names);
          for(int index2=0;index2<days;index++){
              cout<<"Now enter the number of units sold on day "<<index2+1<<endl;
              cin>>*workers[index,index2].untsold;
          }

     }



}

The rest of the program is just displaying the information in a table then delete the dynamic array. The program seems crash right after I enter in something for the *workers[index,index2].untsold variable. Is it possible to put an array of variables within a dynamic array or should I try something else. Please mind that my teacher wants me to use both structure variables and dynamic arrays also note that I'm a beginner.

8
  • Umm...this doesn't look like it'd even compile. workers[index,index2]? new emp[people,days]? This isn't C++. Commented Jan 13, 2014 at 4:23
  • workers = new emp[people,days]; I am not able to understand this.. Commented Jan 13, 2014 at 4:24
  • by "crashing", presumably you mean "I get compilation errors"? Commented Jan 13, 2014 at 4:25
  • There's some semicolons missing in there, too. This will get a lot of compilation errors. Commented Jan 13, 2014 at 4:26
  • Voting to close as unreproducible. This code won't even compile, let alone run, let alone exhibit the behavior mentioned. Commented Jan 13, 2014 at 4:27

2 Answers 2

2

Use std::vector for dynamic size arrays.

To implement a matrix, the simplest and most efficient is to use a single vector to hold all items, then compute index in the single vector from specified (x, y) location in matrix.

If you want operator notation for (x, y) indexing, use operator() (which can take any number of arguments), not operator[] (which can take only one argument). It's technically possible to use multiple applications of square brackets indexing, but it gets complicated and advanced fast, depending on proxy objects to represent partial indexing – so do use either a named operation, or operator().

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

Comments

1
new emp[people,days]

does not allocate a two dimensional array - the comma operator evalutes the left hand side, throws away the result, then returns the value of the right hand side.
The code is equivalent to

new emp[days]

And since you never allocate the untsold meber of any struct,

*workers[index,index2].untsold;

which is equivalent to

*workers[index2].untsold

dereferences an uninitialized pointer, which is undefined.

It is better to use std::vector, but if you're trying to learn about arrays, you should re-read that part of your fine C++ book.

Comments

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.