Using madlib in Visual Studio

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 3667 Views - Last Post: 08 September 2009 - 09:17 PM Rate Topic: -----

#1 izic   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 223
  • Joined: 15-January 09

Using madlib in Visual Studio

Posted 04 September 2009 - 10:14 AM

I'm working on program that will have the following as output:

Enter the name of a place:
Paris

Enter the title of a relative:
Uncle

Enter the name of a color:
Orange

Enter the name of a household item:
shower curtain

MadLib: The next time you go to Paris, don't forget to pick up an orange shower curtain for your Uncle.
===================================

This is my code so far . Looking at my code, how i do use MadLib to get the output I want?

//precomplier directive
#include "DarkGDK.h" 

//main function
void DarkGDK(){				
	
	//Creates a memory location called "nameofplace"; Stores up to 50 characters.
	char nameofplace[50];		
	
	//Creates a memory location called "titleofrelative"; Stores up to 20 characters.
	char titleofrelative[20];
	
	//Creates memory location called "nameofcolor"; Stores up to 15 characters.
	char nameofcolor[15];
	
	//Creates a memory location called "householditem"; Stores up to 50 characters.
	char householditem[50];
	
	//Prompt line; Takes text between quotations and sends them to computer screen.
	dbPrint("Enter the name of a place:");
	
	//Waits for user's input and then stores it in memory location nameofplace.
	strcpy(nameofplace, dbInput());

	//Prompt line; Prompt user to enter the title of a relative.
	dbPrint("Enter the title of a relative:");
	
	//Waits for user's input and then stores it in memory location titleofrelative.
	strcpy(titleofrelative, dbInput());

	//Prompt line; Prompt user to enter the name of a color.
	dbPrint ("Enter the name of a color:"); 
	
	//Waits for user's input and then stores it in title of relative.
	strcpy(nameofcolor, dbInput());

	//Prompt line; Prompt user to enter the name of a household item.
	dbPrint ("Enter the name of a household item:");

	//Waits for user's input and then stores it in titleofrelative.
	strcpy(householditem, dbInput());

	//Pause program until a key is pressed.
	dbWaitKey();


}

Is This A Good Question/Topic? 0
  • +

Replies To: Using madlib in Visual Studio

#2 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Using madlib in Visual Studio

Posted 04 September 2009 - 10:20 AM

I haven't worked with DarkGDK that much but you should try printing the strings out with the dbPrint function like this:
dbPrint("The next time you go to " + place + ", don't forget to pick up a " + color + " " + householdItem + " for your " + relative);
Was This Post Helpful? 0
  • +
  • -

#3 izic   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 223
  • Joined: 15-January 09

Re: Using madlib in Visual Studio

Posted 04 September 2009 - 10:29 AM

I tried your code and i got " error C2110: '+' : cannot add two pointers"
Was This Post Helpful? 0
  • +
  • -

#4 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Using madlib in Visual Studio

Posted 04 September 2009 - 10:31 AM

Okay try first creating the madlib and then printing it like this:
char* madlib;

strcpy(madlib, "The next time you go to ");
strcpy(madlib, place);
// ... copy all the rest on

dbPrint(madlib);


Was This Post Helpful? 1
  • +
  • -

#5 izic   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 223
  • Joined: 15-January 09

Re: Using madlib in Visual Studio

Posted 04 September 2009 - 10:43 AM

So this is what my code should look like?

char* madlib;

strcpy(madlib, "The next time you go to " + nameofplace + ", don't forget to pick up a " + nameofcolor + " " + householditem + " for your " + titleofrelative);
strcpy(madlib, nameofplace);
strcpy(madlib, nameofcolor);
strcpy(madlib, householditem);
strcpy(madlib, titleofrelative);

dbPrint(madlib);
Was This Post Helpful? 0
  • +
  • -

#6 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Using madlib in Visual Studio

Posted 04 September 2009 - 05:56 PM

No like this:
char* madlib;

strcpy(madlib, "The next time you go to ");
strcpy(madlib, nameofplace);
strcpy(madlib, " , don\'t forget to pick up a ");
strcpy(madlib, nameofcolor);
strcpy(madlib, " ");
strcpy(madlib, householditem);
strcpy(madlib, " for your ");
strcpy(madlib, titleofrelative);

Was This Post Helpful? 0
  • +
  • -

#7 izic   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 223
  • Joined: 15-January 09

Re: Using madlib in Visual Studio

Posted 08 September 2009 - 08:50 AM

This is what code looks like now. It's still not printing out the following:

MadLib: The next time you go to Paris (nameofplace), don't forget to pick up a orange (nameofcolor), shower curtain (household item) for your Uncle(titleofrelative0.

Can anyone show me where I'm going wrong?

//precomplier directive
#include "DarkGDK.h"
//main function
void DarkGDK(){	
 
 //Creates a memory location called "nameofplace"; Stores up to 50 characters.
 char nameofplace[50];  
 
 //Creates a memory location called "titleofrelative"; Stores up to 20 characters.
 char titleofrelative[20];
 
 //Creates memory location called "nameofcolor"; Stores up to 15 characters.
 char nameofcolor[15];
 
 //Creates a memory location called "householditem"; Stores up to 50 characters.
 char householditem[50];

 //Prompt line; Takes text between quotations and sends them to computer screen.
 dbPrint("Enter the name of a place:");
 
 //Waits for user's input and then stores it in memory location nameofplace.
 strcpy(nameofplace, dbInput());
 //Prompt line; Prompt user to enter the title of a relative.
 dbPrint("Enter the title of a relative:");
 
 //Waits for user's input and then stores it in memory location titleofrelative.
 strcpy(titleofrelative, dbInput());
	//Prompt line; Prompt user to enter the name of a color.
 dbPrint ("Enter the name of a color:");
 
 //Waits for user's input and then stores it in title of relative.
 strcpy(nameofcolor, dbInput());
 //Prompt line; Prompt user to enter the name of a household item.
 dbPrint ("Enter the name of a household item:");
 //Waits for user's input and then stores it in titleofrelative.
 strcpy(householditem, dbInput());

 char* madlib 
 strcpy(madlib, "The next time you go to ");
 strcpy(madlib, nameofplace);
 strcpy(madlib, " , don't forget to pick up a ");
 strcpy(madlib, nameofcolor);
 strcpy(madlib, " ");
 strcpy(madlib, householditem);
 strcpy(madlib, " for your ");
 strcpy(madlib, titleofrelative);

 //Pause program until a key is pressed.
 dbWaitKey();
}

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Using madlib in Visual Studio

Posted 08 September 2009 - 10:43 AM

char* madlib
 strcpy(madlib, "The next time you go to ");
 strcpy(madlib, nameofplace);
 strcpy(madlib, " , don't forget to pick up a ");
 strcpy(madlib, nameofcolor);
 strcpy(madlib, " ");
 strcpy(madlib, householditem);
 strcpy(madlib, " for your ");
 strcpy(madlib, titleofrelative);


You have not allocated any memory for the madlib variable. That pointer could be pointing anywhere in memory, and you're overwriting the memory there. You're lucky the program hasn't just crashed outright.
Was This Post Helpful? 0
  • +
  • -

#9 izic   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 223
  • Joined: 15-January 09

Re: Using madlib in Visual Studio

Posted 08 September 2009 - 02:09 PM

I've tried allocating memory for it. Instead of getting what I want for output, I keep getting "X^A for output" Here's my code now.

This is what I want for output:
MadLib: The next time you go to Paris (nameofplace), don't forget to pick up a orange (nameofcolor), shower curtain (household item) for your Uncle(titleofrelative).

Can anyone please help and explain in-depth what I'm doing wrong? If you could explain to me on aim that'll be an even better idea.

void DarkGDK(){				
   
	//Creates a memory location called "nameofplace"; Stores up to 50 characters.
	char nameofplace[50];		
   
	//Creates a memory location called "titleofrelative"; Stores up to 20 characters.
	char titleofrelative[20];
   
	//Creates memory location called "nameofcolor"; Stores up to 15 characters.
	char nameofcolor[15];
   
	//Creates a memory location called "householditem"; Stores up to 50 characters.
	char householditem[50];
   
	char* madlib [100];
	strcpy(madlib, "The next time you go to ");
	strcpy(madlib, nameofplace);
	strcpy(madlib, " , don't forget to pick up a ");
	strcpy(madlib, nameofcolor);
	strcpy(madlib, " ");
	strcpy(madlib, householditem);
	strcpy(madlib, " for your ");
	strcpy(madlib, titleofrelative);

	//Prompt line; Takes text between quotations and sends them to computer screen.
	dbPrint("Enter the name of a place:");
   
	//Waits for user's input and then stores it in memory location nameofplace.
	strcpy(nameofplace, dbInput());

	//Prompt line; Prompt user to enter the title of a relative.
	dbPrint("Enter the title of a relative:");
   
	//Waits for user's input and then stores it in memory location titleofrelative.
	strcpy(titleofrelative, dbInput());

	//Prompt line; Prompt user to enter the name of a color.
	dbPrint ("Enter the name of a color:");
   
	//Waits for user's input and then stores it in title of relative.
	strcpy(nameofcolor, dbInput());

	//Prompt line; Prompt user to enter the name of a household item.
	dbPrint ("Enter the name of a household item:");

	//Waits for user's input and then stores it in titleofrelative.
	strcpy(householditem, dbInput());
	
	dbPrint (madlib);

	//Pause program until a key is pressed.
	dbWaitKey();
}

Was This Post Helpful? 0
  • +
  • -

#10 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Using madlib in Visual Studio

Posted 08 September 2009 - 02:20 PM

Oops, my bad. strcpy replaces the whole string.

Here's what you want:
 char madlib [256];
	strcpy(madlib, "The next time you go to ");
	strcat(madlib, nameofplace);
	strcat(madlib, " , don't forget to pick up a ");
	strcat(madlib, nameofcolor);
	strcat(madlib, " ");
	strcat(madlib, householditem);
	strcat(madlib, " for your ");
	strcat(madlib, titleofrelative);


Was This Post Helpful? 0
  • +
  • -

#11 izic   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 223
  • Joined: 15-January 09

Re: Using madlib in Visual Studio

Posted 08 September 2009 - 02:29 PM

Amazing, the output is finally showing up but there's a few problems.

This is what the output looks like:
"The next time you go to !J](nameofplace), don't forget to pick a $]^ for your xA^."

This is what I entered:
Enter name of place: Spain
Enter title of relative: Uncle
Enter name of color: red
Enter household item: broom

This is what my code looks like now:
void DarkGDK(){				
   
	//Creates a memory location called "nameofplace"; Stores up to 50 characters.
	char nameofplace[50];		
   
	//Creates a memory location called "titleofrelative"; Stores up to 20 characters.
	char titleofrelative[20];
   
	//Creates memory location called "nameofcolor"; Stores up to 15 characters.
	char nameofcolor[15];
   
	//Creates a memory location called "householditem"; Stores up to 50 characters.
	char householditem[50];
   
	char madlib [256];
	strcpy(madlib, "The next time you go to ");
	strcat(madlib, nameofplace);
	strcat(madlib, " , don't forget to pick up a ");
	strcat(madlib, nameofcolor);
	strcat(madlib, " ");
	strcat(madlib, householditem);
	strcat(madlib, " for your ");
	strcat(madlib, titleofrelative);

	//Prompt line; Takes text between quotations and sends them to computer screen.
	dbPrint("Enter the name of a place:");
   
	//Waits for user's input and then stores it in memory location nameofplace.
	strcpy(nameofplace, dbInput());

	//Prompt line; Prompt user to enter the title of a relative.
	dbPrint("Enter the title of a relative:");
   
	//Waits for user's input and then stores it in memory location titleofrelative.
	strcpy(titleofrelative, dbInput());

	//Prompt line; Prompt user to enter the name of a color.
	dbPrint ("Enter the name of a color:");
   
	//Waits for user's input and then stores it in title of relative.
	strcpy(nameofcolor, dbInput());

	//Prompt line; Prompt user to enter the name of a household item.
	dbPrint ("Enter the name of a household item:");

	//Waits for user's input and then stores it in titleofrelative.
	strcpy(householditem, dbInput());
	
	dbPrint (madlib);

	//Pause program until a key is pressed.
	dbWaitKey();
}

