What's wrong in this C++ code? (Console) - c++

To me, this code seems to have no errors, and it is correct in the way I learnt C++. What may be wrong?
This is my code:
#include<iostream>
#include<cstdlib>
#include<string>
#include<cmath>
using namespace std;
double Calculation(long double x, long double y);
void Output(long double s, long double d, long double p, long double q);
void main(){
long double a;
long double b;
long double sum;
long double difference;
long double product;
long double quotient;
cout << "Enter your first number." << endl;
cin >> a;
cout << "Enter your second number." << endl;
cin >> b;
Calculation(a, b);
Output(sum, difference, product, quotient);
system("pause");
}
double Calculation(long double x, long double y){
long double sum;
long double difference;
long double product;
long double quotient;
sum = x + y;
difference = x - y;
product = x * y;
quotient = x / y;
return sum;
return difference;
return product;
return quotient;
}
void Output(long double s, long double d, long double p, long double q){
cout << "The sum of your numbers is " << s << "." << endl;
cout << "The difference between your numbers is " << d << "." << endl;
cout << "The product of your numbers is " << p << "." << endl;
cout << "The quotient of your numbers is " << q << "." << endl;
}
Explanation: This is a calculator which works by the variables 'a' and 'b'. It calculates the sum, difference, product, and quotient of 'a' and 'b' by the function Calculate and outputs the answers with the function Output.
Error: uninitialized local variable 'quotient' used.
uninitialized local variable 'product' used.
uninitialized local variable 'difference' used.
uninitialized local variable 'sum' used.

Lots of things are wrong with your code, but there is a single root cause to this - a misunderstanding of how the return statement works.
You have a function with multiple return statements. It appears that you think that all of these statements would execute; that assumption is incorrect. Only the first return statement reached in a function is executed; the remaining ones are ignored.
Moreover, you appear to imply that return statement would influence variables in the caller automatically; it wouldn't. In order to modify a variable in the caller, the caller itself needs to assign the returned value.
If you need your function to return multiple values, you need to change the approach: it should take multiple arguments by reference, and modify them, like this:
void Calculation(long double x, long double y, long double &sum,
long double &difference, long double &product, long double &quotient) {
sum = x + y;
difference = x - y;
product = x * y;
quotient = x / y;
}
You also need to change the prototype declaration of Calculation, like this:
void Calculation(long double x, long double y, long double &sum,
long double &difference, long double &product, long double &quotient);
Call Calculation like this:
Calculation(a, b, sum, difference, product, quotient);
This will solve your compilation issue, and the code will run correctly.

The problem is that you declare the variables listed in the error message as local variables. That means that no other function will be able to use them. Declaring them again in another function declares new local variables.
In this case you might want to declare the variables as global variables. This is done by moving the definitions outside of any function, and only have that definition and not in a function.

In your main function you are not setting any values to those variables before passing them to the Output() function - therefore they are 'uninitialized'. Also, as mentioned in some comments, there are a number of other problems there too, here's a couple:
1) You can't do multiple returns in a function on the same logic path
2) You are not collecting the return of Calculation() anyway
I expect you can fix your issues by passing some of those variables by reference instead.

As others said, the direct cause of your problem is misunderstanding on how the return and scoping works.
When dealing with C++ the compiler/linker warnings can be cryptic and/or confusing. In your example, the compiler should warn you about unreachable code after first return, however Visual Studio 2013 with default default does not do that.
You can make it do that by enabling all warnings, which is a good practice anyway. In project properties go to Configuration Properties -> C/C++ -> General -> Warning level and select EnableAllWarnings.
Final advice: if you are programming for fun or learning how to program, I'd advise you to start with C# or Java, which are easier and have better tool support.

Related

Trouble understanding how elements of a certain function work

I am required to fully understand the following code :
#include <iostream>
using namespace std;
double area(double length, double width);
double time(double p_area, double h_area, double mow_rate);
int main() {
double d_plot_length, d_plot_width, d_home_side, d_mow_rate;
double plot_area, home_area, time_taken;
// I've used double for all of these to get the most precise values possible, something I'd only really consider doing on small programmes such as this
cout << "What is the length of the plot? In meters please." << endl;
cin >> d_plot_length;
cout << "What is the width of the plot? In meters please." << endl;
cin >> d_plot_width;
cout<< "What is the size of the side of the house? In meters please." << endl;
cin >> d_home_side;
cout << "What is the rate at which you are going to be mowing? In meters per minute please" << endl;
cin >> d_mow_rate;
// Just getting all the data I need from the user
plot_area = area(d_plot_length, d_plot_width);
home_area = area(d_home_side, d_home_side);
time_taken = time(plot_area, home_area, d_mow_rate);
cout << "It will take " << time_taken << " minutes to mow this lawn. Better get cracking" << endl;
return 0;
}
double area(double length, double width) {
double value;
value = length * width;
return value;
}
double time(double p_area, double h_area, double mow_rate) {
double value;
value = (p_area - h_area) / mow_rate;
return value;
}
I am struggling to understand how the time() function works.
So far I understand that :
time_taken , gets its value from the time() function: time(plot_area, home_area, d_mow_rate).
The time() function gets its values from the function declaration at the bottom.
double time(double p_area, double h_area, double mow_rate) {
double value;
value = (p_area - h_area) / mow_rate;
return value;
}
However, this is where I'm stuck. The user is asked to enter values for d_plot_length, d_plot_width, etc. So I cannot understand how the compiler knows what these values p_area, and h_area actually are.
I realise that somehow the area() function is being used to aid the time() function, but as far as I'm aware the variables P_area etc within the time() function do not have values assigned to them.
Please can someone fill in the gaps in my understanding.
To be more precise, I want to know exactly how time_taken is displayed on the screen, from the start of the process, to the cout. Like I say I am familiar with most areas but not all.
In your program, you had computed the following values:
plot_area = area(d_plot_length, d_plot_width);
home_area = area(d_home_side, d_home_side);
When the method area(double,double) is invoked, the resultant double value gets stored in these variables.
Then you have the function call:
time_taken = time(plot_area, home_area, d_mow_rate);
This is the call by value type of function invocation. A copy of the values in the variables, plot_area, home_area and d_mow_rate are passed to the function. In the time(double, double, double) the computing is done upon the basis of the logic you had defined in this method and the resultant value is returned to the function call in the main() method.
Please note that the function call is of call by value and hence only a copy of the values are passed to the arguments mentioned in the function time(double, double, double) even though the variable names are the same in the main() and in the function call.
For further reading, I will suggest you to have a look at the following links:
Call By
Value
Call By
Reference
Call By
Pointer

Function power C++

I need to write need a function to compute power in C++
I don't understand why my code below is wrong.
ex. : base: 2 exponent 3 -> result: 4.48498e-306
if i use "int" the result is -> 2
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
double power(double base, double exponent);
int main()
{
double base, exponent, power;
cout << "Enter a base " << endl;
cin >> base;
cout << "Enter an exponent" << endl;
cin >> exponent;
cout << "The result is ': "<< power << endl;
return 0;
}
double power(double base, double exponent)
{
int i;
double s=1;
for (i = 0; i < exponent; ++i)
s *= base;
return s;
}
What am I doing wrong?
In your code you have two declarations of power.
double power(double base, double esponente);
double base,esponente,power;
You declare the variable power but you never initialize it before printing it, so it's going to be undefined. Rerunning your program will actually give you a different number, in all likelihood. You need to remove this local variable declaration, as it shadows the global function declaration.
Then, instead of
cout<<"\nL'elevamento a potenza e': "<<power<<endl;
You want this line:
cout<<"\nL'elevamento a potenza e': "<<power(base, esponente)<<endl;
You are either printing the address of the function power or printing the uninitialized value of the double power the way you have written the code. (Technically you are hitting an uninitialized shadowed variable). In any case, you are not even calling the function you wrote... How does the power function know to be called? With what arguments?
Fix 1. Suggest you delete the 'double' of power.
Fix 2. Actually call the function and use/store/print the result
example.
After getting input from user do this:
double result = power( base, exponent );
cout << result << endl;
To print the result.
You need call function like this, with parameters:
Here is your main. You don't need local variable power. You want call function with name power.
int main ()
{
double base,esponente;
cout<<" \n Inserisci base \n";
cin>>base;
cout<<"\nInserisci esponente\n";
cin>>esponente;
cout<<"\nL'elevamento a potenza e': "<<power(base,esponente)<<endl;
system ("PAUSE");
}

