Question: I am encountering an uninitialized local variable in the main function. This error occurs on the line with the code getInput(word, def, n); under the
I am encountering an uninitialized local variable in the main function. This error occurs on the line with the code "getInput(word, def, n);" under the function "int main(). This error came after i fixed the "getInput" function. In that case, I was getting a redefinition of formal parameters for "word" and "def". In that function a had lines of code " string* word = new string[n];" and "string* def = new string[n]; ". I changed those to "word = new string[n];" and "def = new string[n];". This then caused my problem.
C++ Code:
#include
// Function declarations void getInput(string* &word, string* &def, int n); void output(string* &word, string* &def, int n);
int main() { int n; string* word; string* def; cin.ignore(); //ignore buffer getInput(word, def, n); output(word, def, n);
return 0; }
// Function definitions void getInput(string* &word, string* &def, int n) {
cout << "How many terms do you want the dictionary to hold? "; cin >> n; word = new string[n]; ; // array to store words def = new string[n]; // array to store definitions
for (int i = 0; i < n; i++) { cout << " Enter Term " << i + 1 << ": "; getline(cin, word[i]); cin.clear(); // clearing the buffer cout << "Enter the definition for \"" << word[i] << "\": "; getline(cin, def[i]); } }
void output(string* &word, string* &def, int n) { cout << " You entered:" << endl; for (int i = 0; i < n; i++) { cout << i + 1 << ". " << word[i] << ": " << def[i] << endl; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
