在我的C 代码上寻找功能和数组的错误

Looking for help on errors on my C++ code for functions and arrays

本文关键字:数组 错误 功能 寻找 我的 代码      更新时间:2023-10-16

对于我在C 的项目中,我已经在这个项目上挣扎了几个星期,而且我觉得我只是让自己与添加了此代码中的内容相混淆了。谁能给我一些关于我做错了什么的指导?这是我现在遇到的错误:151 29 [error]无法转换'(fruitlist*)(& itemlist)'从'fruit -list*'到'std :: string {aka std :: basic_string}'152 35 [error]无法转换'(fruitlist*)(& tookslist)'从'fruit -list*'到'std :: string {aka std :: basic_string}'

>

谢谢!

我很抱歉,如果代码间距都弄乱了,这是我的第一篇文章,我一直在遇到有关代码的间隔方式的错误?对不起!

    #include <iostream> //allows data to output to the screen
    #include <string>
    #include <vector>
    using namespace std;
    //function prototypes
    void showFruitList (string);
    void getData(string, int);

    class Customer//create a class called customer
    {
    private://private section
    //string all private variables
    string firstName;
    string lastName;
    string streetAddress;
    string city;
    string state;
    string zipCode;
    public: //public sections
    void setFName (string fName)//public function to set first name
    {
        firstName = fName;
    }
    string getFName ()//public function to get first name
    {
        return firstName;
    }
    void setLName (string lName)//public function to set last name
    {
        lastName = lName;
    }
    string getLName ()//public function to get last name
    {
        return lastName;
    }
    void setStreetAddress (string sAddress)//public function to set street               `       address
    {
        streetAddress = sAddress;
    }
    string getStreetAddress()//public function to get street address
    {
        return streetAddress;
    }
    void setCity (string cty)//public function to set city
    {
        city = cty;
    }
    string getCity()//public function to get city
    {
        return city;
    }
    void setState (string sT)//public function to set state
    {
        state = sT;
    }
    string getState ()//public function to get state
    {
        return state;
    }
    void setZipCode (string zip)//public function to set zip code
    {
        zipCode = zip;
    }
    string getZipCode()//public function to get zip code
    {
        return zipCode;
    }
    };
         struct FruitList //good for outputting lists of things to be called    `           `in main function
        {
        string fruitItem; //string type
        float fruitCostPP; //double type since prices for in decimal form   `          `(fruit cost per pound)
         };
    //Establish customer registration and display a list of products
    int main()
    {
    //declare variables 
    const int SIZE = 80;// assign value of 80 to SIZE
    char firstName[ SIZE ];

    Customer customer1 = Customer();//pulls from class Customer
    string input;//welcome message and customer info input area
    cout << "Welcome to Dried Fruit Central n" <<endl;
    cout << "Please enter your first name: "<< endl;//prompts user to enter   `      `their first name
    getline(cin, input);//user enters their first name
    customer1.setFName(input);//pulls from class Customer
    cout << "nPlease enter your last name: " << endl; //promts user to  `                    `       `enter their last name
    getline(cin, input);//user enters their last name
    customer1.setLName(input);//pulls from class Customer
    cout << "nHello " << customer1.getFName() << "!"<< endl;// greeting to  `      `user using their first name
    cout << "nPlease enter your street address: "<< endl;//prompts user for   `       `their street address
    getline(cin, input);//user enters their street address
    customer1.setStreetAddress(input);//pulls from class Customer
    cout << "nPlease enter your city: "<< endl;//promts user to enter their   `       `city
    getline(cin, input);//user enters their city
    customer1.setCity(input);//pulls from class Customer
    cout << "nPlease enter your state abbreviation: "<< endl;// prompts `       `       `user to enter their state abbreviation
    getline(cin, input); //user enters their state abbreviation
    customer1.setState(input);//pulls from class Customer
    cout << "nPlease enter your zip code: "<< endl;//promts user to enter  `       `their zip code
    getline(cin, input);//user enters their zipcode
    customer1.setZipCode(input);//pulls from class Customer
    cout << "nThank you " << customer1.getFName() << ", you are now  `              `       `registered and can begin shopping with nDried Fruit Central! n"<<   `    `        endl;// lets customer know they are now registered and can begin  `    `        shopping
    cout << "nHere is a list of our dried fruit available for purchase and  `       `price per pound: n"<< endl;//OUtput to customers the list of dried  `    `       will be displayed as well as the price per pound
    FruitList itemsList[8]; // 8 items on the menu
    int itemNum = 0;//
    char inputItemNum;//customers input fruit item number
    vector<int> customerOrder;//customers order
    while (true)//begin while loop
    {
        float totalCost=0;//double due to decimal amounts
        getData(itemsList, 8); //pull from function getData
        showFruitList(itemsList, 8); // pull function showFruitList
        cout<<"Enter your orders. Press Enter after every input. Enter 0 to  `           `end"<<endl;//customer inputs item numbers 
        do
        {
            while((cin>>itemNum).fail() || itemNum < 0 || itemNum > `    `   `             `9)//while loop to prevent customer from inputting the wrong item  `             `number
            {
                cout<<"Enter correct item number : ";//error message
                cin.clear();
                cin.ignore();
            }
            customerOrder.push_back(itemNum - 1);
        }while(itemNum != 0);
        cout<<endl<<"You ordered"<<endl;//displays order total and items  `   `           purchased
        for(int i = 0; i < customerOrder.size() - 1; i++)
        {
            cout<<itemsList[customerOrder.at(i)].fruitItem<<"tt$ " `   `  `          `<<itemsList[customerOrder.at(i)].fruitCostPP<<endl;
            totalCost += itemsList[customerOrder.at(i)].fruitCostPP;//all  `   `           calculations
        }
         cout <<endl<<"Your order total is: $" << totalCost << endl <<  `    `           endl;//displays total cost
        cout << "Input 'n' for a new order or 'Q' to quit t";//allows users  `           `to start new order or quit this order
        cin >> inputItemNum;
        if (inputItemNum == 'q' || inputItemNum == 'Q')
        {
            return 0;
        }
        else while (inputItemNum != 'n')//else while loop for error  `  `   `           selection
        {
            cout << "Please make a valid selection.t";//output error for  `  `            valid selection to be entered
            cin >> inputItemNum;
        }
        cout << endl << endl;
       }
      //    system("pause");
       return 0;
       }

        void showFruitList(FruitList list[], int size) //calls in structure  `          `of fruit menu and price together to be displayed when called in main
        {
       cout << "Type the number of the items that you would like.""n";
       cout << "After each selection, press Enter." "n";
       cout << "To add multiple orders of the same fruit enter the same item    `         `number." "nnn" << endl;
       cout << "When you are done, type 0 and press Enter to get your order   `           `total." "nnn" << endl;
       for (int i = 0; i < size; i++) //goes with size of list 8, and adds a    `         `.) and $ in front of each item
       {
        cout << "tt" << i + 1 << ") " << list[i].fruitItem << "$ " <<    `    `           list[i].fruitCostPP << endl; //structure.   i+1 to start num at 1  `    `           rather than 0
        cout << "n";
        }
        return;
        }
       void getData(FruitList list[], int size) //two arrays that list the   `         `item and one for the price 0-8
       {
        list[0].fruitItem = "Apricots.tt"; //items array starts at 0.  `    `           output     at 1.
        list[0].fruitCostPP = 1.75;//price array starts at 0
        list[1].fruitItem = "Apples.tt";
         list[1].fruitCostPP = 1.25;
         list[2].fruitItem = "Bananas.tt";
         list[2].fruitCostPP = 1.25;
         list[3].fruitItem = "Cherries.tt";
         list[3].fruitCostPP = 2.25;
         list[4].fruitItem = "Cranberries.tt";
         list[4].fruitCostPP = 1.25;
         list[5].fruitItem = "Mangos.tt";
         list[5].fruitCostPP = 1.75;
         list[6].fruitItem = "Peaches.tt";
          list[6].fruitCostPP = 1.25;
          list[7].fruitItem = "Strawberries.t";
          list[7].fruitCostPP = 1.75;
          }

它与 function 原型无关,但是与 struction 定义。

如果您想真正使用一个结构,则必须完整定义。调用函数后,可以将函数定义(实现)放置,但是对于不可能的结构。

要声明编译器需要结构的完整定义的结构的实际变量,否则编译器不知道其包含什么成员或多大。

因此,要解决您的问题,没有简单的结构在顶部进行简单的前向声明,请将整个结构定义放在那里。