Question

I'm trying to get this program to give the user an option to enter a random...

I'm trying to get this program to give the user an option to enter a random word and then have that word included at the end of each break in the story. I can get the option to ask for a word, but can't get the inputted word in the sentence to finish it. I believe my string story is wrong. Please help!

//This program will ask a user to input three random words.
//The words will be placed within the story mode executed by the program.
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    string story =   
    cout<<"There once was a guy named Fred. Fred was not a nice man at all. He would never help anyone but himself. He was so " << endl;
 
   string n,a,v;
   //reading 3 words
   cout<<"Enter a word :";
   cin>>n;
 
   cout << "Fred was always selfish and full of himself. Till one day a magic mirror landed in front of him. In one word, describe what you see? - said the mirror. " << endl;
 
   cout<<"Enter a word :";
   cin>>a;
 
   cout<<"Fred was so excited by his beautiful response, that the mirror transformed the image on glass into a hideous beast. Fred was so alarmed by what he saw, he gasped: What is this creature? Your inner reflection, said the mirror. It's so  " << endl;
   cout<<"Enter a word :";
   cin>>v;
 
   cout<<"Once Fred realized how ugly he looked inside, he decided he would change and be nice and gentle towards everyone. He turned to walk away, but quickly turn back to thank the magic mirror - who mysteriously disappeared! Fred whispered one word before leaving, (Thank you)." << endl;
 
   ofstream myfile;//to read file
myfile.open ("story.txt");//opening file
string story= " "+n+" and "+a+"\n "+v+"\n";
myfile <<story<<endl;//writing story to file
myfile.close();//closingfile
 
   return 0;
}
 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the updated code below:


//This program will ask a user to input three random words.
//The words will be placed within the story mode executed by the program.
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
   string lineOne ="There once was a guy named Fred. Fred was not a nice man at all. He would never help anyone but himself. He was so ";
   cout<<lineOne<<endl;

   string n,a,v;
   //reading 3 words
   cout<<"Enter a word :";
   cin>>n;

   string lineTwo="Fred was always selfish and full of himself. Till one day a magic mirror landed in front of him. In one word, describe what you see? - said the mirror. ";
   cout<<lineTwo<<endl;


   cout<<"Enter a word :";
   cin>>a;


   string lineThree="Fred was so excited by his beautiful response, that the mirror transformed the image on glass into a hideous beast. Fred was so alarmed by what he saw, he gasped: What is this creature? Your inner reflection, said the mirror. It's so ";
   cout<<lineThree<<endl;
   cout<<"Enter a word :";
   cin>>v;

   string lineFour="Once Fred realized how ugly he looked inside, he decided he would change and be nice and gentle towards everyone. He turned to walk away, but quickly turn back to thank the magic mirror - who mysteriously disappeared! Fred whispered one word before leaving, (Thank you).";
   cout<<lineFour<<endl;

   ofstream myfile;//to read file
   myfile.open ("story.txt");//opening file
   string story= lineOne+" "+n+" "+" and "+lineTwo+a+" "+lineThree+v+" "+lineFour;
   myfile <<story<<endl;//writing story to file
   myfile.close();//closingfile


   return 0;
}

output:

story.txt

Add a comment
Know the answer?
Add Answer to:
I'm trying to get this program to give the user an option to enter a random...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hey, so i am trying to have my program read a text file using a structure...

    Hey, so i am trying to have my program read a text file using a structure but i also want to be able to modify the results(I kinda have this part but it could be better). I cant seem to get it to read the file(all the values come up as 0 and i'm not sure why because in my other program where it wrote to the txt file the values are on the txt file) i copied and pasted...

  • I'm trying to write a program that can ask a user about what type of solid...

    I'm trying to write a program that can ask a user about what type of solid they have and to be able to enter values like radius and length of the shape so the program can calculate the volume of a shape and print the result. I also want to give them a repeating option to input another shape if they want to. I'm trying to use at least 7 functions that can display an output, the volumes of a...

  • I'm trying to write in C++ a way to have the user input an double one...

    I'm trying to write in C++ a way to have the user input an double one at a time and have the program output (on entering 0) the number of times those variables differ by at least 1. For example if I enter 1, 1.7, 0.8, -0.1, -1, 0 ... I should get back 4. (or if I just input 0, the output should just be 0). I'm having trouble understanding how to use a while/for operation to make this...

  • /* C++ program that prompts the user to enter the name of the student, enter the...

    /* C++ program that prompts the user to enter the name of the student, enter the five scores values. Then calculate the total scores of the student. Then finally calculate the average score of the total score value. Then, print the name, total score , average scrore value on console output. */ //main.cpp //include header files #include<iostream> #include<string> #include<iomanip> using namespace std; //start of main function int main() {    //declare variable to read string value    string studentName;   ...

  • c++ programming : everything is done, except when you enter ("a" ) in "F" option ,...

    c++ programming : everything is done, except when you enter ("a" ) in "F" option , it does not work. here is the program. #include <iostream> #include <string> #include <bits/stdc++.h> #include <iomanip> #include <fstream> using namespace std; #define MAX 1000 class Inventory { private: long itemId; string itemName; int numberOfItems; double buyingPrice; double sellingPrice; double storageFees; public: void setItemId(long id) { itemId = id; } long getItemId() { return itemId; } void setItemName(string name) { itemName = name; } string...

  • The following program is in c++ ; I am trying to read in games from a...

    The following program is in c++ ; I am trying to read in games from a file and when doing so I am only reading in parts of the file. It is giving me a segmentation fault error I am going to post my code below if anyone is able to help me debug the program I would greatly appreciate it. The program is too large to be submitted as a single question on here :( --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void Business::addGamesFromFile() {...

  • I am trying to write this code which asks "Write a program that ask the user,...

    I am trying to write this code which asks "Write a program that ask the user, the question: What is a^b? //The program generates two signed integers and gives the user four attempts to get the correct answer //a=- , b = + //a= + , b = - " So far this what I wrote. I am not sure how to do this correctly. #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() {    srand(time(0));    int guess,a,ans,b, k;...

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • I need to make a few changes to this C++ program,first of all it should read...

    I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...

  • Topics c ++ Loops While Statement Description Write a program that will display a desired message...

    Topics c ++ Loops While Statement Description Write a program that will display a desired message the desired number of times. The program will ask the user to supply the message to be displayed. It will also ask the user to supply the number of times the message is to be displayed. It will then display that message the required number of times. Requirements Do this assignment using a While statement.    Testing For submitting, use the data in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT