13 Replies - 758 Views - Last Post: 25 March 2010 - 03:15 AM Rate Topic: -----

#1 AbeB  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 28-February 10

Using one function

Posted 25 March 2010 - 12:06 AM

Hi, I'm trying to write this program so that is ask the user to input a wholesale cost and the markup percentage, then display the retail price after that has been added up.

this my first time using function and I'm not sure I did correctly. It's definitely not doing what I'm trying to make it do.

"The program should have a function named calculateRetail that receives the wholesale cost of the item and the markup percentage as arguments, and returns the retail price of the item"

Any help would would be Greatly Appreciated.

Thank you

#include <cstdlib>
#include <iostream>

using namespace std;

//function prototype
void calculateRetail(double, double);


int main(int argc, char *argv[])
{
    
    double wholeCost;
    double markupPercent;
    double retailPrice;
    
    cout << "Please enter the Whole Sale price: ";
    cin >> wholeCost;
    cout << "Please enter the Markup Percentage as a decimal: ";
    cin >> markupPercent;
    
    
    
}
    double calculateRetail(double wholeCost, double markupPercent)
    {
           return wholeCost * markupPercent;
     }
         
         
         
         
         
   
    
    system("PAUSE");
    return EXIT_SUCCESS;
}





Is This A Good Question/Topic? 0
  • +

Replies To: Using one function

#2 Trav  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 210
  • Joined: 01-March 10

Re: Using one function

Posted 25 March 2010 - 12:14 AM

Return 0 should be at the end of main(), and also the last "}" was unnecessary.
#include <cstdlib>
#include <iostream>

using namespace std;

//function prototype
void calculateRetail(double, double);

int main(int argc, char *argv[])
{
    double wholeCost;
    double markupPercent;
    double retailPrice;
    
    cout << "Please enter the Whole Sale price: ";
    cin >> wholeCost;
    cout << "Please enter the Markup Percentage as a decimal: ";
    cin >> markupPercent;

    system("pause");
    return 0;   
}

double calculateRetail(double wholeCost, double markupPercent)
{
    return wholeCost * markupPercent;
}

Was This Post Helpful? 1
  • +
  • -

#3 AbeB  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 28-February 10

Re: Using one function

Posted 25 March 2010 - 12:25 AM

I'm getting a compiling error stating that "new declaration 'double calculateRetail(double, double)'"

#include <cstdlib>
#include <iostream>

using namespace std;

//function prototype
void calculateRetail(double, double);

int main(int argc, char *argv[])
{
    double wholeCost;
    double markupPercent;
    double retailPrice;
    
    cout << "Please enter the Whole Sale price: ";
    cin >> wholeCost;
    cout << "Please enter the Markup Percentage as a decimal: ";
    cin >> markupPercent;

    system("pause");
    return 0;   
}

double calculateRetail(double wholeCost, double markupPercent)
{   // compiler says this line is the error
    return wholeCost * markupPercent;
}
   
    
    system("PAUSE");
    return EXIT_SUCCESS;
}



This post has been edited by AbeB: 25 March 2010 - 12:25 AM

Was This Post Helpful? 0
  • +
  • -

#4 AbeB  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 28-February 10

Re: Using one function

Posted 25 March 2010 - 12:31 AM

Oh I got it to work just changed the function prototype from void to double.

I have another question, How do I make the function return the retail price, that is the whole sale cost and the markup percentage added together.


Thank you

#include <cstdlib>
#include <iostream>

using namespace std;

//function prototype
double calculateRetail(double, double);

int main(int argc, char *argv[])
{
    double wholeCost;
    double markupPercent;
    double retailPrice;
    
    cout << "Please enter the Whole Sale price: ";
    cin >> wholeCost;
    cout << "Please enter the Markup Percentage as a decimal: ";
    cin >> markupPercent;

    system("pause");
    return 0;   
}

double calculateRetail(double wholeCost, double markupPercent)
{
    return wholeCost * markupPercent;
}


Was This Post Helpful? 0
  • +
  • -

#5 Trav  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 210
  • Joined: 01-March 10

Re: Using one function

Posted 25 March 2010 - 12:48 AM

After the input from the user.

retailPrice = calculateRetail(wholeCost, markupPerect);
cout << retailPrice;

This post has been edited by Trav: 25 March 2010 - 12:48 AM

Was This Post Helpful? 1
  • +
  • -

#6 AbeB  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 28-February 10

Re: Using one function

Posted 25 March 2010 - 01:56 AM

I tried compiling but i get an error saying "retailPrice undeclared(first use this function"

any ideas?

I need the function to calculate the wholesale cost * markupPrice and return the retail price.


#include <cstdlib>
#include <iostream>

using namespace std;

//function prototype
double calculateRetail(double, double);

int main(int argc, char *argv[])
{
    double wholeCost;
    double markupPercent;
    double WholeNMarkup;
    double retailPrice;
    

    
    
    
    cout << "Please enter the Whole Sale price: ";
    cin >> wholeCost;
    
    cout << "Please enter the Markup Percentage as a decimal: ";
    cin >> markupPercent;
    
    WholeNMarkup = wholeCost * markupPercent;
    retailPrice = WholeNMarkup;
    
   retailPrice = calculateRetail(wholeCost, markupPercent);
   cout << retailPrice;
    
    
    

    system("PAUSE");
    return 0;   
}

double calculateRetail(double wholeCost, double markupPercent)
{

       wholeCost * markupPercent;
       return retailPrice;
   
} 



Was This Post Helpful? 0
  • +
  • -

