0

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 confused 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.

Michael
  • 105
  • 1
  • 4
  • 14
  • That is ridiculous wording indeed. – hiddensunset4 Feb 15 '11 at 02:38
  • It seems like the first function should take a struct (or class) as a parameter, and then read user input from the keyboard, and fill the populate the fields of the struct/class passed in. CalcDiscount would take the same object, and return the discount (however it's been defined) – Rob Feb 15 '11 at 02:41

4 Answers4

4

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,
                    :
                    :
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • And, downvoted why exactly? There's no other way to do it other than with pointers or globals and, if you're suggesting that for C++, you probably shouldn't be here :-) – paxdiablo Feb 15 '11 at 02:40
  • Yeah, the problem is it doesn't make much sense that way. How are you supposed to return that data with no input parameters? You'll need some context in order to retrieve the information. I didn't downvote BTW – Ed S. Feb 15 '11 at 02:40
  • downvote was for giving it away when it's tagged as homework. 129k and still crying about points. That's sad. – Edward Strange Feb 15 '11 at 02:42
  • The context is in the question: "accepts the following as input from a user" – paxdiablo Feb 15 '11 at 02:43
  • 2
    @Crazy, I'm not crying about points, just trying to get the best answers, including fixing mine if you can show they're wrong. And I agree with you about providing full solutions for homework, I don't like SO being used as a cheap homework service. But telling someone how to use references is not doing their homework for them. I see you chose your moniker wisely, young padawan :-) – paxdiablo Feb 15 '11 at 02:45
  • Well, I should have clarified; it doesn't make sense in the manner that you have written it. You would probably pass in an acct. number, maybe the date of sale, and he output params would be price, whatever. – Ed S. Feb 15 '11 at 02:46
4

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;
}
Matt Eskridge
  • 1,019
  • 10
  • 24
  • Yeah, but that doesn't exactly solve my problem. I feel like GetSalesInfo is supposed to sort of store all the data that you get from the user in the main program so you only have to input everything once. Then you are supposed to be able to use that stored data in CalcDiscount to calculate the discount. I guess kind of like an object even though we don't know what those are yet. But it's been awhile since I learned all this stuff and I'm confused as to how I'm supposed to accomplish that. – Michael Feb 15 '11 at 02:54
  • I think your teacher wants you to define some variables in the main() function, input values for them, process the values, and then output them. You're having problems with the input part of that. What you need to do is define the variables in main(), pass them as reference variables to the function, and then, inside the function, use cin to fill the values up. After they are filled the values inside main()'s scope will automatically be filled with the values from the command prompt. – Matt Eskridge Feb 15 '11 at 03:01
  • @Alf P. Steinbach: void main() compiles in MS visual studios. – Matt Eskridge Feb 15 '11 at 03:01
  • 1
    @Michael, it definitely doesn't _exactly_ solve your problem. On the other hand, you _did_ state "I just need a little push in the right direction" - this and other answers here _are_ that push. Cosmic is showing you how to use reference types to allow a variable change to be reflected back to the caller. You just need to apply that newfound knowledge to your own case. – paxdiablo Feb 15 '11 at 03:02
  • @Cosmic: VLAs compile with g++. – Cheers and hth. - Alf Feb 15 '11 at 03:02
  • @Cosmic, hope you don't mind me fixing the code - MS isn't exactly known around the world for standards compliance :-) I also made it standalone compilable since I prefer code you can just plug in and go without worrying about adding headers and (ugh!) `using` statements. – paxdiablo Feb 15 '11 at 03:07
  • Just FYI, your fix forgot to return an int. :P – Matt Eskridge Feb 15 '11 at 03:08
  • 1
    Roll on, C++0x, where such nonsense isn't required :-) – paxdiablo Feb 15 '11 at 03:11
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.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
-2

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
}
LeonardBlunderbuss
  • 1,264
  • 1
  • 11
  • 22
Kartikya
  • 455
  • 2
  • 9