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...

  • 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() {...

  • 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...

  • 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...

  • Can someone please help me with this code? I'm writing in C++. Thank you in advance....

    Can someone please help me with this code? I'm writing in C++. Thank you in advance. Complete a program that represents a Magic Eight Ball (a Magic Eight Ball allows you to ask questions and receive one of several random answers). In order to complete this, you will need a couple of new functions. First, in order to get a line of input that can contain spaces, you cannot use cin, but instead will use getline: string question; cout <<...

  • Use the file processing example program shown at the bottom. Modify the program to enter the...

    Use the file processing example program shown at the bottom. Modify the program to enter the grades, and count number of grades. Also get total and average grade. Use the following data for grades: 79, 99, 85, 97, 88, 95, 100, 87, 94 EXAMPLE OUTPUT Total: 9 grades Total grade sum: 824 Average grade: 92 Letter grade: A / Using Break, and Continue #include "stdafx.h" #include using namespace std; int main() { int i = 0; for (int x =...

  • Write a C++ program to allow the user to create a test bank of questions. The...

    Write a C++ program to allow the user to create a test bank of questions. The program should first ask the user how many questions he or she wishes to create. This quantity will be the first line in the test bank. The user should now be prompted for all information for each question, and then that question is written out to the test bank in the exact format specified in the Phase 2 Individual Project. For each question, 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
ADVERTISEMENT