Hello i need help with a program i have to write for my class. I am stuck and still pretty new to coding. I would appreciate any and all help.
the assignment is this:
You are the game warden in your town and are responsible for stocking the local lake
(see Figure 1) prior to the opening of the fishing season. The average depth of the lake is
20 feet. Your plan is to stock the lake with 1 fish per 1000 cubic feet, and have
approximately 25% of the original fish population remaining at the end of the season.
What is the maximum number of fishing licenses that can be sold if the average catch is
20 fish per license?

The program criteria are:
1. Present the user with a short greeting describing the program.
2. Read the name of the data file from the command line in argv[1].
3. Open the file, read the data and save it in an array.
4. Compute the volume of the lake using Simpson’s rule and the other statistics
necessary to find the maximum number of licenses that can be sold.
5. Display the final answer with an annotation.

This is my main

#include <iostream>
#include <fstream>
#include <cstdlib>
#include "pa1functions.h"
#include "pa1functions.cpp"

int main(int argc, char* argv[])
{
    functions::greeting(); //Function to greet user and explain program

    functions::read_data();//Read data from file using argv[1]

    functions::get_data();//Open file, read and save file to array

    functions::compute();//Compute volume and licenses

    functions::answer();//Display annotated answer
//Not sure if calling the functions properly. I know the should be something in the ()
    return 0;
}

This is the function prototype

namespace functions
{
    void greeting();
    int read_data();
    int get_data();
    double area();
    double volume();
    void answer();
}

and these are the functions(where i am having the most trouble)

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include "pa1functions.h"

void functions::greeting()
{
    using std::cout;
    using std::cin;
    using std::endl;

    cout <<"Hello Game Master this program will compute the lake volume then\n";    cout <<"find how many fish to put in the lake and determine how many\n";
    cout <<"fishing licenses to sell to keep 25% of the fish population still\n";
    cout <<"in the lake.\n";
    cout <<"Press any key and ENTER to continue:";

    char x;
    cin >> x;

    cout << endl;
}

int functions::read_data()
{
    using std::cout;
    using std::cin;
    using std::cerr;
    using std::ifstream;

    const char* inFile = "lake.txt";

    cout << "Please enter the name of the file with the lake information\n";
    cout << "then press ENTER ->\n";
    //Could not get away for the user to imput the file name and have the code work.
    //so i just assigned inFile to the txt file i had.
    //and the txt file can be any 8 numbers i just used 2-16 seperated with spaces

    ifstream in(inFile);//opening the file for reading

    if(!in)
    {
        cerr << "Error, the file could not be opened!\n";
        return (-1);
    }


    int lake_data[8];
    while(!in.eof())
    {
        in.get
    }


}

i think the biggest spot i am stuck in is reading a file that the user imputs then saving it to an array to be used for simpsons rule. But i did switch up the code abit. Apparently using argv and argc is the best way to get a user input file but i have no idea how to use either.

void functions::read_data()//to make sure that the user imput file opened correctly
{
    using std::cout;
    using std::cin;
    using std::cerr;
    using std::ifstream;

    FILE *inFile;
    int i;

    cout << "Please enter the name of the file with the lake information\n";
    cout << "then press ENTER ->\n";
    ifstream infile(argv[1]);


    ifstream in(inFile);//opening the file for reading
    if(!in)
    {
        cerr << "Error, the file could not be opened!\n";
        exit (-1);
    }
}




    int functions::get_data()//To open user file and save data into an array
                             //to be used with simpsons rule
   {
        using std::cout;
        using std::cin;
        using std::cerr;
        using std::ifstream;

        ifstream infile(inFile);


    int i;
    int lake_data[i];

    while(!in.eof())
    {
        in.get(lake_data[i]);
    }
}

argv will only be available from your main and contains the command arguments you start your application with. Example: C:\>myProgram.exe input.txt

In this case argv[0] would be 'input.txt'

You can then pass this around your application to the code that needs it.

Otherwise, just use std::cin to fetch the user input when you ask for it.

PS. Remember, arrays are 0-based.

Edited 3 Years Ago by Ketsuekiame

This article has been dead for over six months. Start a new discussion instead.