Trouble with simple functions

I am writing code to call functions having to do with force, mass, and acceleration equations. The functions are called correctly, but the inputs are not multiplied as they should be. My output for the first function is a crazy small number, and the output for the first function is always 0.
Here is my code. Any feedback wold be very helpful. Thanks.
#include <iostream>
#include <cstdlib>
using namespace std;
void displayMenu();
double force(double);
double secondForce(double,double);
int main(int argc, char** argv)
{
int menuOption;
displayMenu();
system("PAUSE");
return 0;
}
void displayMenu(void)
{
int menuOption;
double weight, accel;
cout << " Main Menu" << endl;
cout << "Enter 1 for Force calculation with acceleration = 9.8m/s^2.\n";
cout << "Enter 2 for Force calculation with user defined acceleration.\n";
cout << "Enter 3 to quit the program.\n";
cin >> menuOption;
if(menuOption==1)
{
cout << "Enter a mass.\n";
cin >> weight;
cout << "The force is ";
cout << force(weight);
cout << "N.";
}
else if(menuOption==2){
cout << "Enter a mass.\n";
cin >> weight;
cout << "Enter an acceleration.\n";
cin >> accel;
cout << "The force is ";
cout << secondForce(weight, accel);
cout << "N.";
}
}
double force(double weight)
{
double force, mass;
force=(mass*(9.8));
return force;
}
double secondForce(double secondMass, double secondWeight)
{
double secondForce, mass, acceleration;
secondForce=(mass*acceleration);
return secondForce;
}
You are using uninitialized variables in your functions. Using these uninitialized variables in your program is undefined behavior.
force=(mass*(9.8)); << mass has a garbage value
secondForce=(mass*acceleration); << mass and acceleration have a garbage value
I think you meant to have
double force(double weight)
{
return weight * 9.8;
}
double secondForce(double secondMass, double secondAccel)
{
return secondMAss * secondAccel;
}
The problem is you are using variables in your calculation that are undefined. See my embedded comments below:
double secondForce(double secondMass, double secondWeight)
{
double secondForce, mass, acceleration;
secondForce=(mass*acceleration); //what is the value of mass and acceleration here??
return secondForce;
}
As a side note: Consider stepping through your code inside of a debugger like gdb so you can see how your program executes. Trying to reason through it is very difficult for large programs. In your example, it's easy to spot for an experienced programmer.
You're multiplying the data by garbage values
double force(double weight)
{
double force, mass;
force=(mass*(9.8));
return force;
}
double secondForce(double secondMass, double secondWeight)
{
double secondForce, mass, acceleration;
secondForce=(mass*acceleration);
return secondForce;
}
What's the value of mass and acceleration here? These variables are not initialized. You have to assign some values to them first.
You should enable compiler warnings and pay attention to them. For example, here is what MSVC says even on the lowest warning level:
warning C4700: uninitialized local variable 'mass' used
warning C4700: uninitialized local variable 'mass' used
warning C4700: uninitialized local variable 'acceleration' used
In C++, using an uninitialised variable is never a good idea. Unlike in other languages, most variables in C++ are not automatically initialised with 0 values. This causes your program to inhibit unspecified or undefined behaviour when the value contained in the variable is to be accessed.
Be safe and correct. Initialise! For example:
double force = 0.0;
double mass = 0.0;
Note that the language also allows you to first initialise, then assign, then use the variable. For example:
double force;
force = 0.0;
cout << force;
But that's bad style. Why leave a variable uninitialised and then later assign something when you can do everything in one step? Leaving the variable uninitialised first also prevents you from making it const.
Ensuring correct initialisation of all variables will make the behaviour of your program defined and consistent. You will have other problems, though, because you multiply the 0 mass, which will always result in 0 and is certainly not what you intend. However, deterministic behaviour is a good starting point to fix the wrong math.

