I am trying to create a program where you enter two values in individual functions and then print them out in the main function. But I am having an error stating that my function is not returning values.
#include <iostream>
#include <iomanip>
using namespace std;
void welcome();
double mass(double m);
double freqnat(double nf);
int main()
{
double attachedmass = 0;
double naturalfrequency = 0;
welcome();
mass(attachedmass);
freqnat(naturalfrequency);
cout << attachedmass << setw(20) << naturalfrequency << endl;
}
void welcome()
{
cout << "Welcome to the spring stiffness program." << endl << endl << "This program calculates spring stiffness using mass and natural frequency to calculate your spring stiffness." << endl << endl;
system("pause");
cout << endl;
}
double mass(double m)
{
cout << "Please enter your desired mass." << endl << endl;
cin >> m;
}
double freqnat(double nf)
{
cout << "Please enter your desired natural frequency." << endl << endl;
cin >> nf;
}
I tried using return m; and return nf; at the end of the functions, hoping this would tell the function to return the values inputted by the user. Instead, the program does run but the values print out as zeroes.
CodePudding user response:
You need to return values AND store them somewhere:
// storing results in variables
attachedmass = mass(attachedmass);
naturalfrequency = freqnat(naturalfrequency);
Your functions should be:
double mass(double m)
{
cout << "Please enter your desired mass." << endl << endl;
cin >> m;
return m;
}
double freqnat(double nf)
{
cout << "Please enter your desired natural frequency." << endl << endl;
cin >> nf;
return nf;
}
Having said this, you don't need to pass any parameters at all to the functions for this. They can be something like this:
#include <iostream>
#include <iomanip>
using namespace std;
void welcome()
{
cout << "Welcome to the spring stiffness program." << endl << endl << "This program calculates spring stiffness using mass and natural frequency to calculate your spring stiffness." << endl << endl;
cout << endl;
}
double mass()
{
double user_in;
cout << "Please enter your desired mass." << endl << endl;
cin >> user_in;
return user_in;
}
double freqnat()
{
double user_in;
cout << "Please enter your desired natural frequency." << endl << endl;
cin >> user_in;
return user_in;
}
int main()
{
double attachedmass = 0;
double naturalfrequency = 0;
welcome();
attachedmass = mass();
naturalfrequency = freqnat();
cout << attachedmass << setw(20) << naturalfrequency << endl;
}
CodePudding user response:
So with youre program you are using the functions but you are not assigning the return value to anything so the two double values will not change, you will probably want to pass the doubles in by reference like this:
double mass(double& m);
and
double freqnat(double& nf);
Before you werent changing the two double values atall so they would not change but now you are passing them by reference which means you can change them.
Also you will not need to return anything so you can just make mass and freqnat's return type void as you just passed a reference to it. So the working program would be:
#include <iostream>
#include <iomanip>
using namespace std;
void welcome();
double mass(double m);
double freqnat(double nf);
int main()
{
double attachedmass = 0;
double naturalfrequency = 0;
welcome();
mass(attachedmass);
freqnat(naturalfrequency);
cout << attachedmass << setw(20) << naturalfrequency << endl;
}
void welcome()
{
cout << "Welcome to the spring stiffness program." << endl << endl << "This program calculates spring stiffness using mass and natural frequency to calculate your spring stiffness." << endl << endl;
system("pause");
cout << endl;
}
void mass(double& m)
{
cout << "Please enter your desired mass." << endl << endl;
cin >> m;
}
void freqnat(double& nf)
{
cout << "Please enter your desired natural frequency." << endl << endl;
cin >> nf;
}