0

// I am trying to calculate the initial cost, current cost, and profit in this program. But somehow the calc() function wasn't working.

#include<iostream>

using namespace std;

int main()
{
  
    int shares;
    cout<<"Enter the number of shares: ";
    cin>>shares;
    cout<<shares<<endl;
    
    int buy;
    cout<<"Enter the Price you bought in: $";
    cin>>buy;
    cout<<buy<<endl;
    
    int current;
    cout<<"Enter the current price: ";
    cin>>current;
    cout<<current<<endl;
    
    void calc(float shares, float current, float buy, float &total, float &currentval, float &profit);
   
    return 0;
}


    void calc(float shares, float current, float buy, float &total, float &currentval, float &profit)
    {
        total=shares*buy;
        currentval=shares*current;
        profit=currentval-total;
        
        cout<<"The total is $"<<total<<endl;
        cout<<"The current value is $"<<currentval<<endl;
        cout<<"The profit is $"<<profit<<endl;
    }
splrs
  • 2,424
  • 2
  • 19
  • 29
  • 4
    `void calc(...);` is a function declaration, not an invocation of a function. Declare the function earlier and then call it without the `void`. – Nathan Pierson Feb 12 '21 at 23:45

2 Answers2

0

The code you posted does not call the function calc. The line in the main that look like a call, is declaring a function, but it is never called.

void calc(float shares, float current, float buy, float &total, float &currentval, float &profit); 

musst be replaced by:

float total,currentval,profit;
calc(shares, current, buy,total, currentval,profit); 
gerum
  • 974
  • 1
  • 8
  • 21
0

Before calling function, you need declare it. For example

#include <iostream>

int calc(int arg1, int arg2); //this is declaring of function calc, which gas two int arguments and returns integer

int main()
{
    //some your code
    std::cout << calc(2, 3); //this prints 10
}

int calc(int arg1, int arg2) //here you define what does this function do
{
    return arg1 * (arg1 + arg2); //something random calculating 
};

You declare your calc function after main, but you need to do it before.

roma57
  • 46
  • 1
  • 3
  • 2
    You can certainly declare functions within a function. You just can't have the body (implementation) of a function within another function. – PaulMcKenzie Feb 13 '21 at 00:09
  • what is the difference between *value and &value? – hdb3837 Feb 13 '21 at 00:14
  • @hdb3837 [That's a question better answered by a text book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) because it changes based on context. It'll be covered in the first few chapters. – user4581301 Feb 13 '21 at 00:27
  • @PaulMcKenzie but isn't it useless without lambdas? – roma57 Feb 14 '21 at 15:36
  • @roma57 It is not useless-- it is just that it is rarely used. It has existed since the early days of C++ (and C). The function prototype (declaration) can appear inside or outside a function body. This has really nothing to do with lambdas, just *where* you can declare a function. – PaulMcKenzie Feb 14 '21 at 17:45