0

I'm sort of new to c++, I've taken a class over the summer and have gotten to the point of learning about pointers and how to use them. Right now, I'm trying to figure out how to take user input into an array in one Class Function, and then take that and use it in another class function. I'm not worried about anything else in the code right now.

I've tried using seperate variables then setting them equal to i, j, and using various loops to solve the problem but nothing is working so far. The problem is getting the input from opOne into opThree. My entire code is below. Apologies in advance for unused variables, I've yet to clean the code up to look pretty.

using namespace std;

class TravelLog {
private:
    double _elapsedTime[1000] = { 0 };
    double _speed[1000] = { 0 };

public:

    //variables
    const double speed = 30;
    double speedInput[30] = { 0 };

    const double time = 30;
    double timeInput[30] = { 0 };

    int inputSpeed;
    int inputTime;

    double miles;
    int index = 1;

    //functions
    void menu();

    int opOne(double speed, double time);
    int opTwo(double miles);
    int opThree(double speed, double time);
    void opFour();

};

//Sets the template for me to use without modifying the actual template
TravelLog A;

//Function for option 1 to take in speed and time
//leave this alone for right now
int TravelLog::opOne(double speed, double time)
{
    //Takes in speed input
    cout << "Speed: ";
    cin >> A.inputSpeed;

    //for loop to take in the time input and store it into an array
    //Takes in speed input
    cout << "Time: ";
    cin >> A.inputTime;

    A.index++;
    return 0;
}


//function for option 2 to take in ammount of miles
int TravelLog::opTwo(double miles)
{
    return 0;
}


//funcion for option 3 to print the records
int TravelLog::opThree(double speed, double time)
{
    //for loop to take in the speed input stored in the array and print it out
    for (int i = 0; i < A.index; i++)
    {
        A.speedInput[i] = inputSpeed;

        cout << "Speed: ";
        cout << A.speedInput[i] << endl;    
    }

    //for loop to take in the time input stored in the array and print it out
    for (int i = 0; i < A.index; i++)
    {
        A.timeInput[i] = A.inputTime;

        cout << "Time: ";
        cout << A.timeInput[i] << endl;
    }

    cout << endl;
    return 0;
}


//This function works: No Need to Edit
void TravelLog::opFour()
{
    cout << "Exiting the program. TravelLog Deleted.";
}


//This function works: No need to Edit
void TravelLog::menu()
{
    double input;

    cout << "1. Input speed and total time elapsed." << endl;
    cout << "2. Total miles" << endl;
    cout << "3. Records." << endl;
    cout << "4. Exit." << endl;

    cout << "Your input: ";
    cin >> input;

    if (input == 1)
    {
        TravelLog a;
        a.opOne(0, 0);
    }
    else if (input == 2)
    {
        TravelLog b;
        b.opTwo(0);
    }
    else if (input == 3)
    {
        TravelLog c;
        c.opThree(0, 0);
    }
    else if (input == 4)
    {
        TravelLog d;
        d.opFour();
        exit;
    }

    return;
}


int main()
{
    cout << "Please select:" << endl;

    // (^_^) Please write your codes here //
    for (int b = 0; b < 1000; b++)
    {
        TravelLog e;
        e.menu();
    }

    return 0;
}```
  • 3
    Which C++ textbook are you using to learn C++? If you're not using a C++ textbook, you need to get one. C++ is just too complicated to be learned by trial and error. The shown code exhibits multiple fundamental misunderstandings of how classes work in C++, and the proper way to use them. Unfortunately, Stackoverflow is not a replacement for a C++ textbook, and we are not a tutorial site, we only answer ***specific*** programming questions. It's just not possible to fully explain the fundamental issues with what the shown code is trying to do, in or two paragraphs here. You need a textbook. – Sam Varshavchik Sep 23 '22 at 01:52
  • We're learning mostly from the professor, with very few supplimental articles he hands out to us. They're from various sources. – Awkward_Snacks Sep 23 '22 at 03:05
  • I suppose that it's possible that you have an incompetent C++ instructor, that's always a possibility. That's unfortunate, but doesn't really change the fact that nothing short of a tutorial and full explanation of how class methods work, and it even means to have a class method, would help in fixing the shown code. Perhaps you want to take some initiative on your own, and [get a good C++ textbook](https://stackoverflow.com/questions/388242/). – Sam Varshavchik Sep 23 '22 at 11:00

0 Answers0