How would I write this program?

Here is what the program needs to do:

Write a program which allows the user to read a list of numbers from a data file. If the user specifies a data file which does not currently exist, then the program will create it. Assume there will never be more than 50 numbers in the data file.
The program should present the user with a menu containing the following options:
Current data file being processed.
(1) Select / create data file (.txt file extension will be added automatically)
(2) Display all the numbers in the data file, along with their total and average
(3) Display all the numbers in the data file sorted from smallest to largest
(4) Search for a number in the data file and display how many times the number occurs in the file
(5) Display the largest number in the data file
(6) Append one or more random numbers to the data file
(7) Exit the program
Prompt the user to make a selection from the menu and use input validation to ensure the user makes a valid selection (1 - 7).
Use a switch statement to process each of the above options. Each case statement should call a function which carries out the required action.
When asking the user which data file they wish to process, ask the user to only enter the part of the filename preceding the .txt extension and automatically add the .txt extension for the user. Create an empty data file if the file does not already exist.
Options (2) - (6) should test whether a data file has been specified before calling the function. If a data file has not yet been specified, the option should not call the function and the display should be refreshed.
When a data file is first created it will be empty. The functions which process the data file will need to handle the special case when the file exists, but does not contain any numbers. To make the program easier to write, you may opt to ignore this special case and assume that the user will immediately add numbers to the file once it has been created.
When adding a number, append it to the end of the data file.

How would I do this? What I am stuck on is how do I generate the 50 random numbers? And how do I add, average, etc with them?

If someone could at least show me how to write the 1st part (creating a file and generating 50 random numbers) I think I will get it after that.
how do I generate the 50 random numbers?
1
2
3
4
5
6
7
8
#include <cstdlib>
#include <ctime>
/*...*/
std::vector<int> values;
unsigned count =0;
while(count < 50) {
    values.push_back(std::rand % 100);//will generate numbers in range [0;99]
}
Then how would I write the next parts, (lets go with 2 and 3)?
Someone please help me with this program. I am very lost and dont really know how to do this
Anyone? I really need help with this, have never felt so lost before
Ok im all set with #2, how would I write #3?
No one is going to make this program for you, you'll have to actually try to do it yourself. If you get stuck on something specific, such as "how do I sort a vector of numbers?", then ask it here. But don't ask "How do I do this entire thing?". Google is your friend, and so is the Reference section of this website. Start writing the code that you know how to do, then try to write the code that you aren't so sure on. When you encounter a problem, Google it and check the Reference section. If you can't figure it out, then ask here.

http://www.cplusplus.com/reference/
> If someone could at least show me how to write the 1st part
> (creating a file and generating 50 random numbers) I think I will get it after that.

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
#include <cstdlib>
#include <ctime>

int main()
{
    std::string file_mame ;
    std::srand( std::time(nullptr) ) ; // seed the lcg
    // See: http://en.cppreference.com/w/cpp/numeric/random/srand

    // (1) Select / create data file (.txt file extension will be added automatically)
    {
        // get the file name
        std::cout << "file name without extension: " ;
        std::cin >> file_mame ;
        file_mame += ".txt" ; // add extension

        bool exists = std::ifstream(file_mame) ; // check if the file exists
        // by checking if the stream is in a good state
        // See: http://www.mochima.com/tutorials/fileIO.html

        if(exists) std::cout << "the file " << file_mame << " exists\n" ;
        else // if not, create an empty file
        {
            if( std::ofstream(file_mame) ) std::cout << "created file " << file_mame << '\n' ;
            else std::cout << "file " << file_mame << " could not be created\n" ;
        }
    }

    // (6) Append one or more random numbers to the data file
    {
        std::ofstream file( file_mame, std::ios::app ) ; // open file for appending

        if( !file ) std::cout << "file " << file_mame << " could not be opened\n" ;
        else // append 50 pseudo random numbers to the file
            for( int i = 0 ; i < 50 ; ++i ) file << std::rand() << '\n' ;
    }
}


Study: http://www.mochima.com/tutorials/fileIO.html

And then: std::vector<> http://www.mochima.com/tutorials/vectors.html

Once you have done that, you would be able to writ the rest of the program on your own.
How do I sort the numbers from smallest to largest?
How do I sort the numbers from smallest to largest?
http://en.cppreference.com/w/cpp/algorithm/sort
Ok I have worked all day on this program, and have gotten all steps done except for 4 and 6. Anyone know how to do those??
I'm down to my last one, stuck on #4. Could someone explain to me how to search for a number in an array
display how many times the number occurs
http://en.cppreference.com/w/cpp/algorithm/count
Last edited on
Why don't you show what you can do first and then come here to ask questions.
Topic archived. No new replies allowed.