Beginner mad libs problem and im lost

Write your question here.

Ive been provided with a skeleton code and must fill the rest out.

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
70
71
72
73
74
75
76
 /*
 * madlibs.cpp  This program prompts the user to type words 
 * that are filled in to a short story, like the game MadLibs.
 */

#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::string;
using std::endl;
using std::setprecision;

int main()
{
 string part1 = "Jack and Jill went up the";
 string noun1 = "hill";
 string part2 = "to fetch a pail of";
 string noun2 = "water";
 
 string part3 = "Jack fell down and";
 string verb1 = "broke";
 string part4 = "his";
 string noun3 = "crown";
 
 string part5 = "and Jill came";
 string verb2 = "tumbling";
 string part6 = "after.";
 
 string part7 = "Jack should really be more";
 string adjective1 = "careful";
 string part8 = "!";
 
 string part9 = "His insurance premiums jumped from $";
 double number1 = 300;
 string part10 =  "to $";
 string part11 = ".";
 
 string part12 = "And really, Jill should be more";
 string part13 = "too.";
 
 string part14 = "Hill climbing can be very";
 string adjective2 = "dangerous";
 string part15 = "! :-)";
 
 /* TODO: prompt the user to enter values for noun1, noun2, noun3, verb1, verb2, adjetive1, adjetive2, and number
  * use cin to actually get the input from the user and store the results in the respective variables.
  */
 cout<< "Enter a noun:\t";
cin >> noun1;
 cout << part1 << " " << noun1 << " " << part2 << " " << noun2 << '.' << endl;
 
 /* TODO:  the rest of these lines don't space the output correctly,
  * and words are running together.  Fix this.
  */
 
 cout << part3 <<  verb1 <<  part4 <<  noun3 ;

 cout << part5 <<  verb2 <<  part6;
 
 cout << part7 << adjective1 << part8 ;
 
 //TODO  number1 actually represents a dollar amount.  
 //Use the iomanip library to force cout to print two decimal points
 cout << part9 << number1 <<  part10 << number1*2  << part11 ;

 cout<< part12<<  adjective1 <<  part13 ;
 
 cout<< part14 <<  adjective2 << part15 ;
  
  return 0;
}
 



Im kind of lost....here is what I've come up with so far.

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

/*
 * madlibs.cpp  This program prompts the user to type words 
 * that are filled in to a short story, like the game MadLibs.
 */

#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::string;
using std::endl;
using std::setprecision;

int main()
{
 string part1 = "Jack and Jill went up the";
 string noun1;
 string part2 = "to fetch a pail of";
 string noun2;
 
 string part3 = "Jack fell down and";
 string verb1;
 string part4 = "his";
 string noun3;
 
 string part5 = "and Jill came";
 string verb2;
 string part6 = "after.";
 
 string part7 = "Jack should really be more";
 string adjective1;
 string part8 = "!";
 
 string part9 = "His insurance premiums jumped from $";
 double number1;
 string part10 =  "to $";
 string part11 = ".";
 
 string part12 = "And really, Jill should be more";
 string part13 = "too.";
 
 string part14 = "Hill climbing can be very";
 string adjective2;
 string part15 = "! :-)";
 
 /* TODO: prompt the user to enter values for noun1, noun2, noun3, verb1, verb2, adjetive1, adjetive2, and number
  * use cin to actually get the input from the user and store the results in the respective variables.
  */
 cout << "Enter a noun:\n";
 cin >> noun1;
 cout << "Enter another noun:\n";
 cin >> noun2;
 cout << "Enter another noun:\n";
 cin >> noun3;
 cout << "Enter a verb:\n";
 cin >> verb1;
 cout << "Enter another verb:\n";
 cin >> verb2;	
 cout << "Enter an adjective:\n";
 cin >> adjective1;
 cout << "Enter another adjective:\n";
 cin >> adjective2;
 cout << "Enter a number:\n";
 cin >> number1;


 cout << part1 << " " << noun1 << " " << part2 << " " << noun2 << '.' << endl;
 
 /* TODO:  the rest of these lines don't space the output correctly,
  * and words are running together.  Fix this.
  */
 
 cout << part3 <<  verb1 <<  part4 <<  noun3 ;

 cout << part5 <<  verb2 <<  part6;
 
 cout << part7 << adjective1 << part8 ;
 
 //TODO  number1 actually represents a dollar amount.  
 //Use the iomanip library to force cout to print two decimal points
 cout << part9 << number1 <<  part10 << number1*2  << part11 ;

 cout<< part12<<  adjective1 <<  part13 ;
 
 cout<< part14 <<  adjective2 << part15 ;
  
  return 0;
}








