hi.

i am working on an assignment where i have a file that has a list of numbers. using those numbers, i should be able to create a program, where when prompted, i can find the mean of the numbers from that file, the biggest numbers, etc.

the output would be like this:

Make a selection from the list

1. Get the largest value

2. Get the smallest value

3. Get the sum of the values

4. Get the average

5. Get the number of values entered

6. End this program

and then i could get that value from the file.

what i want to know, is how is a file attached to my sources for the programming and how i can get started on it. my book doesnt cover the topics much, nor does it cover what a menu-driven program is. (which i assume is this)

Dani AI

Generated

A short, practical guide for that builds on what and said.

The numbers file is a runtime input — you don't "attach" it to the source. Create a plain-text file (numbers separated by spaces or newlines), place it where your program runs (the executable/working directory) or give its full path, or prompt the user for the filename. If an IDE run can't find the file, either copy it to the executable folder or run the program from the folder that contains the file. A menu-driven program is just a loop that presents choices; implement the choices with a switch or if/else.

A compact example that reads all numbers into a vector and then provides the menu:

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iomanip>

int main() {
    std::string fname;
    std::cout << "Enter filename: ";
    if(!std::getline(std::cin, fname)) return 0;
    std::ifstream in(fname);
    if(!in) { std::cerr << "Cannot open file\n"; return 1; }

    std::vector<double> v;
    double x;
    while(in >> x) v.push_back(x);
    if(v.empty()) { std::cout << "No numbers found\n"; return 0; }

    for(;;) {
        std::cout << "1) largest  2) smallest  3) sum  4) average  5) count  6) exit\nChoice: ";
        int c; if(!(std::cin >> c)) break;
        switch(c) {
            case 1: std::cout << *std::max_element(v.begin(), v.end()) << "\n"; break;
            case 2: std::cout << *std::min_element(v.begin(), v.end()) << "\n"; break;
            case 3: std::cout << std::accumulate(v.begin(), v.end(), 0.0) << "\n"; break;
            case 4: std::cout << std::fixed << std::setprecision(4)
                              << (std::accumulate(v.begin(), v.end(), 0.0) / v.size()) << "\n"; break;
            case 5: std::cout << v.size() << "\n"; break;
            case 6: return 0;
            default: std::cout << "Invalid\n";
        }
    }
}

Troubleshooting and tips: always check that the file opened successfully; protect against division by zero for average; if the file may contain non-numeric text, read lines and parse with std::stringstream; if the file is huge, compute sum/count/min/max in one pass without storing every value; if numbers use commas (1,234) or commas between values, pre-clean the text or parse carefully. Running the program with input redirection (myprog < numbers.txt) is a quick way to test reading from stdin instead of a named file.

Recommended Answers

All 2 Replies

A menu drive program frequently has a list of options for the user to choose from, typically by having the user enter a letter or a number to correspond to the activity in the list they want to do. The user's input is then frequently used in a switch statement to have that task performed.

In C++ files are routinely handled using stream objects. An ifstream object can be used to read from a file. An ofstream object can be used to write to a file. An fstream object can be used to write or read to a file, depending on what mode it is in. You delcare a stream oject of the type desired. You can either pass a C style string (containing the desired file name) to the stream constructor or to the open() stream method to associate the desire file with the stream. Once the file is associated with the stream you can manipulate the material put into or taken out of the file using the same protocols you use with cin or cout (there are a few tricks to position the read/write location if you don't want/need to use the defaults, but that you can learn about later).

In other words, look at the chapter on Input and Output

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.