•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,571 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,617 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2707 | Replies: 13
![]() |
•
•
Join Date: Oct 2007
Posts: 37
Reputation:
Rep Power: 2
Solved Threads: 0
Trying to write code to concatenate two arrays of pointers to chars (char* example[])
The program should randomly generate a sentence using 4 arrays of prepositions, nouns, adjectives, and verbs. I found a concat algorithm somewhere on the net and modified it to fit my needs but whenever i execute the program it returns 20 blank sentences. It probably has to do with scope, or I am putting my *s in the wrong places. Any help is appreciated
Heres my code:
The program should randomly generate a sentence using 4 arrays of prepositions, nouns, adjectives, and verbs. I found a concat algorithm somewhere on the net and modified it to fit my needs but whenever i execute the program it returns 20 blank sentences. It probably has to do with scope, or I am putting my *s in the wrong places. Any help is appreciated
Heres my code:
c++ Syntax (Toggle Plain Text)
//****************************************************************************** //*Title: Sentence Lab //*Author: Micah Vanella Date: October 24,2007 //*The purpose of this program is to create a series of random sentences using //* pointer strings. //****************************************************************************** #include <iostream> #include <cstdlib> //For srand and rand using namespace std; const int numSent=20; //Number of sentences to output const int maxSentSize=128; //Maximum number of characters in the sentence //***********************************************************FUNCTION DECLARATIONS char* makeSentence(char sentence[], char* aArr[], char* nArr[], char* vArr[], char* pArr[], int sizeA, int sizeN, int sizeV, int sizeP); char* concat(char* first, char* second); void printSentence(char *sent); //****************************************************************MAIN FUNCTION void main(){ cout << "Welcome to the Amazing Sentence Maker 4000." << endl; //Introduction cout << "\nPlease enter an integer for the seed value." << endl;//User input of int seedval; // seed value cin >> seedval; srand(seedval); //Set the seed using the user specified seed value //Articles, nouns, verbs, and prepositions declarations char* article[] = {"the", "a", "one", "some", "every", "any"}; char* noun[] = {"boy", "girl", "monkey", "LU", "car"}; char* verb[] = {"drove", "jumped", "ran", "walked", "bit"}; char* prep[] = {"to", "from", "over", "under", "on"}; char sent[numSent][maxSentSize]; //To hold each sentence //Find the number int sizeA = (sizeof(article)/sizeof(*article)); int sizeN = (sizeof(noun)/sizeof(*noun)); int sizeV = (sizeof(verb)/sizeof(*verb)); int sizeP = (sizeof(prep)/sizeof(*prep)); //Print 20 sentences for (int i=0; i<numSent; i++){ makeSentence(sent[i], article, noun, verb, prep, sizeA, sizeN, sizeV, sizeP); printSentence(sent[i]); } char tempChar; cout << "Program ended.\nPlease enter any value and press enter to exit.\n"; cin >> tempChar; return; } //************************************************************************************ //*Function: makeSentence //*Author: Micah Vanella Date: 10/21/07 //*This function creates the sentence by calling a concatenate function to add //* each word to the "sentence" array and eventually returning the completed sentence //*The sentence structure is the following: Article, noun, verb, preposition, article, //* noun. //************************************************************************************ char* makeSentence(char sentence[], char* article[], char* noun[], char* verb[], char* prep[], int sizeA, int sizeN, int sizeV, int sizeP){ *sentence=0; //Initialize the array of chars concat(sentence, article[rand()%sizeA]); concat(sentence, noun[rand()%sizeN]); concat(sentence, verb[rand()%sizeV]); concat(sentence, prep[rand()%sizeP]); concat(sentence, article[rand()%sizeA]); concat(sentence, noun[rand()%sizeN]); return sentence; } //************************************************************************** //*Function: concat //*Author: Micah Vanella Date: 10/21/07 //*This function concatenates two strings passed in. It adds the src string //* to the end of the dst string. It places a space between the two words. //************************************************************************** char* concat(char* dst, char* src){ while (*dst){ //get to the end of the dst string ++dst; } *dst++ = ' '; //add a space between the words then increment do{ *dst++ = *src; //copy the src string to the end of the dst string } while (*src++); return dst; } //************************************************************************** //*Function: printSentence //*Author: Micah Vanella Date: 10/21/07 //*This function takes a sentence string, changes the first character to a //* capital, then adds a period at the end and sends it to the display. //************************************************************************** void printSentence(char *sent){ toupper(*sent); while(*++sent); *sent++ = '.'; *sent = 0; cout << sent << endl; return; }
Last edited by weasel7711 : Oct 30th, 2007 at 12:54 am.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
lines 85 thru 88: use a while loop
function printSentence at line 98: you have to save pointer sent before executing lines 100 thru 102, then use the saved pointer in line 103 because as written that function is destroying the value of the pointer.
while( *dest++ = *src++ ) ;
function printSentence at line 98: you have to save pointer sent before executing lines 100 thru 102, then use the saved pointer in line 103 because as written that function is destroying the value of the pointer.
Last edited by Ancient Dragon : Oct 30th, 2007 at 1:15 am.
What's dst pointing to when you return it from concat?
Last edited by Dave Sinkula : Oct 30th, 2007 at 1:35 am.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
did you correct printSentence() that I mentioned?
•
•
Join Date: Oct 2007
Posts: 37
Reputation:
Rep Power: 2
Solved Threads: 0
•
•
•
•
lines 85 thru 88: use a while loop
while( *dest++ = *src++ ) ;
function printSentence at line 98: you have to save pointer sent before executing lines 100 thru 102, then use the saved pointer in line 103 because as written that function is destroying the value of the pointer.
do you mean something like this:
c++ Syntax (Toggle Plain Text)
void printSentence(char *sent){ *sent = toupper(*sent); while(*++sent); *sent++ = '.'; *sent = 0; cout << sent << endl; return; }
Isn't it modified by address therefore its modified permanently? I was having it just point to itself. I wrote the function to return a value but i figured since it modifies the string anyway I don't need it to return a value. I just left the return in there in case I want to reuse the code later.
Last edited by weasel7711 : Oct 30th, 2007 at 10:32 am.
•
•
•
•
••••function printSentence at line 98: you have to save pointer sent before executing lines 100 thru 102, then use the saved pointer in line 103 because as written that function is destroying the value of the pointer.
do you mean something like this:
?c++ Syntax (Toggle Plain Text)
void printSentence(char *sent){ *sent = toupper(*sent); while(*++sent); *sent++ = '.'; *sent = 0; cout << sent << endl; return; }
Isn't it modified by address therefore its modified permanently? I was having it just point to itself. I wrote the function to return a value but i figured since it modifies the string anyway I don't need it to return a value. I just left the return in there in case I want to reuse the code later.
No. All you did is change the first character to upper case. At the end of the function sent point to the end of string where you loaded a 0. Outputting it now simply outputs a null string.
You need to copy the pointer in sent to another variable and use that variable to do your changing. Leave sent alone so it still points to the beginning of the string for outputting.
And it's perfectly acceptable to use a
return at the end of a function. It actially helps readability. Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
-- Pearl Williams
•
•
Join Date: Oct 2007
Posts: 37
Reputation:
Rep Power: 2
Solved Threads: 0
Sorry if this is getting annoying to anyone,
I tried the following:
but it just outputs a bunch of crazy characters then errors out. Pointers cause me mental anguish.
I tried the following:
c++ Syntax (Toggle Plain Text)
void printSentence(char *sentPtr){ char *sent = sentPtr; toupper(*sent); while(*++sent); *sent++ = '.'; *sent = 0; cout << sentPtr << endl; return; }
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
It doesn't work because line 4 doesn't do anything. This function will convert the entire sentence to all upper-case. Hope that's what you want to do.
[edit] I compiled and ran your original program with the changes I made above and it compiled and ran without error. I did not see the problem you reported.[/edit]
cplusplus Syntax (Toggle Plain Text)
void printSentence(char *sentPtr){ char *sent = sentPtr; while(*sent) { if(islower(*sent)) *sent = toupper(*sent); sent++; } *sent++ = '.'; *sent = 0; cout << sentPtr << endl; }
[edit] I compiled and ran your original program with the changes I made above and it compiled and ran without error. I did not see the problem you reported.[/edit]
Last edited by Ancient Dragon : Oct 30th, 2007 at 11:03 pm.
•
•
•
•
It doesn't work because line 4 doesn't do anything. This function will convert the entire sentence to all upper-case. Hope that's what you want to do.
cplusplus Syntax (Toggle Plain Text)
void printSentence(char *sentPtr){ char *sent = sentPtr; while(*sent) { *sent = toupper(*sent++); } *sent++ = '.'; *sent = 0; cout << sentPtr << endl; }
toupper() by definition only sets lower case letters to uppercase and will leave all other characters alone. Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
-- Pearl Williams
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Where to declare varaiables? (Java)
- first cannot conver std::string to const char, now runtime error! (C++)
- problem in concatenation of 2 strings (C)
- Is there a way to tokenize an array of strings (C++)
- simple class programming but getting lost. (C++)
- very 1st python tutorial 4 newbies (Python)
- using stringstream for tokenizing a string (C++)
- filling an array with strings using gets() (C)
- C++ handling of strings in a boolean expression (C++)
- string translation (Java)
Other Threads in the C++ Forum
- Previous Thread: listview groups
- Next Thread: c++ project



Linear Mode