Mad Lib problem

Hi, newbie here. I'm working on a simple program having to do with mad libs. This is the code.

#include <iostream>
using namespace std;

int main()
{
char teacher_name, your_name, food, adjective, color, animal
;int number

;cout << "Welcome to Bad Excuse Generator!\n";

cout << "Enter the name of your instructor:\n";
cin >> teacher_name;

cout << "What's the last thing you've ate today?\n";
cin >> food;

;cout << "Enter a number between 100 and 120:\n";
cin >> number;

;cout << "Enter a creative adjective:\n";
cin >> adjective;

;cout << "Enter your favorite color:\n";
cin >> color;

;cout << "Enter an animal:\n";
cin >> animal;

;cout << "Dear Instructor";
cout << teacher_name
;cout << "I am sorry that I am unable to turn in my homework at this time. First, I ate a rotten";
cout << food
;cout << ",which caused me to turn";
cout << color
;cout << "and extremely ill. I came down with a fever of";
cout << number
;cout << ". Next, my";
cout << adjective
;cout << "pet";
cout << animal
;cout << "must have smelled the remains of the";
cout << food
;cout << "on my homework, because he ate it. I am currently rewriting my homework and hope you will accept it late.\n";
cout << "Sincerely,\n";
cout << your_name

cout << "In all seriousness, though, get off Facebook and do your homework.\n";


;return 0;
}



It works fine at first, but after entering the instructor's name, the program stops and this error message pops up:

Run-Time Check Failure #3 - The variable 'your_name' is being used without being initialized.

And then another tab opens and a code which is too long to put in this message appears. Here's the beginning of it:

/***
*crtexe.c - Initialization for console EXE using CRT DLL
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* This is the actual startup routine for apps linking to the CRT DLL.
* It calls the user's main routine [w]main() or [w]WinMain after
* performing C Run-Time Library initialization.
*
* With ifdefs, this source file also provides the source code for:
* wcrtexe.c the startup routine for console apps with wide chars
* crtexew.c the startup routine for Windows apps
* wcrtexew.c the startup routine for Windows apps with wide chars
*
*******************************************************************************/

#ifdef CRTDLL

/*
* SPECIAL BUILD MACROS! Note that crtexe.c (and crtexew.c) is linked in with
* the client's code. It does not go into crtdll.dll! Therefore, it must be
* built under the _DLL switch (like user code) and CRTDLL must be undefined.
* The symbol SPECIAL_CRTEXE is turned on to suppress the normal CRT DLL
* definition of _fmode and _commode using __declspec(dllexport). Otherwise
* this module would not be able to refer to both the local and DLL versions
* of these two variables.
*/

#undef CRTDLL
#ifndef _DLL
#define _DLL
#endif /* _DLL */

#define SPECIAL_CRTEXE

#include <cruntime.h>
#include <oscalls.h>
#include <internal.h>
#include <process.h>
#include <math.h>
#include <rterr.h>
#include <stdlib.h>
#include <tchar.h>
#include <rtcapi.h>
#include <sect_attribs.h>
#include <locale.h>




What's going on here?

The variable 'your_name' is being used without being initialized.


You are using the variable your_name without having initialised it.

In simpler terms, you never make it equal anything but you're using it anyway.

As an aside, is that craziness with the semi-colons just a cut and paste artifact, or do you actually have semi-colons at the start of lines?
Last edited on
1. use code tags
2. declare variables that can store more than a single character
(optional)
3. learn some kind of input checking, e.g make sure that the user enters only integers between 100-120
4. rtfm, your compiler error messages are your responsibilities to learn and make sense of :P
#include <string>


and then use string instead of char
The semi-colons are because Visual Studio keep displaying errors telling me to put a colon before cout.
^That's because you didn't have any semicolons on the lines before them.
Just, put a semicolon at the end of every line. Unless your compiler say they are not needed. Then take them off.

However, I suppose you COULD put semicolons before cout. Its just bad practice. And looks ugly. ;)
Sorry I take that back..... You do have semicolons. And you did declare your_name.

Try this code instead,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>
using namespace std;

int main()
{
string teacher_name;
string your_name;
string food;
string adjec_1;                   //I put each Variable on a seperate line for clarifications sake.
string color;
string animal;
float number;

cout << "Welcome to Bad Excuse Generator!";

cout <<endl << "Enter the name of your instructor: -> ";
getline (cin, teacher_name);

cout <<endl << "What's the last thing you've ate today? -> ";
getline (cin, food);

cout <<endl << "Enter a number between 100 and 120: -> ";
cin >> number;

cout <<endl << "Enter a creative adjective: -> " ;
getline (cin, adjec_1);

cout <<endl << "Enter your favorite color: -> "; 
getline (cin, color);

cout <<endl << "Enter an animal:- >";
getline (cin, animal);

cout << "Dear Instructor";
cout << teacher_name;
cout << "I am sorry that I am unable to turn in my homework at this time. First, I ate a rotten ";
cout << food;
cout << ",which caused me to turn";
cout << color;
cout << "and extremely ill. I came down with a fever of "; 
cout << number;
cout << ". Next, my ";
cout << adjec_1;
cout << "pet ";
cout << animal;
cout << "must have smelled the remains of the ";
cout << food;
cout << "on my homework, because he ate it. I am currently rewriting my homework and hope you will accept it late.\n";
cout << "Sincerely,\n";
cout << your_name;

cout << endl << "In all seriousness, though, get off Facebook and do your homework.\n";

cout << endl << "Press enter to continue....";
cin.get();


return 0;
}






It should work in your compiler. I made a few changes as you can tell. Anyway, There are still some more problems, but Ill let you figure them out....
Note that this code exhibits the exact same problem that the OP came to us with:

Run-Time Check Failure #3 - The variable 'your_name' is being used without being initialized.
Last edited on
You never enter anything for your_name.

Either have the user enter it, or simply initialize it with your name.

string your_name = 'Professor Scrimpkins';
Topic archived. No new replies allowed.