Where is the Syntax Error?

Hello there, i'm starting my first C++ class and i'm having trouble finding the logic error and syntax error in my code. The programs purpose is to write a mad lib by inserting your own words into the program.

I have visual studio 2012 as my compiler and it keeps saying the line "pet: "; is a syntax error, however when I remove that line the line bellow it is giving me said syntax error instead?

Any tips on how to fix that and improve this program would be greatly appreciated.


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
#include <iostream>
#include <fstream>
using namespace std;
#include <string>
int main()
	/**************************************************************************************************************************************
	This is a C++ Program that will allow us to write a mad lib telling our instructor why our homework is late.
	***************************************************************************************************************************************/
{
	// I will declare the variables of my mablibs here.
	int myName, aFood, aNumber, anAdjective, aColor, anAnimal; 

	// I will set my intro.
	cout << "Dear Instructor \n"; 
	cout << "\n"; 
	cout << "I am sorry that I am unable to turn in my homework at this time."; 

	// Now that i've set my introduction I will enter the user input to create the madlib.
	cout << "First, I ate a rotten:";
	cin >> aFood; 

	cout << "which made me turn: and extemely ill.";
	cin >> aColor;

	cout << "I came down with a fever of:"; 
	cin >> aNumber; 

	cout << "Next my:"; 
	cin >> anAdjective
	// My code is erroring right here, the line is exactly the same 
	// My code is erroring right here, the line is exactly the same 

        //This is the line that keeps giving me an error message.
	cout << "pet: "; 
        //This is the line that keeps giving me an error message.
	cin >> anAnimal; 

	cout << "must have smelled the remains of the: "; 
	cin >> aFood; 

	cout << "on my homework because he ate it. I am currently rewriting my homework and hope you will accept it late."; 
	cout << "Sincerely\n"; 
	cout << "John Sprunger" 
		//ending of madlib
	return 0;
}
Lines 29 and 43 are missing a semi-colon.
Also, you realize that all you're doing is inputting integers?
Last edited on
No I did not, and I have no idea how to change that, it's the second week of my first programming class. lol
Well, since this seems to be a homework assignment, and since you're including <string>, my guess is that your teacher wants you to use strings.
Have you covered strings in class / are you familiar with strings?
You have included <string>, so you may as well use it. Try this instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
using namespace std;

int main() {
    string myName, aFood, aNumber, anAdjective, aColour, anAnimal;

    // ...

    cout << "First, I ate a rotten: ";
    // either
    cin >> aFood;        // For one word
    // or
    getline(cin, aFood); // For many words

    // ...
}


Also, why have the variables if you never use them?
Last edited on
xismn- We covered them last class and I've done very little python and javascript on my own so i'm familiar with the concept of strings.
The format in which C++ is asking to me define variable's is confusing i'm use to being able to say "var thisVariable" and be done without much thought else.
NT3- the variables are required for the assignment, not sure why she's having us use them myself. I appreciate the help.

When I asked her in an email she said "The program should be asking the user to enter specific text and then display the madlib after."
I"m not sure what she's getting after?
Last edited on
I think that she wants you to enter things like a food, a name, an adjective, a number, etc, and then to write out your statements using the variables that you input before.

Also, it appears that one of your inputs is available as an integer: aNumber wouldn't make much sense as a string...
Alright, so, the way your current program works is by displaying a bit of text, then waiting for user input, then displaying some more text, then waiting again, etc.
Obviously, this is not how madlibs work. First, the program should ask the user to enter certain kinds of words/input (adjective, noun, number, whatever). When the user enters all the information, it should display the entire madlib with the user's words in the appropriate places.

Using strings for this is the only sensible choice. There's no reason she wouldn't want you to use strings.

Here's some very simple code using a string - there's no error handling.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

int main() {
	std::string adjective;

	std::cout << "Enter an adjective:\t";
	std::getline(std::cin, adjective);


	std::cout << "The dog was " << adjective << "." << std::endl;

	std::cin.get();
	return 0;
}
Last edited on
Hey thanks for all the help, I have my program working now, it may not be super efficient but it was good enough for my class, this is what I ended up with.

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
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <fstream>
using namespace std;
#include <string>
int main()
	/**************************************************************************************************************************************
	This is a C++ Program that will allow us to write a mad lib telling our instructor why our homework is late.
	***************************************************************************************************************************************/
{
	// I will declare the variables of my mablibs here.
	int aNumber;
	string myName, aFood, anAdjective, aColor, anAnimal;

	
	// Now that i've set my introduction I will enter the user input to create the madlib.

	cout << "Whats your name?: "; 
	cin >> myName; 

	cout << "Pick a food: ";
	cin >> aFood; 

	cout << "Pick a color: ";
	cin >> aColor;

	cout << "Pick a number between 100-200: "; 
	cin >> aNumber; 

	cout << "Pick an adjective: "; 
	cin >> anAdjective;

	cout << "Pick an animal: "; 
	cin >> anAnimal; 

	// Now that the user has set his variables I will create the madlib story. 

		// I will set my intro.
	cout << "Dear Instructor \n"; 
	cout << "\n"; 
	cout << "I am sorry that I am unable to turn in my homework at this time."; 


	cout << " First I ate ";
	cout << aFood;

	cout << " which made me turn ";
	cout << aColor;
	cout << " and extremely ill. ";

	cout << "I came down with a fever of ";
	cout << aNumber; 

	cout << ". Next my ";
	cout << anAdjective; 
	cout << " pet "; 
	cout << anAnimal; 

	cout << " must have smelled the remains of the ";
	cout << aFood; 


		// I will set my outro. 
	cout << " on my homework because he ate it. I am currently rewriting my homework and hope you will accept it late."; 
	cout << " Sincerely\n"; 
	cout << myName;
		//ending of madlib

	return 0;
}
Topic archived. No new replies allowed.