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...
You are asked to create to to-do list program to store and manage to-do items for...
You are asked to create to to-do list program to store and manage to-do items for user. You need to define a class to-do item which holds the following attributes: • Id => number, generated by the program when item is created • Title => string • Description => string • Type (e.g. shopping, housing, work, etc.) => Enum • Priority => number between 1 to 5 • Status => (e.g. done, in progress, hold, etc.) => Enum • Create...
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
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...
c++ Create a program that creates a sorted list from a data file. The program will...
c++ Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name...
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 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT