Question

In: Computer Science

C or C++ program Create a list of 5 things-to-do when you are bored (in the...

C or C++ program

Create a list of 5 things-to-do when you are bored (in the text file things.txt, one line each), where each thing is described as a string of characters of length in between 10 and 50.

Design a C program to read these things in (from stdin or by input redirection) and store them in the least memory possible (i.e., only the last byte in the storage for each string can be the null character).

After reading things in, your C program continues to accept input from the user, it ignores whatever the user enters but the return key, and for each return key it randomly chooses one out of the 5 things to print to the stdout.

Your C program iterates the process for ever until the user enters an EOF (which means by input redirection your program terminates at the end of file).

By randomly choosing a thing, it means you should try to make the probability distribution as evenly as possible to any one of the 5 things-to-do.

Any help is appreciated!!

Thank you!!

Solutions

Expert Solution

Hello, Student I hope you are doing good and well in lockdwon.

Here is implementation of your problem which randomly print one (1) things-to-do from a file which is written by user, I have tried my best to include comments as much as I can , still if you have any problem then please ask me in comment section, I am always happy to help.

program.cpp

#include<iostream>
#include<stdlib.h>
#include<fstream>
#include<time.h>
#include<vector>

using namespace std;

// main() Function implementation
int main()
{
    string one_line;
    vector<string> lines;

    srand(time(0));

    //input file which is in same folder where my program is.

    ifstream file("things.txt"); 

    //counting number of total lines in the file 
    // store the lines in the string vector
    int num_of_lines=0;
    
    while(getline(file,one_line))
    {
       num_of_lines++;
    lines.push_back(one_line);
  }

    //generate a random number between 0 and will count of total lines
    int rand_num=rand()%num_of_lines;

    //fetching a random things-to-do for you!!

    cout<<lines[rand_num]<<endl;

    return 0;
}

And here is my input file things.txt

Brush 
Mirzapur 2
Office work
Shooping
Mobile repair

Output : - I have 2 sample output which gave me both time different things-to-do .

Second Output : -

Explanation :-

1. First you need to save input file which will have 5 things-to-do in same program directory otherwise you can mention full directory where your file is.

2. I used ifstream to read that file.

3. I used srand() function with ‘time(NULL)’ that will give random one things-to-do.

4. I used getline() function to count total number of lines in file.

5.I used push_back() funtion to store lines in string vector.

6. Now generate a random line.

7. Finally I fetched the line index and matched with rand() function to give output .

I have explained everything and still you have any doubt then feel free to ask in comment.

Please upvote or hit that like,thumbs-up button, it really motivates me<3

Thank you!!!

Have a nice day:)


Related Solutions

C program! Create a list of 5 things-to-do when you are bored (in the text file...
C program! Create a list of 5 things-to-do when you are bored (in the text file things.txt, one line each), where each thing is described as a string of characters of length in between 10 and 50. Design a C program to read these things in (from stdin or by input redirection) and store them in the least memory possible (i.e., only the last byte in the storage for each string can be the null character). After reading things in,...
c++   In this lab you will create a program to make a list of conference sessions...
c++   In this lab you will create a program to make a list of conference sessions you want to attend. (List can be of anything...) You can hard code 10 Sessions in the beginning of your main program. For example, I have session UK1("Descaling agile",    "Gojko Adzic",       60);        session UK2("Theory of constraints", "Pawel Kaminski", 90); and then:        List l;        l.add(&UK1);        l.add(&UK2); Your Session struct should have the following member data: The Title The Speaker The session...
C++ Create a program that use the linkedbag 3. Develop a program to maintain a list...
C++ Create a program that use the linkedbag 3. Develop a program to maintain a list of homework assignments. When an assignment is assigned, add it to the list, and when it is completed, remove it. You should keep track of the due date. Your program should provide the following services: • Add a new assignment. • Remove an assignment. • Provide a list of the assignments in the order they were assigned. • Find the assignment(s) with the earliest...
create a new business~ t alk about it list of things need to do
create a new business~ t alk about it list of things need to do
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
List and describe 5 things that management of a company can do to increase their profit...
List and describe 5 things that management of a company can do to increase their profit margin?
List and describe 5 things that company management can do to control and or reduce their...
List and describe 5 things that company management can do to control and or reduce their operating margins?
Create a c++ program that: Create an input file using notepad ( .txt ). When testing...
Create a c++ program that: Create an input file using notepad ( .txt ). When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File” marker. Input date...
Write a  program in c++ using a map to create the following output. Here is the list...
Write a  program in c++ using a map to create the following output. Here is the list of students: 100: Tom Lee 101: Joe Jones 102: Kim Adams 103: Bob Thomas 104: Linda Lee Enter a student an ID to get a student's name: ID:  103 students[103] - Bob Thomas # of students: 5 Delete a student (Y or N)?  Y Enter ID of student to be deleted:  103 Here is the list of students after the delete: 100: Tom Lee 101: Joe Jones...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT