Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / String input game crashes on strcpy. Any Advice?

Author
Message
FrankieV
14
Years of Service
User Offline
Joined: 10th Jun 2010
Location:
Posted: 18th Jun 2010 07:19
This is supposed to be a silly "Mad Lib" game. I just copied the example from my course's book and made my changes. The example executes just fine, so what am I missing? Is there any way I can make this code better and more efficient?
Cuddle Bunniezzz 12
15
Years of Service
User Offline
Joined: 14th Jan 2009
Location: Buffalo, NY
Posted: 19th Jun 2010 07:42 Edited at: 19th Jun 2010 07:43
What I am seeing here is that your variables are not declared in the global scope.

add this section of code after your "#include"s.



I'm not sure how new you are to C++, but FYI, each variable is now allowed to be 16 characters long, it's about as much as you will need for a simple mad-lib.

Second, go through all your code, and look for those variables, and add "gVar::" to the beginning of each variable.

Ex:


Last, get rid of all your argument parameters for each function, ex:


I'm away from a compiler right now, so I can't test this code at them moment, it should work though. If you don't understand what is going on, in the morning I'll provide and explanation.

http://ref.darkgdk.us/ <- Online DarkGDK Refernece. More content coming soon.
jamesL
14
Years of Service
User Offline
Joined: 31st May 2010
Location:
Posted: 22nd Jun 2010 08:04
C++ ALWAYS you to have variables more than 16 chars long and there is no need to make them global

the only thing you have to make global is
const int WORD_SIZE = 30;

the problem is the length of your buffers

you have
const int WORD_SIZE = 30;
char color[WORD_SIZE];
strcpy( color, dbInput() );

but what if somebody enters
blueeeeeeeeeeeeeeeeeeeeeeeeeeee

that's more than 30 chars
that would overflow the buffer and cause a crash, you need to use
strcpy_s( color, WORD_SIZE-1, dbInput() );

that way only 29 chars will be copied no matter what they enter (you have to save one char for the null terminator)
and that's why you need to make the WORD_SIZE definition global, so the input function will know what WORD_SIZE is

you also have a problem with
const int LINE_SIZE = 80;

char line1[LINE_SIZE];

// Build line 1.
strcpy( line1, "1. Select the type of bread you want to use. Many prefer the taste of ");
strcat( line1, color );
strcat( line1, " while others prefer wheat bread because it is healthy.");

count and see how many chars you just copied into line1
way more than 80
your over flowed the buffer again

you could just use
printf
or even
puts

puts( "1. Select the type of bread you want to use. Many prefer the taste of ");
puts ( color );
puts (" while others prefer wheat bread because it is healthy.\n");

and get rid of line1, line2, line3 and so on

Login to post a reply

Server time is: 2024-11-19 20:56:56
Your offset time is: 2024-11-19 20:56:56