Pergunta

I need to include 4 Functions WITH arguments in this code but I cant for the life of me think of a way to incorporate any that have an argument. Aren't function arguments usually used with integers for calculations? What are some examples of functions i could create for this code? If this is easy, excuse me for I am fairly new.

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

ofstream outFile;
ifstream inFile;

const int MAXCHAR = 101;
const int MAXLINE = 256;



struct task
{
    char course[MAXCHAR];
    char desc[MAXCHAR];
    char date[MAXCHAR];
};

int main()
{

    task track[MAXLINE];
    bool quit = false;
    while (quit == false)
    {

        char choice;
        cout << "Welcome to my Task List: \n";
        cout << "<a> to add task\n";
        cout << "<s> to show the task list\n";
        cout << "<f> to find a task by course name\n";
        cout << "<q> to quit\n";
        cin >> choice;
        cin.ignore(100, '\n');

        if (choice == 'a' || choice == 'A')
        {
            int count = 0;
            outFile.open("tasks.txt", fstream::app);

            cout << "Enter Course Name (less than 101 characters): ";
            cin.get(track[count].course, MAXCHAR, '\n');
            cin.clear();
            cin.ignore(100, '\n');

            cout << "Enter Task Description (less than 101 characters): ";
            cin.get(track[count].desc, MAXCHAR, '\n');
            cin.clear();
            cin.ignore(100, '\n');

            cout << "Enter due date (mm/dd/yyyy): ";
            cin.get(track[count].date, MAXCHAR, '\n');
            cin.clear();
            cin.ignore(100, '\n');

            char confirm;
            cout << "\nAre you sure you want to add " << track[count].course << ";" << track[count].desc << ";" 
                 << track[count].date << "? (y/n)";
            cin >> confirm;
            if (confirm == 'y' || confirm == 'Y')
            {
                cin.clear();
                cin.ignore(100, '\n');
                outFile << track[count].course << ";" << track[count].desc << ";" << track[count].date << "\n";
                cout << "Task has been added\n";
                count++;
            }
            else if (confirm == 'n' || confirm == 'N')
            {
                cin.clear();
                cin.ignore(100, '\n');
            }
            outFile.close();
        }
        else if (choice == 's' || choice == 'S')
        {
            int count = 0;
            inFile.open("tasks.txt");

            while (inFile)
            {
                inFile.getline(track[count].course, MAXLINE, ';');
                inFile.getline(track[count].desc, MAXLINE, ';');
                inFile.getline(track[count].date, MAXLINE, '\n');
                if (inFile)
                {
                    cout << track[count].course << ";" << track[count].desc << ";" 
                         << track[count].date << "\n";
                    count++;
                }
            }

            inFile.close();
            cin.clear();

        }

        else if (choice == 'f' || choice == 'F')
        {
        int count = 0;
            char course[MAXCHAR];
            cout << "Enter Course Name: ";
            cin >> course;

            inFile.open("tasks.txt");
            while (inFile)
            {
                inFile.getline(track[count].course, MAXLINE, ';');
                inFile.getline(track[count].desc, MAXLINE, ';');
                inFile.getline(track[count].date, MAXLINE, '\n');
                if (strcmp(track[count].course, course) == 0)
                {
                    cout << track[count].course <<  ";" << track[count].desc 
                         << track[count].date << "\n";

                    count++;
                }


            }
            inFile.close();

        }
        else if (choice == 'q' || choice == 'Q')
        {
            quit = true;
        }

    }

}
Foi útil?

Solução

The cases you have for 'a' and 'A', 's' and 'S', 'f' and 'F' do not have much in common. So I do not think you can write one function to incorporate all of their functionality within one function, then based on a parameter do an action. You can, however, make your code more readable by enclosing all of the 3 different cases in one function that does not take a parameter and return void:

void Case_s_S()
{
    int count = 0;
    inFile.open("tasks.txt");

    while (inFile)
    {
        inFile.getline(track[count].course, MAXLINE, ';');
        inFile.getline(track[count].desc, MAXLINE, ';');
        inFile.getline(track[count].date, MAXLINE, '\n');
        if (inFile)
        {
            cout << track[count].course << ";" << track[count].desc << ";" 
                 << track[count].date << "\n";
            count++;
        }
    }

    inFile.close();
    cin.clear();
}

One more suggestion I can give you, so you keep your "main" more neat, is to make use of function pointers. You can lookup std::function which is defined in . What you can do is have a map of chars and std::functions, which will look like std::map>. Then you can assign keys of 'a' and 'A' to point to value of function Case_a_A and so on and so forth. That way when the user presses the key, you just go to that key and call its value, which will be the function.

Outras dicas

The point of methods is in a lot of ways to segment your code so that it's easier to read. Consider the parts of your code that could be self contained, and then separate that into functions of their own.

One basic example with your code might be:

if (choice == 'a' || choice == 'A')
   DoStuff_a(<params>);
else if (choice == 's' || choice == 'S')
   DoStuff_s(<params>);    

        //...

else if (choice == 'f' || choice == 'F')
   DoStuff_f(<params>);  
else if (choice == 'q' || choice == 'Q')
   quit = true;

There are many ways to segregate your code (e.g., GetInput(), GetFileData(), etc.) - a pretty decent discussion on this can be found here. Just remember that function design is (a) largely up to personal preference, and (b) should be considered even for main()!

Many developers firmly believe that main() should be 95%+ function calls, and very little "worker-code".

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top