【问题标题】:Creating 2 arrays of 2 structs.创建 2 个 2 个结构的数组。
【发布时间】:2014-03-15 23:38:27
【问题描述】:

我被分配了一个问题域。编写一个程序,询问用户他想购买哪些水果以及数量。根据用户的输入,它产生一个账单。 交互示例: 你要买水果吗?是的

什么水果?橙色

多少磅? 2

您要购买其他水果吗?是的

什么水果?苹果

多少磅? 1.5

您要购买另一种水果吗?没有

感谢您的光临。

这是您的帐单:

每磅价格的水果数量价格

橙色 2 磅 1.5 3

苹果 1.5 磅 2 3

总金额 6

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

struct fruitData
{
    string fruitName;
    float price;
};

struct invoiceData
{
    char fruit_ordered;
    float price1;
    float quantity_ordered;
    float totalFruitprice;
} ;


int main()

    char choice;
    string fruit_choice;
    float pounds_choice;
    int count=0;

    fruitData fruits[5];
    invoiceData customers[5];

    strcpy(fruits[0].fruitName, "Banana");
    strcpy(fruits[1].fruitName, "Apple");
    strcpy(fruits[2].fruitName, "Pears");
    strcpy(fruits[3].fruitName, "Oranges");
    strcpy(fruits[4].fruitName, "Papaya");

    fruits[0].price=1;
    fruits[1].price=2;
    fruits[2].price=2.5;
    fruits[3].price=1.5;
    fruits[5].price=1.40;

    strcpy(customers[0].fruitName, "Banana");
    strcpy(customers[1].fruitName, "Apple");
    strcpy(customers[2].fruitName, "Pears");
    strcpy(customers[3].fruitName, "Oranges");
    strcpy(customers[4].fruitName, "Papaya");

    customers[0].price=1;
    customers[1].price=2;
    customers[2].price=2.5;
    customers[3].price=1.5;
    customers[4].price=1.40;


    cout << "Welcome to the fruit market" << endl;
    cout << "Would you like to purchase fruit?" << endl;
    cin >> choice >> endl;

    while (choice == 'Y')
    {
    cout << "Which fruit?" << endl;
    cin >> fruit_choice >> endl;

    cout << "How many pounds?" << endl;
    cin >> pounds_choice >> endl;

    if (fruit_choice == "Banana")
    {
    customers[0].quantity_ordered = pounds_choice;
    customers[0].total_price = customers[0].quantity_ordered * customers[0].price;
    }
    else if (fruit_choice == "Apple")
    {
    customers[1].quantity_ordered = pounds_choice;
    customers[1].total_price = customers[1].quantity_ordered * customers[1].price;
    }
    else if (fruit_choice == "Pears")
    {
    customers[2].quantity_ordered = pounds_choice;
    customers[2].total_price = customers[2].quantity_ordered * customers[2].price;
    }
    else if (fruit_choice == "Oranges")
    {
    customers[3].quantity_ordered = pounds_choice;
    customers[3].total_price = customers[3].quantity_ordered * customers[3].price;
    }
    else (fruit_choice == "Papaya")
    {
    customers[4].quantity_ordered = pounds_choice;
    customers[4].total_price = customers[4].quantity_ordered * customers[4].price;
    }
    }

    for (count = 1 ; count <=5 ; count++)
    {
    if (customers[0].total_price != 0)
    {
        cout << customers[0].fruit_name <<
        cout << customers[0].quantity_ordered <<
        cout << customers[0].price <<
        cout << customers[0].total_price <<

    }
    }



}

我遇到了很多错误。其中一个是“string fruit_chioce”中的字符串并没有像它应该的那样变成蓝色。并且编译器说字符选择应该以“;”开头.我也无法为这些结构关联一个数组。任何帮助将不胜感激,谢谢。

