c++ - 结构、数组、函数

标签 c++ arrays function structure

这里是新手,在将我的结构化数组参数发送到函数中时遇到了一些困难。看来我的基本流血编译器不喜欢我设置的功能。我对整个程序进行了一些改进,但为此苦苦挣扎了几个小时。我尝试查看此站点上的可比较程序,但没有足够相似的内容让我查明错误。我的库存功能的工作是在可乐 = 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)

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

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

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

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

关于c++ - 结构、数组、函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13436657/

相关文章:

c++ - 访问 map 中的第二个元素,即对象指针

javascript - 数组过滤器和数组显示

javascript - 拉维 |如何在 JavaScript 中访问 session 数组?

c++ - i2c smbus* 函数在 centos 7 i2c-dev.h 上不存在

c++ - QT - 不同用途的最佳绘画 "canvas"

php - Json编码获取重复数据

javascript - 有什么方法可以判断 javascript 函数是否已返回?

c++ - 那里有 "function size profiler"吗?

javascript - 我怎样才能像 jQuery 一样在函数内部做变量对象?

c++ - QFileDialog - 创建没有扩展名的文件