DrOdie
DrOdie

Reputation: 33

Output issues when working with structs in C++

We just started working with structs in class and were given pre-existing code and told to take user input for the struct and create a function which prints the inventory. This is what I have done:

#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;

struct LineItem
{
    int quantity;
    string description;
    double price;
};

void print_inventory_report(LineItem[],const int);


int main()
{
  const int arraysize = 2;
  LineItem inventory[arraysize];

  for(int index = 0;index < arraysize;index++)
  {
      cout << "What part is this for (enter a one word description)? ";
      cin >> inventory[arraysize].description;

      cout << "Enter the quantity: ";
      cin >> inventory[arraysize].quantity;

      cout << "Enter the price: ";
      cin >> inventory[arraysize].price;

      cout << endl;
  }

  print_inventory_report(inventory,arraysize);

return 0;
}

void print_inventory_report(LineItem inv[], const int s)
{
    cout << "Totals: " << endl;;

    for(int i = 0; i < s;i++)
        cout << "  " << i+1 << ". " << inv[i].description << " $" << (inv[i].quantity * inv[i].price) <<endl;
        cout << endl;
}

Based on this, if the user entered the following:

What part is this for? (Enter a one word description) Struts
Enter the quantity: 2
Enter the price: 45.68

What part is this for? (Enter a one word description) Oil
Enter the quantity: 4
Enter the price: 5.74

I would expect the output to be:

Totals:
  1. Struts: $91.36
  2. Oil: $22.96

But instead I get this:

Totals:
  1.   $7.95081e+070
  2.   $7.95081e+070

I also get the following error box:

Error Message

Are the two problems I'm having connected to one another?

Upvotes: 2

Views: 53

Answers (1)

gsamaras
gsamaras

Reputation: 73366

Are the two problems I'm having connected to one another?

Yes, since you are actually accessing memory your program doesn't own! So, you output garbage values and you are getting a relevant error message. In Linux, I got:

gsamaras@gsamaras:~$ ./a.out 
What part is this for (enter a one word description)? Struts
Segmentation fault (core dumped)

You are doing:

const int arraysize = 2;
LineItem inventory[arraysize];
...
cin >> inventory[arraysize].description;

So now you trying to access an array out of bounds, since indexing starts from 0. A segmentation fault lurks.


You could use your counter and do:

cin >> inventory[index].description;

to get yourself ready for input. After correcting all the relevant errors in your code, you should be able to regenerate this behaviour:

gsamaras@gsamaras:~$ ./a.out 
What part is this for (enter a one word description)? Struts
Enter the quantity: 2
Enter the price: 3

What part is this for (enter a one word description)? Strats
Enter the quantity: 2
Enter the price: 3

Totals: 
  1. Struts $6
  2. Strats $6

You may want to have a look at my example mystruct.

Upvotes: 3

Related Questions