edelossantos
asked on
Pointers, Arrays for Grammar Logic
// This code runs but program misbehaves, please advise.
************************** *******Cod e********* ********** ********** ********** ********** ****
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
// screen handling routines
void clearScreen();
void hitEnter(); // press Enter to continue.
void drawMenu(); // screen display for menu
// generate sentence routines
void initArray(char *[], int); //initialize each word to NULL string
void addVerbs(char *[], int); // populate the verbs array with input from user
void addNouns(char *[], int); // populate the nouns array with input from user
void display(char *[], int); // display each string in the Noun or Verb array
void generateNV(char *[], char *[], int);
void generateNVN(char **, char **, int);
const int wordSize = 25; // size of character array
const int listSize = 10; // size of pointer array
int main (void) {
// declare 2 ptr arrays to hold a list of nouns and verbs
char *nouns[listSize];
char *verbs[listSize];
char *test[4] = {"one","two", "three", "four"};
// allocate memory for 10 character strings in each ptr array
for(int i=0; i < listSize; i++)
nouns[i] = new char[wordSize];
for (int i=0; i < listSize; i++)
verbs[i] = new char[wordSize];
char choice; // user menu choice
do {
do { // do some validation on user input here....
drawMenu();
cin.get(choice);
} while ((choice < 49 || choice > 53) && choice != 'q');
switch (choice)
{
int key;
case '1': initArray(nouns, listSize);
initArray(verbs, listSize);
hitEnter();
drawMenu();
break;
case '2': addNouns ( nouns, listSize);
addVerbs ( verbs, listSize);
hitEnter();
drawMenu();
break;
case '3': display(nouns, listSize);
display(verbs, listSize);
hitEnter();
drawMenu();
break;
case '4': generateNV(nouns, verbs, listSize);
hitEnter();
drawMenu();
break;
case '5': generateNVN(nouns, verbs, listSize);
hitEnter();
drawMenu();
break;
case 'q': cout << "\ngoodbye\n";
break;
default: break;
} // end switch
} while (choice != 'q');
return 0;
} // end main
// ARRAY ROUTINES
// initializes the array words to NULL strings
void initArray(char *ptr[], int size) {
for(int i = 0; i < size; i++) {
*ptr[i] = '\0';
cout << ptr[i];
}
}
// display the array on the screen
void display(char *ptr[], int size) {
for(int i = 0; i < size; i++)
{
if (ptr[i] != NULL)
cout << ptr[i] << endl;
}
cout << endl;
}
// populate the verbs array with 10 verbs input from user
void addVerbs(char *verbs[], int size) {
for(int i = 0; i < size; i++) {
clearScreen();
cout << " Enter " << size - i << " verbs:";
cin >> verbs[i];
}
}
// populate the verbs array with 10 verbs input from user
void addNouns(char *nouns[], int size) {
for(int i = 0; i < size; i++) {
clearScreen();
cout << "Enter " << size - i << " nouns:";
cin >> nouns[i];
}
}
void generateNV( char *nouns[], char *verbs[], int size) {
cout << nouns[rand() % size] << " " << verbs[rand() % size] << endl;
}
void generateNVN( char **nouns, char **verbs, int size )
// note the various ways to access the data in nouns and verbs arrays
{
cout << *(nouns + (rand() % size)) << " " << verbs[rand() % 9] << " "
<< nouns[rand() % size] << endl;
}
// SCREEN HANDLING ROUTINES
/* function hitEnter: programs pauses for user input --------- */
void hitEnter(void) {
char ch;
do // read blanks and trailing CR
cin.get(ch);
while ( ch == ' ');
cout << "\n\nHit Enter to continue....";
cin.get(ch);
}
void clearScreen(void) {
cout << "\033[2J"; // send vt100 ESC sequence to clear screen
return;
}
void drawMenu (void) {
static int count = 0;
cout << "\033[2J"; // send vt100 ESC sequence to clear screen
if(count == 0)
cout << "\t\tWelcome to a program to generate sentences";
else
cout << "\t\t\tHomework #8";
cout << "\n\n";
cout << "\n1] Initialize arrays";
cout << "\n2] Populate arrays";
cout << "\n3] Display arrays";
cout << "\n4] Generate a N-V Sentence";
cout << "\n5] Generate a N-V-N Sentence";
cout << "\nq] quit\n\nEnter Choice: ";
count++;
}
************************** ********** ********** ********** ********** ********** ********** ***
**************************
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
// screen handling routines
void clearScreen();
void hitEnter(); // press Enter to continue.
void drawMenu(); // screen display for menu
// generate sentence routines
void initArray(char *[], int); //initialize each word to NULL string
void addVerbs(char *[], int); // populate the verbs array with input from user
void addNouns(char *[], int); // populate the nouns array with input from user
void display(char *[], int); // display each string in the Noun or Verb array
void generateNV(char *[], char *[], int);
void generateNVN(char **, char **, int);
const int wordSize = 25; // size of character array
const int listSize = 10; // size of pointer array
int main (void) {
// declare 2 ptr arrays to hold a list of nouns and verbs
char *nouns[listSize];
char *verbs[listSize];
char *test[4] = {"one","two", "three", "four"};
// allocate memory for 10 character strings in each ptr array
for(int i=0; i < listSize; i++)
nouns[i] = new char[wordSize];
for (int i=0; i < listSize; i++)
verbs[i] = new char[wordSize];
char choice; // user menu choice
do {
do { // do some validation on user input here....
drawMenu();
cin.get(choice);
} while ((choice < 49 || choice > 53) && choice != 'q');
switch (choice)
{
int key;
case '1': initArray(nouns, listSize);
initArray(verbs, listSize);
hitEnter();
drawMenu();
break;
case '2': addNouns ( nouns, listSize);
addVerbs ( verbs, listSize);
hitEnter();
drawMenu();
break;
case '3': display(nouns, listSize);
display(verbs, listSize);
hitEnter();
drawMenu();
break;
case '4': generateNV(nouns, verbs, listSize);
hitEnter();
drawMenu();
break;
case '5': generateNVN(nouns, verbs, listSize);
hitEnter();
drawMenu();
break;
case 'q': cout << "\ngoodbye\n";
break;
default: break;
} // end switch
} while (choice != 'q');
return 0;
} // end main
// ARRAY ROUTINES
// initializes the array words to NULL strings
void initArray(char *ptr[], int size) {
for(int i = 0; i < size; i++) {
*ptr[i] = '\0';
cout << ptr[i];
}
}
// display the array on the screen
void display(char *ptr[], int size) {
for(int i = 0; i < size; i++)
{
if (ptr[i] != NULL)
cout << ptr[i] << endl;
}
cout << endl;
}
// populate the verbs array with 10 verbs input from user
void addVerbs(char *verbs[], int size) {
for(int i = 0; i < size; i++) {
clearScreen();
cout << " Enter " << size - i << " verbs:";
cin >> verbs[i];
}
}
// populate the verbs array with 10 verbs input from user
void addNouns(char *nouns[], int size) {
for(int i = 0; i < size; i++) {
clearScreen();
cout << "Enter " << size - i << " nouns:";
cin >> nouns[i];
}
}
void generateNV( char *nouns[], char *verbs[], int size) {
cout << nouns[rand() % size] << " " << verbs[rand() % size] << endl;
}
void generateNVN( char **nouns, char **verbs, int size )
// note the various ways to access the data in nouns and verbs arrays
{
cout << *(nouns + (rand() % size)) << " " << verbs[rand() % 9] << " "
<< nouns[rand() % size] << endl;
}
// SCREEN HANDLING ROUTINES
/* function hitEnter: programs pauses for user input --------- */
void hitEnter(void) {
char ch;
do // read blanks and trailing CR
cin.get(ch);
while ( ch == ' ');
cout << "\n\nHit Enter to continue....";
cin.get(ch);
}
void clearScreen(void) {
cout << "\033[2J"; // send vt100 ESC sequence to clear screen
return;
}
void drawMenu (void) {
static int count = 0;
cout << "\033[2J"; // send vt100 ESC sequence to clear screen
if(count == 0)
cout << "\t\tWelcome to a program to generate sentences";
else
cout << "\t\t\tHomework #8";
cout << "\n\n";
cout << "\n1] Initialize arrays";
cout << "\n2] Populate arrays";
cout << "\n3] Display arrays";
cout << "\n4] Generate a N-V Sentence";
cout << "\n5] Generate a N-V-N Sentence";
cout << "\nq] quit\n\nEnter Choice: ";
count++;
}
**************************
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER