Thecube
Thecube

Reputation: 71

Calling a class function from main with parameters

I am trying to pass some variables from my main function into a class that I have made which will later let me store the inputs inn a file. I want to be able to have multiple different objects stored, up to around 20.

int main(){

int a[20];
float b[20];
string c[20];
string d[20];
string name[20];
char contin;
menu MyMenu;

for(int i = 0; i < 20; i++){
    cout << "Please enter the name of the item: ";
    cin >> name[i];
    cout << "\nPlease enter a value: ";
    cin >> a[i];
    cout << "\nPlease enter a value: ";
    cin >> b[i];
    cout << "\nPlease enter a phrase: ";
    cin >> c[i];
    cout << "\nPlease enter a a phrase: ";
    cin >> d[i];
    cout << "\n\nWould you like to go through the list again?(y/n): ";
    cin >> contin;
    cout << "\n";
    if(contin == 'N' || 'n'){
        break;
    };
};
void MyMenu.storeitem(a[20], b[20], c[20], d[20]);`};`

This is currently my main function which has a loop and then stores the inputs into an array.

The line that I having trouble with is last line where i am trying to pass it all to the class function.

Below is the class

class menu{
    public:
    void storeitem(int a[20], float b[20], string c[20], string d[20]);
};
void menu::storeitem(int a[20], float b[20], string c[20], string d[20]){

    int storeb[20];
    int storea[20];
    string storec[20];
    string stored[20];

    storeb[20] = b[20];
    storea[20] = a[20];
    storec[20] = c[20];
    stored[20] = d[20];
};

I think the problem is in the last line of the main function where I am trying to call the class function.

Upvotes: 1

Views: 110

Answers (2)

user5843215
user5843215

Reputation:

You don't need to pass the size of the arrays as parameters in the storeitem function. However you should add a count this is represented by int i, as the last parameter.

void menu::storeitem(int a[], float b[], string c[], string d[], int i){

add the i++; to the inside at the bottom of your loop this will keep how many full times they went through and entered data. you now also have to pass i as an argument when you are calling your function.

cin >> contin;
    i++;
    if((contin == 'n') || contin == 'N'){
            break;
    }
}
MyMenu.storeitem(a, b, c, d, i);

What Are You Trying To Accomplish here? Are you trying to store the contents that we put in array b into storeb? All these statements are saying make the 20th element in stroreb, storea, etc... equal to whatever the 20th element of the parameters we passed into the function. Some more information would be greatly appreciated.

storeb[20] = b[20];
storea[20] = a[20];
storec[20] = c[20];
stored[20] = d[20];
};

Upvotes: 0

MikeCAT
MikeCAT

Reputation: 75062

  • You won't need void or ` to call functions.
  • storeb[20] = b[20]; and such things are not how to copy arrays but nonsense with out-of-range access.
  • Do not do out-of-range access and match the type of expected arguments and what is passed.
  • Using magic numbers is not good because it raises risk of making typo or forgettng to change some of the values.

Try this:

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::string;

const int MAX = 20;

class menu{
    public:
    void storeitem(int a[MAX], float b[MAX], string c[MAX], string d[MAX]);
};
void menu::storeitem(int a[MAX], float b[MAX], string c[MAX], string d[MAX]){

    int storeb[MAX];
    int storea[MAX];
    string storec[MAX];
    string stored[MAX];

    for(int i = 0; i < MAX; i++){
        storeb[i] = b[i];
        storea[i] = a[i];
        storec[i] = c[i];
        stored[i] = d[i];
    }

    // do something with what is stored
}

int main(){

    int a[MAX] = {0};
    float b[MAX] = {0};
    string c[MAX];
    string d[MAX];
    string name[MAX];
    char contin;
    menu MyMenu;

    for(int i = 0; i < MAX; i++){
        cout << "Please enter the name of the item: ";
        cin >> name[i];
        cout << "\nPlease enter a value: ";
        cin >> a[i];
        cout << "\nPlease enter a value: ";
        cin >> b[i];
        cout << "\nPlease enter a phrase: ";
        cin >> c[i];
        cout << "\nPlease enter a a phrase: ";
        cin >> d[i];
        cout << "\n\nWould you like to go through the list again?(y/n): ";
        cin >> contin;
        cout << "\n";
        if(contin == 'N' || 'n'){
            break;
        }
    }
    MyMenu.storeitem(a, b, c, d);
}

Upvotes: 1

Related Questions