Last edited on
I am going to assume you are asking about the remaining TODOs in this program.

On line 69 you have output that is spaced properly.
On lines after the TODO on 71 there are some very obvious differences between those cout statements and the cout statement from line 69.

The TODO on line 89 involves using setprecision from the iomanip library:
http://www.cplusplus.com/reference/iomanip/setprecision/
If you need the dollar sign, you can do something very similar to printing a space (" ") between words, but instead do a dollar sign ("$").
Last edited on
Thanks for the tips.

So I did the cout and cin parts to ask for user submission correctly?
You could run the program to see if that is the case. As far as I can tell it should receive the input properly.

Edit:
I went ahead and ran your program on ideone, it would seem that you are getting the input right and just need to fix the output:
http://ideone.com/UjbLIs
Last edited on
code runs fine....spacing todo is easy to do but i cant get setprecision to work right now. Any tips on setprecision? this is what i just tried:
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

/*
 * madlibs.cpp  This program prompts the user to type words 
 * that are filled in to a short story, like the game MadLibs.
 */

#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::string;
using std::endl;
using std::setprecision;

int main()
{
 string part1 = "Jack and Jill went up the";
 string noun1;
 string part2 = "to fetch a pail of";
 string noun2;
 
 string part3 = "Jack fell down and";
 string verb1;
 string part4 = "his";
 string noun3;
 
 string part5 = "and Jill came";
 string verb2;
 string part6 = "after.";
 
 string part7 = "Jack should really be more";
 string adjective1;
 string part8 = "!";
 
 string part9 = "His insurance premiums jumped from $";
 double number1;
 string part10 =  "to $";
 string part11 = ".";
 
 string part12 = "And really, Jill should be more";
 string part13 = "too.";
 
 string part14 = "Hill climbing can be very";
 string adjective2;
 string part15 = "! :-)";
 
 /* TODO: prompt the user to enter values for noun1, noun2, noun3, verb1, verb2, adjetive1, adjetive2, and number
  * use cin to actually get the input from the user and store the results in the respective variables.
  */
 cout << "Enter a noun:\n";
 cin >> noun1;
 cout << "Enter another noun:\n";
 cin >> noun2;
 cout << "Enter another noun:\n";
 cin >> noun3;
 cout << "Enter a verb:\n";
 cin >> verb1;
 cout << "Enter another verb:\n";
 cin >> verb2;	
 cout << "Enter an adjective:\n";
 cin >> adjective1;
 cout << "Enter another adjective:\n";
 cin >> adjective2;
 cout << "Enter a number:\n";
 cin >> number1;

 cout << setprecision(4);
 cout << part1 << " " << noun1 << " " << part2 << " " << noun2 << '.' << endl;
 
 /* TODO:  the rest of these lines don't space the output correctly,
  * and words are running together.  Fix this.
  */
 
 cout << part3 <<  verb1 <<  part4 <<  noun3 ;

 cout << part5 <<  verb2 <<  part6;
 
 cout << part7 << adjective1 << part8 ;
 
 //TODO  number1 actually represents a dollar amount.  
 //Use the iomanip library to force cout to print two decimal points
 cout << part9 << number1 <<  part10 << number1*2  << part11 ;

 cout<< part12<<  adjective1 <<  part13 ;
 
 cout<< part14 <<  adjective2 << part15 ;
  
  return 0;
}

First instead of doing using std::NameOfFunc; for every one you need just do using namespace std;

As for your question what do you mean it isnt working can you tell me what you exspect and what your getting. Untill then move the setprecision() next to var in cout your using it for see if it helps.
I'm expecting the number I input when the program asks me to enter a number to display with two decimal places in the final story that gets printed but that's not occurring.


I was thinking perhaps I did something wrong with where I put setprecision(x)?
Try also using fixed. My guess is you are entering something like 5.00 instead of something like 3.1415926. Check the link I posted earlier on setprecision.
Last edited on
Topic archived. No new replies allowed.