Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

c++ - Print out the input into an array by calling the function but its not working

i know this is a simple problem solving but i'm still cannot figure it out. i'm trying to print out the input into an array by calling the function but its not working. Help me. below down here is the code. main.cpp and groovy.h. for your information, i am new to programming

Here is the output that i get

enter image description here

//main.cpp

#include <iostream>
#include "groovy.h"
#include <string>
#include <iomanip>
using namespace std;

void displayInventory(const groovy[], int);

int main() {
  int s;

  const int size = 20;
  groovy car[size];
  /*= { 
    groovy("M01", "Mazda CX5", 132403.00),
    groovy("M02", "Mazda CX3", 126829.00),
    groovy("M03", "Mazda 6 Grand Touring", 208408.00),
    groovy("M04", "Mazda CX8", 173038.00),
  };

  cout<<"
List of available car : 
"<<endl;
  displayInventory(car, size);
*/
  string code, model;
  double price;

  cout<<"
Please enter model details :- "<<endl;
  cout<<"Model code : ";
  cin>>code;
  cout<<"Model name : ";
  cin>>model;
  cout<<"Model price : ";
  cin>>price;
  
  car[size].storeInfo(code, model, price);
  displayInventory(car, size);

  return 0;
}

void displayInventory(const groovy object[], int size){
  for (int i = 0; i < size; i++){
    cout<<setw(5)<<left<<object[i].getCode()
        <<setw(28)<<left<<object[i].getModel()
        <<"RM "<<right<<object[i].getPrice()<<endl;
  }
}

//groovy.h

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class groovy{
  private:
    string modelCode;
    string model;
    double price;
  
  public:
    groovy(){
      modelCode = "XXX";
      model = " ";
      price = 0.0;
    }
    groovy(string c,string m, double p){
      modelCode = c;
      model = m;
      price = p;
    }
    void storeInfo(string c,string m, double p) {
      modelCode = c;
      model = m;
      price = p;
    }
    string getCode() const {
      string code = modelCode;
      return code;
    }
    string getModel() const {
      string m = model;
      return m;
    }
    double getPrice() const {
      double p = price;
      return price;
    }
};
question from:https://stackoverflow.com/questions/65649720/print-out-the-input-into-an-array-by-calling-the-function-but-its-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  car[size].storeInfo(code, model, price);

is bad because the array car has only size elements: car[0] to car[size-1].

The index must be not less than 0 and less than size.

Try

  car[0].storeInfo(code, model, price);

instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...