【问题讨论】:

  • 对于初学者来说没有开放 { for main()。
  • fruits[5] 和 fruits[5] 一样。价格超出范围。
  • 您不能(轻松)混合 C++ 字符串和“C”字符串。坚持使用 C++。尝试fruits[0].fruitName = "Banana" 等。或者更好,将数组初始化为fruitData fruits[] = { "Banana", "Apple", ... };
  • 建议:重新检查所有代码。有很多问题不仅仅是你问的。未完待续。

标签: c++ arrays string struct


【解决方案1】:

最好的方法是使用std::vector。例如

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

struct Fruit {
    string name;
    float price;
};

int main() {
    vector<Fruit> fruits;

    while(true) {
        Fruit fruit;
        string answer;
        cout << "Which fruit? ";
        cin >> fruit.name;
        cout << "How many pounds? ";
        cin >> fruit.price;
        fruits.push_back(fruit);
        cout << "Do you want to purchase another fruit? ";
        cin >> answer;
        cout << endl;
        if (answer=="No") break;
    }

    for(int i=0; i<(int)fruits.size(); ++i) {
        cout << "Fruit " << i+1 << " -> Name: " << fruits[i].name << ", price: " << fruits[i].price << endl;
    }

    return 0;
}

【讨论】:

    【解决方案2】:
    1. int main() 之后应该紧跟一个{

    2. fruits 的长度只有 5 个元素,但您尝试在此处访问第六个元素:(我想它确实应该是 4 而不是 5

      fruits[5].price=1.40;
      
    3. fruitName 是一个 C++ 字符串,不能使用 strcpystrcpy 用于 C 字符串,即基于 char* 的字符串)。简单分配:

      fruits[0].fruitName = "Banana";
      
    4. 您的while 循环将永远不会结束,因为您再也不会要求输入choice

    5. 你最后的else在这里

      else (fruit_choice == "Papaya")
      

      也应该是else if

    6. 你的 for 循环从零到五个(第六个元素),但数组只有 5 个元素,所以它应该是:

      for (count = 1 ; count < 5 ; count++)
      
    7. 最后的输出行应该以操作数(要输出的某个变量)而不是&lt;&lt; 结尾。也像其他所有声明一样,它们必须以 ; 结尾。例如换行:

      cout << customers[count].fruit_name << endl;
      

      我还将0 替换为count,因为您肯定不想总是输出第一个数组元素。

    【讨论】:

    • 查看我错过的一些内容的其他答案。
    • 感谢您的所有帮助。我已成功提交此实验室。
    【解决方案3】:

    在结构fruitData中

    struct fruitData
    {
        string fruitName;
        float price;
    };
    

    数据成员fruitName 被定义为具有类型std::string。您不能将标准 C 函数 strcpystd::string 类型的对象一起使用,因为它们不是字符数组。你可以使用一个简单的分配。例如

    fruits[0].fruitName = "Banana";
    

    但是最好在定义数组的时候初始化。例如

    fruitData fruits[5] = 
    {
        { "Banana", 1 }, { "Apple", 2 }, { "Pears", 2.5 }, 
        { "Oranges", 1.5 }, { "Papaya", 1.40 }
    };
    

    还在结构 invoiceData 中,您将数据成员 fruit_ordered 定义为

    char fruit_ordered;
    

    (一个非常奇怪的决定)它只能包含一个字符。此外,我认为您的意思是fruitName 而不是fruit_ordered,因为您在下面写道

    strcpy(customers[0].fruitName, "Banana");
    

    但是结构没有声明这个名字。因此,您可能不会像以前那样使用标准 C 函数 strcpy,因为 fruitName 不是字符数组。

    此外,您没有在声明 C 函数的地方包含标题 &lt;cstring&gt;

    本声明(及类似声明)

    cin >> choice >> endl;
    

    无效。您不能将std::endlstd::cin 一起使用

    也是这个循环

    for (count = 1 ; count <=5 ; count++)
    {
    if (customers[0].total_price != 0)
    {
        cout << customers[0].fruit_name <<
        cout << customers[0].quantity_ordered <<
        cout << customers[0].price <<
        cout << customers[0].total_price <<
    
    }
    }
    

    没有任何意义。

    我认为还有其他错误,但这已经足够了。考虑到具有 5 个元素的数组的有效索引范围是 0 - 4

    在这段代码中

    fruits[0].price=1;
    fruits[1].price=2;
    fruits[2].price=2.5;
    fruits[3].price=1.5;
    fruits[5].price=1.40;
    

    你有一个错字。这里的索引是 0, 1, 2, 3, 5 但必须是 0, 1, 2, 3, 4。

    简而言之,代码充满了错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 2022-01-10
      • 2018-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-21
      相关资源
      最近更新 更多