#7 Trav  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 210
  • Joined: 01-March 10

Re: Using one function

Posted 25 March 2010 - 02:16 AM

You don't need all this.

WholeNMarkup = wholeCost * markupPercent;
    retailPrice = WholeNMarkup;
    
   retailPrice = calculateRetail(wholeCost, markupPercent);
   cout << retailPrice;


You could simply do this, since you declared "retailPrice" as a double at the start of your progam.

retailPrice = wholeCost * markupPercetn;
     cout << retailPrice;


#include <cstdlib>
#include <iostream>

using namespace std;

//function prototype
double calculateRetail(double, double);

int main(int argc, char *argv[])
{
    double wholeCost;
    double markupPercent;
    // double WholeNMarkup; // you don't need this
    double retailPrice; // declaration of retailPrice as a double
    

    
    
    
    cout << "Please enter the Whole Sale price: ";
    cin >> wholeCost;
    
    cout << "Please enter the Markup Percentage as a decimal: ";
    cin >> markupPercent;
    
    retailPrice = wholeCost * markupPercent;
    cout << "Retail price: " << retailPrice;

    system("PAUSE");
    return 0;   
}

This post has been edited by Trav: 25 March 2010 - 02:18 AM

Was This Post Helpful? 1
  • +
  • -

#8 AbeB  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 28-February 10

Re: Using one function

Posted 25 March 2010 - 02:16 AM

I believe this code is correct.

The only problem I am having is that when i run the program it displays the retail price as 1.#QNAN instead of 11.

Any advice would be great

thanks

#include <cstdlib>
#include <iostream>

using namespace std;

//function prototype
double calculateRetail(double, double);

int main(int argc, char *argv[])
{
    double wholeCost;
    double markupPercent;
    double WholeNMarkup;
    double retailPrice;
    

    
    
    
    cout << "Please enter the Whole Sale price: ";
    cin >> wholeCost;
    
    cout << "Please enter the Markup Percentage as a decimal: ";
    cin >> markupPercent;
    
    
    
   retailPrice = calculateRetail(wholeCost, markupPercent);
   cout << "The retail price is: $" << retailPrice << endl;
    
    
    

    system("PAUSE");
    return 0;   
}

double calculateRetail(double wholeCost, double markupPercent)
{

       wholeCost * markupPercent;
       
   
}


Was This Post Helpful? 0
  • +
  • -

#9 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Using one function

Posted 25 March 2010 - 02:26 AM

Your forgot the return statement in "calculateRetail" function. Use:
double calculateRetail(double wholeCost, double markupPercent)
{
    return (wholeCost * markupPercent);
}


Also, I don't get it when you say that 11 is outputted as 1.#QNAN. Could you give us an input example, and tell us what the program should output, so we can figure it out?

This post has been edited by sarmanu: 25 March 2010 - 02:26 AM

Was This Post Helpful? 1
  • +
  • -

#10 AbeB  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 28-February 10

Re: Using one function

Posted 25 March 2010 - 02:39 AM

Hi thank you for that fix. I inputed 10 as the whole sale cost then I inputed .10 as the markup price, .10 meaning 10% which would be $1. then the retail price should hold that already multiplied and added together.

BUT

I can't figure out what I am doing wrong, when i run the program and it says "the retail price is: $" it says the amount of the markup percent which is $1.

any ideas?
Was This Post Helpful? 0
  • +
  • -

#11 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Using one function

Posted 25 March 2010 - 02:49 AM

.10 means 0.1
0.1 * 10 = 1, so yes, you get the expected output.

This post has been edited by sarmanu: 25 March 2010 - 02:51 AM

Was This Post Helpful? 1
  • +
  • -

#12 AbeB  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 28-February 10

Re: Using one function

Posted 25 March 2010 - 02:57 AM

when i do that it returns wholesale cost that I input. I need the function to multiply wholeCost with markupPercent and then add that number to the wholeCost to make the retailPrice.

Do you get what I'm trying to? =/

I'll show you what I did to make it output what I want but I am suppose to make the function receive the wholeCost and markupCost as arguments and return the retailPrice


#include <cstdlib>
#include <iostream>

using namespace std;

//function prototype
double calculateRetail(double, double);

int main(int argc, char *argv[])
{
    double wholeCost;
    double markupPercent;
    double WholeNMarkup;
    double retailPrice;
    

    
    
    
    cout << "Please enter the Whole Sale price: ";
    cin >> wholeCost;
    
    cout << "Please enter the Markup Percentage as a decimal: ";
    cin >> markupPercent;
    
    
    
   retailPrice = calculateRetail(wholeCost,markupPercent);
   cout << "The retail price is: $" << retailPrice << endl;
    
    
    

    system("PAUSE");
    return 0;   
}

double calculateRetail(double wholeCost, double markupPercent)
{

      return (wholeCost * markupPercent);
       
   
} 
   


Was This Post Helpful? 0
  • +
  • -

#13 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Using one function

Posted 25 March 2010 - 03:04 AM

Something like that?
double calculateRetail(double wholeCost, double markupPercent)
{
      double D = wholeCost * markupPercent; 
      return (D + wholeCost); // add wholeCost * markupPercentage to wholeCost
}


This post has been edited by sarmanu: 25 March 2010 - 03:06 AM

Was This Post Helpful? 1
  • +
  • -

#14 AbeB  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 28-February 10

Re: Using one function

Posted 25 March 2010 - 03:15 AM

That works perfect. Thank you both very much for you help and time


:) :clap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1