?
Solved

Pointers, Arrays for Grammar Logic

Posted on 2005-03-03
3
Medium Priority
?
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 14

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 14

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Introduction to Processes
Starting up a Project
Suggested Courses

771 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