Unresolved external link in C++ function [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I keep getting the following errors and can't seem to resolve them:
error LNK2019: unresolved external symbol "double __cdecl orderIn(double,double,double)" (?orderIn##YANNNN#Z) referenced in function _main
fatal error LNK1120: 1 unresolved externals
I know there is something wrong with the way I am trying to pass the variable through the functions but I just can't get it. I want the information gathered and calculated in the first function to pass through and be utilized by the second function. I have tried numerous methods to no avail.
What am I missing here?
Thanks!
#include <iostream>
#include <iomanip>
using namespace std;
double orderIn(double, double, double);
void shippingOut(double, double, double);
double spoolsOrdered,
spoolsInStock,
shipping,
total,
backordered,
charges,
spoolsShipping;
int main()
{
orderIn(spoolsOrdered, spoolsInStock, shipping);
shippingOut(spoolsShipping, backordered, total);
return 0;
}//end int main
double orderIn(double &spoolsOrdered, double &spoolsInStock, double &shipping)
{
char extracharge;
//spools ordered
cout << "How many spools would you like to order? ";
cin >> spoolsOrdered;
while (spoolsOrdered < 1)
{
cout << "That is not a valid entry ";
cin >> spoolsOrdered;
}
//spools in stock
cout << "How many spools are currently in stock? ";
cin >> spoolsInStock;
//extra charges
cout << "Are there any special charges on this order? ";
cin >> extracharge;
//special charges
if ( extracharge == 'Y' || extracharge == 'y')
{
cout << "What is the additional charge per spool? ";
cin >> charges;
shipping = (10 + charges);
}
else
shipping = 10;
return (&spoolsOrdered, &spoolsInStock, shipping);
}
void shippingOut(double spoolsOrdered, double spoolnStock, double shipping)
{
double backordered;
double subTotal;
double totalShipping;
double total;
double spoolsShipping;
if (spoolsOrdered > spoolsInStock)
{
backordered=(spoolsOrdered - spoolsInStock);
cout << "There are " << spoolsInStock << " spools ready to be shipped./n";
cout << "The remaining " << backordered <<" are on backorder.";
spoolsShipping=spoolsInStock;
}
else
{
cout << "All " <<spoolsOrdered << " spools ordered are ready to ship.\n";
spoolsShipping=spoolsOrdered;
}
//Product Charges
subTotal = spoolsShipping * 100;
cout << "Subtotal: $" << subTotal << endl;
//Shipping Charges
totalShipping = spoolsOrdered * shipping;
cout << "S/H Total: $" << totalShipping << endl;
//Total
total = subTotal + totalShipping;
cout << "The total of the order ready to ship is: $" << total << endl;
}
You declare:
double orderIn(double, double, double);
and then use it. You later define:
double orderIn(double &spoolsOrdered, double &spoolsInStock, double &shipping)
This is a different function; the argument types are references to double, not simple double.
Fix either the declaration or the definition — it looks like you really need to fix the declaration since you want to set the variables in the calling function:
double orderIn(double &, double &, double &);
You should also review why you have so many global variables, and why the global variable names are shadowed by the parameters. Avoid globals whenever possible.
a) when I tried to run it in the past, I could only get it to work by defining the variables before main. Where should all of these variables be declared?
Normally, you'll declare variables in a function (in this case, main()), and then pass the variables to functions that need to use them. Sometimes, globals are appropriate. So, I expected:
int main()
{
double spoolsOrdered = 0.0;
double spoolsInStock = 0.0;
double shipping = 0.0;
double total = 0.0;
double backordered = 0.0;
double spoolsShipping = 0.0;
orderIn(spoolsOrdered, spoolsInStock, shipping);
shippingOut(spoolsShipping, backordered, total);
// Use these values?
return 0;
}
If you don't have a use for the values in main(), why are you passing them around in the first place.
Then I noticed that both orderIn() and shippingOut() return a double, but you don't use the value. What does orderIn() return? There's a surprise:
return (&spoolsOrdered, &spoolsInStock, shipping);
This doesn't do what you think it does. The commas are the comma operator. The address of spoolsOrdered is evaluated and discarded; the address of spoolsInStock is evaluated and discarded; then the value in shipping is returned. You could change the function to return void and remove the return statement altogether, similar to shippingOut().
The global variable charges should be a local variable in orderIn().
b) what is the relationship between the variables and the parameters?
Inside the function orderIn(double &spoolsOrdered, double &spoolsInStock, double &shipping), the parameters each hide a global variable of the same name. Since this is C++, you can still access the global variable by using the scope operator :: like this:
::spoolsOrdered // The global variable
spoolsOrdered // The local reference variable -- a reference to the global
Largely by coincidence, it ends up being much the same in this case, but if you had value parameters or pointer parameters, or if the call did not pass the global variables as the reference parameters, the effects would be quite different.
If you use GCC (g++), the -Wshadow option reports shadowing problems.
Your function prototype is:
double orderIn(double, double, double);
However your actual function definition is:
double orderIn(double &spoolsOrdered, double &spoolsInStock, double &shipping)
{
}
double and double& are different types, thus you need to either adjust the prototype or the definition.

