结构、数组、函数

Structures, arrays, functions

本文关键字:函数 数组 结构      更新时间:2023-10-16

这里的新手,在将我的结构化数组参数发送到函数中时遇到一些困难。 看来我的基本流血编译器不喜欢我设置的函数。 我对整个程序进行了一些改进,但已经为此苦苦挣扎了几个小时。 我尝试查看此站点上的类似程序,但没有类似的东西足以让我指出错误。 我的库存函数的工作是发送一条消息,说当可乐等饮料 = 0 时售罄。任何帮助将不胜感激。 我已经将函数定义从 int main 之后的底部移动到顶部以清除不同的编译器错误。 我对该计划任何部分的所有反馈持开放态度,因为它是课堂作业,可以使用指针在下一次测试中取得成功。谢谢

   #include<iostream>
#include<iomanip>
using namespace std;
struct Soda
{
   string name;
   float  price;
    int    inv;
};
void functInventory(Soda[],int);       //prototype
void functInventory(Soda drink[],int num)       //function definition
{
      if ( drink[num].inv = 0)
      cout << "SOLD OUT" <<endl;
}
int main()
{
    const int option = 5;
    string cola, rbeer, lemlime, grape, cream;
    int InsAmount, choice;
    int income = 0;
    Soda array[option] =    {
                            {cola,   .75, 20},
                            {rbeer,  .75, 20},
                            {lemlime,.75, 20},
                            {grape, .80,   20},
                            {cream, .80,   20}
                          }; 
     cout << "Please choose 1-6 " << endl;
     cout    << " 1. Cola = $.75 " << endl;
     cout  << " 2. Root Beer = $.75 "  << endl;
     cout << " 3. Lemon Lime = $.75 " << endl;
     cout  << " 4. Grape Soda = $.80 " << endl;
     cout << " 5. Cream Soda = $.80 " << endl;
     cout << " 6. QUIT & EXIT "       << endl;
  switch(choice)
     {
       case 1:   array[0].inv - 1 = array[0].inv;
                  income = income + .75;
                  functInventory(array, choice);
                  break;
       case 2:    array[1].inv - 1 = array[1].inv;
                  income = income + .75;
                  functInventory(array, choice);
                  break;
       case 3:    array[2].inv - 1 = array[2].inv;
                  income = income + .75;
                  functInventory(array, choice);
                  break;
       case 4:    array[3].inv - 1 = array[3].inv;
                  income = income + .80;
                  functInventory(array, choice);
                  break;
       case 5:    array[4].inv - 1 = array[4].inv;
                  functInventory(array, choice)
                  income = income + .80;
                  break;
       case 6:  cout << "The total amount earned is: $" << income <<endl;
                  break;
        default:
                cout <<  "Please enter a capital letter from 1-6 " << endl;
   }
     cout << "Please Tell Me How Much Money You Are Inserting:  " <<endl;
     cin  >> InsAmount;
     change = InsAmount - .75;        //I will work on creating a more accurate function 4 change
     cout << "your change is " << change; << "  "<< endl;
    return 0;
     }

你说流血事件?我认为这是基于 gcc:打开警告,例如,-W -Wall 和可能的其他警告!这会告诉你这可能不是你想做的:

if ( drink[num].inv = 0)

这是一个作业,而不是比较!你可能的意思是

if (drink[num].inv == 0)

。或者,为了防止意外=弄乱您的逻辑

if (0 == drink[num].inv)

。因为如果您尝试使用赋值,这会导致编译器错误。我没有检查其他错误,但这似乎是一个明显的错误。由于这是我最近的习惯,我还要指出你不应该使用std::endl

在代码中再往下看:这甚至不应该编译:

array[0].inv - 1 = array[0].inv;

表达式 array[0].inv - 1 的结果是一个临时的内置类型,我认为你不能分配给这个类型(当我尝试编译代码时它肯定会失败;当我尝试编译它时,它会很好地发出关于我上面提到的问题的警告)。

您从编译器获得的错误可能如下所示:

test2.cpp:46:46:46:错误:左键需要作为赋值的左操作数

在 switch 语句中,赋值语句(每种情况下的第一行)的值位于等号的左侧而不是右侧。 这有点像在说:

int my_age;
26 = my_age;

这是行不通的。 变量在这里是你的值。