-1

I'm creating a program that involves a dictionary of terms and definitions. It requires me to create two string arrays of an inputted length. Then prompt the user to input the terms and definitions respectively. It has 2 functions, the main function (works as the input function), and the output function. I have included my code with this.

My issue is the output that I'm getting, it seems to not recognize index 0 for terms as anything. It skips it and put everything forward one index. I think it's something with my getline() function.

I've tried cin.ignore() and that only deletes the first character of each string.

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


//Main acts as the input function
int main() {


    //Declarations
    void output(string* terms, string* definitions, int numEnteries);
    string* terms;
    string* definitions;
    int numEnteries;

    //Input
    cout << "How many terms would you like in the dictionary?: " << endl;
    cin >> numEnteries;

    //Making the arrays the size the user wants
    terms = new string[numEnteries];
    definitions = new string[numEnteries];

    //Inputs for both terms and definitions
    //I think the issue may be in this loop, I may be wrong though
    for (int i = 0; i < numEnteries; i++){
        cout << "Enter term " << (i+1) << ":";
        getline(cin, terms[i]);
        cin >> ws;

        cout << "Enter the definition for '" << terms[i] << "': ";
        getline(cin, definitions[i]);
        cin >> ws;
    }

    //Calling the output function
    output(terms, definitions, numEnteries);

    return 0;
}

void output(string terms[], string definitions[], int numEnteries){

    cout << "You entered: " << endl;

    //Outputting the terms and definitions in order
    for (int i = 0; i < numEnteries; i++){
    cout << (i+1) << ". " << terms[i] << ": ";
    cout << definitions[i] << endl;
    }

    //Deleting the "new" arrays
    delete[] terms;
    delete[] definitions;

I expected the output to be a nice list like: 1. "Term 1: Def 1" 2. "Term 2: Def 2" ... etc

But instead I'm getting: 1. : term 1 2. def 1: term 2 3. def 2: term 3

1 Answers1

0

It looks like essentially that you forgot a

//Input
cout << "How many terms would you like in the dictionary?: " << endl; 
cin >> ws;

after entering the number. With that added everything seems to work as expected, see here.

Thomas
  • 4,639
  • 2
  • 13
  • 25
  • That seemed to help, but now I have a weird spacing issue. After I input the number of enteries, it prompts me to enter the first term but doesn't print the line "Enter term 1" Until after I type something else and hit enter. Then the following inputs are pushed back to the wrong print line. – Olivia Hall Jun 29 '19 at 18:32