Was This Post Helpful? 0
  • +
  • -

#12 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Using madlib in Visual Studio

Posted 08 September 2009 - 02:39 PM

That's because your variables don't have anything in them yet. Move that block of code ( the strcpy and strcat stuff ) to the line below strcpy(householditem, dbInput());


i.e.
void DarkGDK(){				
   
	//Creates a memory location called "nameofplace"; Stores up to 50 characters.
	char nameofplace[50];		
   
	//Creates a memory location called "titleofrelative"; Stores up to 20 characters.
	char titleofrelative[20];
   
	//Creates memory location called "nameofcolor"; Stores up to 15 characters.
	char nameofcolor[15];
   
	//Creates a memory location called "householditem"; Stores up to 50 characters.
	char householditem[50];   

	//Prompt line; Takes text between quotations and sends them to computer screen.
	dbPrint("Enter the name of a place:");
   
	//Waits for user's input and then stores it in memory location nameofplace.
	strcpy(nameofplace, dbInput());

	//Prompt line; Prompt user to enter the title of a relative.
	dbPrint("Enter the title of a relative:");
   
	//Waits for user's input and then stores it in memory location titleofrelative.
	strcpy(titleofrelative, dbInput());

	//Prompt line; Prompt user to enter the name of a color.
	dbPrint ("Enter the name of a color:");
   
	//Waits for user's input and then stores it in title of relative.
	strcpy(nameofcolor, dbInput());

	//Prompt line; Prompt user to enter the name of a household item.
	dbPrint ("Enter the name of a household item:");

	//Waits for user's input and then stores it in titleofrelative.
	strcpy(householditem, dbInput());

	char madlib [256];
	strcpy(madlib, "The next time you go to ");
	strcat(madlib, nameofplace);
	strcat(madlib, " , don't forget to pick up a ");
	strcat(madlib, nameofcolor);
	strcat(madlib, " ");
	strcat(madlib, householditem);
	strcat(madlib, " for your ");
	strcat(madlib, titleofrelative);
   
	dbPrint (madlib);

	//Pause program until a key is pressed.
	dbWaitKey();
}

This post has been edited by eker676: 08 September 2009 - 02:40 PM

Was This Post Helpful? 0
  • +
  • -

#13 izic   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 223
  • Joined: 15-January 09

Re: Using madlib in Visual Studio

Posted 08 September 2009 - 02:48 PM

How to make all the text that's suppose to print out fit? It seems the text too long that it doesn't show the last part "for your uncle."
Was This Post Helpful? 0
  • +
  • -

#14 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Using madlib in Visual Studio

Posted 08 September 2009 - 02:55 PM

Place a newline somewhere in the string.

i.e.

strcat(madlib, "\nfor your ");
Was This Post Helpful? 1
  • +
  • -

#15 izic   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 223
  • Joined: 15-January 09

Re: Using madlib in Visual Studio

Posted 08 September 2009 - 02:59 PM

Thank you so much for all your help! I can't tell you how much I appreciate it!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2