0

I've included my MCV below.

Brief Overview of Program: I have an array of structures that is my inventory. The program has 4 functions: 1) initializes 2 instances of the array, 2) a menu of options, 3) a fxn that prints the items in the array, and 4) the option to add to the array.

Problem Encountered: When I add a new item to my array inside my "add item function", it does not seem to be initializing the string member variable, while the number member variable does. I know this because when I choose the "print items" function again, the item just added does not show up. To test if this was a scope issue, I even had it print the items inside of the same function that it adds them. Same issue. I am sure it is some issue with getline and .ignore, but I can't see what.

What I've tried: I have googled and read questions here and on other forums. The problem there is that the questions are either way too advanced for what I'm trying to do, or the answers talk about using getline and .ignore (which I am already using in the way suggested). I have also consulted my intro programming book (input/output chapter, strings chapter). I have also made multiple drivers to test my ideas about what could be causing the issue. None seem to address this, or I am not understanding what I'm reading.

EDIT: Moving cin.ignore(1000, '\n') ABOVE getline instead of below fixed the issue.

#include <iostream> 
#include <cstdlib> 
#include <cstring> 
using namespace std;

//struct definition
struct item
{
    int number;
    string name;
};

//fxn prototypes
void fillarray(item inventory[], int size);
int menu_selection(void);
void print_list(item inventory[], int size);
int add2list(item inventory[], int size);

/**********************************************************************/

int main()
{
int menu = 1, size = 2, selection; //menu controls while loop to keep menu showing. selection determines which if else branch the program chooses
item inventory[100]; //array of structs

while (menu == 1)
{
    fillarray(inventory, size);
    selection = menu_selection(); 
    if (selection == 1)
    {
        print_list(inventory, size); 
    }
    else if (selection == 2) 
    {
        size = add2list(inventory, size); 
        menu = 1;
    }
    else if (selection == 3)
    {
        exit(0);
    }   
};
return 0;
}

/**********************************************************************/

void fillarray(item inventory[], int size)
{
//initialize name member variable of struct 
inventory[0].name = "hammer";
inventory[1].name = "drill press";

//initialize the number in the array (up to what has been initialized for name)
int i = 0;
for (i = 0; i < size; i++)
    {
        inventory[i].number = i+1;
    };
}

int menu_selection(void)
{
int option;
    cout << "INVENTORY OPTIONS" << endl
        << "1. Print items" << endl
        << "2. Enter a new item." << endl
        << "3. exit." << endl
        << "Type the number for the chosen option: ";
    cin >> option; 
    cout << endl << endl;
    return option;      
}

void print_list(item inventory[], int size) //print array
{
    cout << "Size is: " << size << endl;

    int i = 0;
    for (i = 0; i < size; i++) 
    {
        cout << inventory[i].number << "   " << inventory[i].name << endl;
    };
    cout << endl;
}

int add2list(item inventory[], int size) //enter new inventory item
{
    //add
    cout << "Enter item: ";
    getline(cin, (inventory[size].name));
    cin.ignore(1000,'\n'); 

    inventory[size].number = size + 1;

    cout << endl;

    //*below it prints the array inside the same function to see what make is different from printing in print_list fxn
    int i = 0;
    for (i = 0; i < size; i++) 
    {
        cout << inventory[i].number << "   " << inventory[i].name << endl;
    };
    cout << endl;

    size++;
    cout << "size is: " << size << endl;
    return size;
}

Ok, based on feedback, I made some code without the menus and the string member variable initializes just fine. But I have no idea why it works that way and not the first way.

#include <iostream> 
#include <cstdlib> 
#include <cstring> 
using namespace std;

//struct definition
struct item
{
    int number;
    string name;
};

int main()
{

int size = 2; 
item inventory[100]; //array of structs


//INITIALIZES ARRAY
inventory[0].name = "hammer";
inventory[1].name = "drill press";

//NUMBERS ARRAY
int i = 0;
for (i = 0; i < size; i++)
    {
        inventory[i].number = i+1;
    };


//PRINTS ARRAY
for (i = 0; i < size; i++) 
    {
        cout << inventory[i].number << "   " << inventory[i].name << endl;
    };
        cout << endl;

//ADD TO ARRAY

    cout << "Enter item: ";
    getline(cin, inventory[size].name); //whether or not i include delimiter, doesn't let me input--just skips to the for loop below

    inventory[size].number = size + 1;
    size++;

    cout << endl;

    //PRINT AGAIN to see if changes updated
    for (i = 0; i < size; i++) 
    {
        cout << inventory[i].number << "   " << inventory[i].name << endl;
    };
    cout << endl;
        exit(0);
return 0;
}
Lee Lemur
  • 31
  • 1
  • 6
  • Why do you use a global variable size, that you also pass as parameter by value? Whenever you change size it's the local copy! Use std::vector and push_back instead of an array. –  Sep 09 '17 at 21:54
  • _Is there why my issue lies?_ I don't know, test it. Use a debugger to find errors like that. –  Sep 09 '17 at 22:00
  • Manni, I went ahead and changed my size variable so that the function returned it to main. However, it doesn't address the real issue here, which is that when I try to initialize a string member variable with getline, it doesn't seem to do so. – Lee Lemur Sep 09 '17 at 22:37
  • Not a badly laid out question. One note: An MCVE the demonstrate a problem with the add routine would not have the menu routine or any user input. It would just add an item and display to show the failure. – user4581301 Sep 09 '17 at 23:00
  • 1
    The `cin >> option;` in `menu_selection` interacts with the `getline` in `add2list`. You probably just have the `ignore` in the wrong position. – Bo Persson Sep 10 '17 at 00:32
  • so i just went and made an MCVE without the menu, and the problem went away! I have no idea why. I will edit my post to include that code. – Lee Lemur Sep 10 '17 at 00:42

0 Answers0