开发者

C++ Void Function Confusion

I'm working on a homework assignment and I'm confused about what the directions want me to write for these functions. The directions say:

Write a void function called GetSalesInfo that accepts the following as input from a user

  • Account Number
  • Date of Sale
  • County Code
  • Total Sale
  • Shipping Weight

This function should return all these inputs to the calling function - I'm most con开发者_开发技巧fused about this part

Write the following value returning functions:

  • CalcDiscount - Returns the appropriate discount for the sale

I don't understand what the first function is supposed to do, I have the parameters set up, but I don't know what to do in the actual function.

void GetSalesInfo(int accNum,       // Account Number - IN
            int month,          // Month of sale - IN
            int day,            // Day of sale - IN
            int year,           // Year of sale - IN
            char countyCode,    // County Code - IN
            float total,        // Total Sale Amount (Before Tax) - IN
            int weight)         // Shipping Weight - IN
{

}

Also, I don't know how to access all this info in the CalcDiscount function.

I'm not asking anyone to do my homework, I just need a little push in the right direction and my professor is never available to help anyone.


Well, it specifically calls for a void function which means it will return nothing via the return value.

C++ has reference types which augment the C way of doing it (passing in pointers to the variables then dereferencing them to change them outside the function):

void GetSalesInfo (int &accNum,
                   int &month,
                   int &day,
                   :
                   :

When you change these variables in the function, that will be reflected back into the variables you passed in.

So your function can simply input the data from the user and store them into those variables. Then, back in the calling function, you'll have them available to be passed to CalcDiscount, something like:

float CalcDiscount (int accNum,
                    int month,
                    int day,
                    :
                    :


I beleive that the first function shough use pass-by-reference. Place a & symbol after the datatype and any changes made to a variable will be made to the variable passed, not just a copy of it. Example:

#include <iostream>

void increment(int& num) {
    num += 1;
}

int main(void) {
    int num = 0;
    increment(num);
    std::cout << num << std::endl;
    return 0;
}


I agree, it is worded poorly. You obviously can't return multiple values from a function, so you have two practical choices.

  1. Return a data object that encapsulates all of those values (recommended, but may not be appropriate for this homework assignment.
  2. Take references to multiple output parameters and assign them within the function.

The function declaration just doesn't make sense as it is.

Also, I don't know how to access all this info in the CalcDiscount function.

It depends on where you need to call it. You will probably call it from within GetSalesInfo.


Sorry to say but i believe that your abstract that you made is wrong, your TEACHER wants the functionto accept those values i.e as in "cin" , thus you won't be passing those directly to the function.

The abstract would be rather like :

void GetSalesInfo()
{
  ////Code of the INPUT function here     
    * Account Number
    * Date of Sale
    * County Code
    * Total Sale
    * Shipping Weight    
}

void A()
{
  GetSalesInfo();

  // Here this GetSalesInfo would be responsible for acceting the values and returning it to the calling function "A"

  //The values would be returned with use of pointers only as your Teacher won't allow global variables :D

  //Rest of the code goes here
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