Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Pointers, Arrays for Grammar Logic

Posted on 2005-03-03
3
Medium Priority
?
266 Views
Last Modified: 2010-04-17
// This code runs but program misbehaves, please advise.

*********************************Code*****************************************************
#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++;

}

*****************************************************************************************
0
Comment
Question by:edelossantos
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
Juan Ocasio earned 2000 total points
ID: 13454081
Here is what I did to modify it:

Change

  for (int i=0; i < listSize; i++)
     verbs[i] = new char[wordSize];

to   for (int x=0; x < listSize; x++)
     verbs[x] = new char[wordSize];

Remove
 int key;

you do not reference this variable anywhere.

I used Visual C++ so my clear screen would be different.  I take it you are using UNIX?

HTH

jocasio
0
 
LVL 15

Assisted Solution

by:Juan Ocasio
Juan Ocasio earned 2000 total points
ID: 13454207
Also  Move your drawMenu outside of the do loop, you do this everytime so it is duplicating the method

do {
   
     do {     // do some validation on user input here....
       drawMenu();
       cin.get(choice);
     } while ((choice < 49 || choice > 53) && choice != 'q');

to

drawMenu();
do {
     do {     // do some validation on user input here....

       cin.get(choice);
     } while ((choice < 49 || choice > 53) && choice != 'q');
0
 

Author Comment

by:edelossantos
ID: 13454438
Perfect...thank you.  Del
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question