How do I make a program using 3 overloaded functions (difference in parameters is int, long, float) meanwhile only asking one input entry?

I have to write a program where I put in two numbers and the program calculates the average. I want to write my program so that if I input two decimal numbers, the program will call the float function, and if I input two integer numbers it will call the integer function, input two long numbers, it will call long function.
The main problem I am having is choosing the variable type. Because if I use a long type for my variable, but the two numbers input by the user are decimal, then the program calls the long function and the average comes out as a long variable.
Here is what I've got so far:
#include <iostream>
int average(int, int);
long average(long, long);
float average(float, float);
int main()
{
//The variables
// The problem with them is that if I use
// a type, let's say int, but the input
// is a float, then it won't work
x;
y;
averag;
std::cout << "Here is a number: ";
std::cin >> x;
std::cout << "\nHere is another one: ";
std::cin >> y;
//The function in call
averag = average(x, y);
std::cout << "\n\nHere is the average: ";
std::cout << averag;
return 0;
}
//The definition of the int function
int average(int x, int y)
{
int x;
int y;
int average;
average = (x + y) / 2;
return average;
}
// The definition of the short function
short average(short x, short y)
{
short x;
short y;
short average;
average = (x + y) / 2;
return average;
}
// The definition of the float function
float average(float x, float y)
{
float x;
float y;
float average;
average = (x + y) / 2;
return average;
}
//I think i'm not grasping the scopes in which I can define a variable
//and where I must define the parameters within the functions
I think that defining the type variables in each function scope is wrong, but how else am i supposed to define the variables!
How to do that ?
I would have the user input the type first (e.g. 'Enter F for float, I for integer...' etc) then using that input as a condition, read the input into the appropriate type of variable and call your overloaded average function.
Yes, defining the variables in the function scope the way you have is wrong. Your parameters are defining the variables, defining them in the function will just define them again. I'm not the most familiar with c++, but I think that will "shadow" the parameter variables, and your calculations will not work.
For the main question, I agree with sje397's answer, have the user input the type of average they want to perform first. If the user enters 5 and 4, the double average will be different than the long average.
I would input the value as a string and then use successive calls to boost::lexical_cast to infer a type:
std::string x,y;
std::cin >> x >> y;
try {
short averag = average(boost::lexical_cast<short>(x), boost::lexical_cast<short>(y));
std::cout << averag << "\n";
return;
} catch(boost::bad_lexical_cast&) {}
try {
int averag = average(boost::lexical_cast<int>(x), boost::lexical_cast<int>(y));
std::cout << averag << "\n";
return;
} catch(boost::bad_lexical_cast&) {}
try {
float averag = average(boost::lexical_cast<float>(x), boost::lexical_cast<float>(y));
std::cout << averag << "\n";
return;
} catch(boost::bad_lexical_cast&) {}
std::cout << "Oops\n";
I'd probably not use this form, however. I'd probably factor out the common code into a function template.
You could ask the user which calculation he wants to perform.
Drawbacks:
Internals of the program which are of no interest to the user, are exposed (the user wants to calculate the average of two numbers).
You cannot trust the user. If your program is robust and doesn't crash (for example if the user types in decimal numbers after choosing the integer calculation), it may produce wrong results
I reccomend to read in the input each time and look if the value is a decimal number, a short integer (less than 9 digits) or a long integer. Then you have to consider a few cases (for example one input is decimal, the other a long integer) and to perform the calcuation. It's quite low-level, but I would it refactor out into a function anyway.