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.
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;
}
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;
}
Related
I created this simple program to practice working with classes. I'm certain there are some errors with the way I used the class, but I'm just beginning to learn about them, so I haven't learned all of the conventions and etiquette. My main question is, can Xcode have functions that are buffered and other functions that are non-buffered? I'd like my function void InputValuesAndDisplayTotals(); to be buffered and my function void ManuallyAddCoinsAndDisplayTotals(); to be non-buffered (basically, hit a key and the character is instantly processed without using the enter key). Is this possible? Thanks in advanced.
#include <iostream>
#include <iomanip>
using namespace std;
class Coin
{
private:
const float PENNYVALUE = 0.01;
const float NICKELVALUE = 0.05;
const float DIMEVALUE = 0.10;
const float QUARTERVALUE = 0.25;
int PennyInput=0;
int NickelInput=0;
int DimeInput=0;
int QuarterInput=0;
public:
void InputValuesAndDisplayTotals();
void ManuallyAddCoinsAndDisplayTotals();
void Total();
};
int main()
{
int Choice;
Coin Count;
cout << "1 to enter total coin counts, 2 to manually count coins: ";
cin >> Choice;
if (Choice==1)
{
Count.InputValuesAndDisplayTotals();
Count.Total();
}
else
{
Count.ManuallyAddCoinsAndDisplayTotals();
}
cout << endl;
}
void Coin::InputValuesAndDisplayTotals()
{
cout << fixed << setprecision(2) << endl;
cout << "Input penny count: ";
cin >> PennyInput;
cout << "Total penny value: $" << PENNYVALUE*PennyInput;
Total();
cout << endl;
cout << "Input nickel count: ";
cin >> NickelInput;
cout << "Total nickel value: $" << NICKELVALUE*NickelInput;
Total();
cout << endl;
cout << "Input dime count: ";
cin >> DimeInput;
cout << "Total dime value: $" << DIMEVALUE*DimeInput;
Total();
cout << endl;
cout << "Input quarter count: ";
cin >> QuarterInput;
cout << "Total quarter value: $" << QUARTERVALUE*QuarterInput;
Total();
}
void Coin::ManuallyAddCoinsAndDisplayTotals()
{
char Choice2;
cout << "\n'1' for penny,\n'2' for nickel,\n'3' for dime,\n'4' for quarter,\n'q' to quit\n";
do
{
cout << "\nInput: ";
cin >> Choice2;
if (Choice2=='1')
++PennyInput;
if (Choice2=='2')
++NickelInput;
if (Choice2=='3')
++DimeInput;
if (Choice2=='4')
++QuarterInput;
cout << endl;
cout << "Pennies: " << PennyInput << endl;
cout << "Nickels: " << NickelInput << endl;
cout << "Dimes: " << DimeInput << endl;
cout << "Quarters: " << QuarterInput << endl;
Total();
}
while (Choice2!='q' && Choice2!='Q');
}
void Coin::Total()
{
cout << "\nTotal amount: $";
cout << fixed << setprecision(2) << (PENNYVALUE*PennyInput)+(NICKELVALUE*NickelInput)+(DIMEVALUE*DimeInput)+(QUARTERVALUE*QuarterInput);
cout << "\n";
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have to make a Calculator using a constructor and destructor, that adds, subtracts, multiplies and divides, and returns the total every time. For some reason, when I call the line "Calculator.add(num);" or any of the "Calculator." portions return me an error saying that it "expected an identifier". Am I missing something simple?
Thanks.
Here is my main.cpp file.
#include <iostream>
#include "Calculator.h"
#include <cstdlib>
using namespace std;
double total;
int main(){
while (true){
cout << "*** Calculator *** " << endl;
cout << "A: Add a value " << endl;
cout << "S: Subtract a value " << endl;
cout << "M: Multiply by a value " << endl;
cout << "D: Divide by a value " << endl;
cout << "T: Get the total " << endl;
cout << "Q: Quit " << endl;
cout << endl;
char input;
cin >> input;
if (input == 'A'){
cout << "Current Total: " << total << endl;
cout << "Selection: A";
cout << endl;
cout << "*** Add selected *** " << endl;
cout << "Value:";
double num;
cin >> num;
cout << endl;
double turnTotal = total;
Calculator.add(num);
cout << turnTotal << "+" << num << " = " << total;
}
if (input == 'S'){
cout << "Current Total: " << total << endl;
cout << "Selection: S";
cout << endl;
cout << "*** Subtract selected *** " << endl;
cout << "Value: ";
double num2;
cin >> num2;
cout << endl;
double turnTotal2 = total;
Calculator.subtract(num2);
cout << turnTotal2 << "-" << num2 << "=" << total;
}
if (input == 'M'){
cout << "Current Total: " << total << endl;
cout << "Selection: M";
cout << endl;
cout << "*** Multiply selected *** " << endl;
cout << "Value: ";
double num3;
cin >> num3;
cout << endl;
double turnTotal3 = total;
Calculator.multiply(num3);
cout << turnTotal3 << "*" << num3 << "=" << total;
}
if (input == 'D'){
cout << "Current Total: " << total << endl;
cout << "Selection: D";
cout << endl;
cout << "*** Divide selected *** " << endl;
cout << "Value: ";
double num4;
cin >> num4;
cout << endl;
double turnTotal4 = total;
Calculator.divide(num4);
cout << turnTotal4 << "/" << num4 << "=" << total;
}
if (input == 'T'){
cout << "Current Total: " << total << endl;
cout << "Selection: T";
cout << endl;
cout << "*** Total selected *** " << endl;
cout << "Value: ";
double num5;
cin >> num5;
cout << endl;
double turnTotal5 = total;
Calculator.getTotal(num5);
cout << turnTotal5 << "-" << num5 << "=" << total;
}
if (input == 'Q'){
cout << "Thank you for using the calculator! Bye bye! Have a great day!" << endl;
}
}
}
And here is the .cpp file
#include <cstdlib>
#include <iostream>
#include "Calculator.h"
using namespace std;
Calculator::Calculator(double x){
double total = x;
return;
}
double Calculator::getTotal(){
return total;
}
void Calculator::add(double x){
total += x;
}
void Calculator::subtract(double x){
total -= x;
}
void Calculator::multiply(double x){
total *= x;
}
void Calculator::divide(double x){
total /= x;
}
And here's the class .h file.
#include <cstdlib>
#include <iostream>
using namespace std;
//class specification
class Calculator {
public:
//constructor
Calculator(){double total = 0;}
Calculator(double total);
//member functions
void add(double x);
void subtract(double x);
void multiply(double x);
void divide(double x);
double getTotal();
//destructor
~Calculator();
private:
//data
double total = 0;
};
You doesn't seem to initialize your Calculator first. You could add the initialization for example on the start of main function as follows
double total;
Calculator calc;
int main(){
while (true){
And then use it like
calc.add(num);
I'm new to programming (in general) and C++ (in particular) and currently learning classes and objects.
I've defined the following as an exercise:
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
class X
{
public:
void setNum_1(int);
void setNum_2(int);
void setNum_3(int);
void setNum_4(int);
double getNum_1();
double getNum_2(int num_1);
double getNum_3(int num_1, int num_2);
double getNum_4(int num_1, int num_2, int num_3);
private:
int num_1;
int num_2;
int num_3;
int num_4;
};
int main()
{
X testObject;
int lNum_1 = 0;
int lNum_2 = 0;
int lNum_3 = 0;
int lNum_4 = 0;
cout << endl;
cout << "Please enter an integer: ";
cin >> lNum_1;
cout << "Please enter an integer: ";
cin >> lNum_2;
cout << "Please enter an integer: ";
cin >> lNum_3;
cout << "Please enter an integer: ";
cin >> lNum_4;
testObject.setNum_1(lNum_1);
testObject.setNum_2(lNum_2);
testObject.setNum_3(lNum_3);
testObject.setNum_4(lNum_4);
cout << endl;
cout << "The 1st number returned is: " << testObject.getNum_1() << endl;
cout << "The 2nd number returned is: " << testObject.getNum_2(lNum_1) << endl;
cout << "The 3rd number returned is: " << testObject.getNum_3(lNum_1, lNum_2) << endl;
cout << "The 4th number returned is: " << testObject.getNum_4(lNum_1, lNum_2, lNum_3) << endl;
cout << endl;
return 0;
}
void X::setNum_1(int n_1)
{
num_1 = n_1;
}
void X::setNum_2(int n_2)
{
num_2 = n_2;
}
void X::setNum_3(int n_3)
{
num_3 = n_3;
}
void X::setNum_4(int n_4)
{
num_4 = n_4;
}
double X::getNum_1()
{
return sqrt(num_1);
}
double X::getNum_2(int num_1)
{
return pow(num_2,3);
}
double X::getNum_3(int num_1, int num_2)
{
return num_1 * num_2;
}
double X::getNum_4(int num_1, int num_2, int num_3)
{
return (num_1 + num_2) / num_3;
}
Can anyone offer some guidance on how to modify this class so that it has only one member function with all of the arguments defaulted?
Thanks in advance,
Ryan
Ofcourse there are MANY ways to do this and most of them will be more elegant than what I have done here. But this should give you some ideas.
a. You should use the variables defined in the class
b. If you need to perform certain conditional operations in a function, use a switch case or an if statement. And decide the operation based on a parameter passed.
Again, many ways to make this more elegant, but this should get you started and thinking.
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
class X
{
public:
void setNums(int, int, int, int);
double performOp(int);
private:
int num_1;
int num_2;
int num_3;
int num_4;
};
int main()
{
X testObject;
int lNum_1 = 0;
int lNum_2 = 0;
int lNum_3 = 0;
int lNum_4 = 0;
cout << endl;
cout << "Please enter an integer: ";
cin >> lNum_1;
cout << "Please enter an integer: ";
cin >> lNum_2;
cout << "Please enter an integer: ";
cin >> lNum_3;
cout << "Please enter an integer: ";
cin >> lNum_4;
testObject.setNums(lNum_1,lNum_2,lNum_3,lNum_4);
cout << endl;
cout << "The 1st number returned is: " << testObject.performOp(1) << endl;
cout << "The 2nd number returned is: " << testObject.performOp(2) << endl;
cout << "The 3rd number returned is: " << testObject.performOp(3) << endl;
cout << "The 4th number returned is: " << testObject.performOp(4) << endl;
cout << endl;
return 0;
}
void X::setNums(int n_1, int n_2, int n_3, int n_4)
{
num_1 = n_1;
num_2 = n_2;
num_3 = n_3;
num_4 = n_4;
}
double X::performOp(int n)
{
if(n == 1) return sqrt(num_1);
if(n == 2) return pow(num_2,3);
if(n == 3) return num_1 * num_2;
if(n == 4) return (num_1 + num_2) / num_3;
}
Could someone explain why pow() in the following code is returning a 0 when the program is run, rather than the actual calculation? I'm a newbie to programming and I'm entirely stumped.
Thanks for any help.
#include <iostream>
#include <math.h>
#include <windows.h>
using namespace std;
//Prototypes:
double phiExpo;
double phiNegExpo;
double opt1f(double phi, double userInput){
return userInput * phi;}
double opt2f(double phi, double userInput){
return userInput / phi;}
double opt3f(){
return phiExpo;}
double opt4f(){
return phiNegExpo;}
double phiExpof(double phi, double userInput){
pow(phi, userInput);}
double phiNegExpof(double phi, double userInput){
pow(phi,-userInput);}
//Execute program:
int main()
{
double userInput;
int userChoice;
double phi = 1.61803399;
bool quit = false;
int userChoice2;
cout << "I want to (press corresponding number, then enter):" << endl;
cout << endl;
startchoices:
cout << "1. Multiply by phi:" << endl;
cout << "2. Divide by phi:" << endl;
cout << "3. Exponentiate phi:" << endl;
cout << "4. Negatively exponentiate phi:" << endl;
cout << "5. Quit." << endl;
cout << endl;
cin >> userChoice;
cout << endl;
do {
switch (userChoice){
case 1:
cout << "Enter number for multiplication: ";
cin >> userInput;
cout << endl;
cout << "Phi multiplied by " << userInput << ": ";
cout << opt1f(phi, userInput) << endl;
cout << endl;
Sleep(2000);
cout << "1. Continue." << endl;
cout << "2. Return to menu." << endl;
cout << endl;
cin >> userChoice2;
cout << endl;
if(userChoice2 > 1){
goto startchoices;}
break;
case 2:
cout << "Enter number for division: ";
cin >> userInput;
cout << endl;
cout << "Phi divided by " << userInput << ": ";
cout << opt2f(phi, userInput);
cout << endl;
Sleep(2000);
cout << "1. Continue." << endl;
cout << "2. Return to menu." << endl;
cout << endl;
cin >> userChoice2;
cout << endl;
if(userChoice2 > 1){goto startchoices;}
break;
case 3:
cout << "Enter number to exponentiate phi by: ";
cin >> userInput;
cout << endl;
cout << "Phi to the power of " << userInput << ": ";
cout << opt3f();
cout << endl;
Sleep(2000);
cout<<endl;
cout << "1. Continue." << endl;
cout << "2. Return to menu." << endl;
cout << endl;
cin >> userChoice2;
cout << endl;
if(userChoice2 > 1){goto startchoices;}
break;
}
}
}
You never actuall call pow. On choice 3, you only call opt3f, which only returns the global variable phiExpo, which is initialized to 0 because it's global. Then you also need to return from the phiExpof function, like others already pointed out.
It is probably not returning 0. Instead, you are not returning the result of pow:
double phiExpof(double phi, double userInput){
return pow(phi, userInput);
}
When you don't explicitly return a value, you will get undefined behavior, in this case 0.
Note:
I didn't notice the other code... This is one problem. The other is that you aren't actually calling phiExpof. Instead you are returning phiExpo which is a global variable.
How do you know it's returning 0? Your code doesn't even check the return value.
I am new to programming and trying to write a new program. While checking through my program it is returning the error code 1.#QNAN. I have tried isolating the variable and researching for answers but cannot find any solutions.
My code:
// This is a program to decide what culvert should be properly used for a specific installation
// using given measurements and data
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
// initializing functions
double slope_function();
double cbasin();
// initializing classes: Subdivisions specs
//intitializing global variables
double edge_road =0;
double up_stream =0;
double down_stream =0;
double tbm =0.0;
//double culv_length =0;
double slope = 0.0 ;
char street_name[1001];
int min_culv = 15;
double up_strm_culv =0;
double dwn_strm_culv =0;
int main (int nNumberofArgs, char* pszArgs[])
{
cout<< "This program will allow the surveyor to double check their calculations\n";
cout << "in deciding what size, type, and requirements are allowed for the\n";
cout << "installation of culverts in Terrebonne Parish.\n\n";
// begin input
cout << "what is the name of the street\nwhere the culverts will be installed: ";
cin.getline (street_name,1000);
cout << endl;
cout << "What is the Benchmark: ";
cin >> tbm;
cout << endl;
cout << "What is the elevation of the edge of the road: ";
cin >> edge_road;
cout << endl;
cout << "What is the up-stream culvert size: ";
cin >> up_strm_culv;
cout << endl;
cout << "What is the culverts up-stream inverted elevation: ";
cin >> up_stream;
cout << endl;
cout << "What is the down-stream culvert size: ";
cin >> dwn_strm_culv;
cout << endl;
cout << "What is the culverts down-stream inverted elevation: ";
cin >> down_stream;
cout << endl;
cout << "What is the length of culvert requested: ";
cin >> culv_length;
cout << "Your slope is : ";
cout << slope_function();
cout << endl;
cout << street_name;
cout << endl;
cout << cbasin();
cout << endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
system ("pause");
return 0;
}
// slope function
double slope_function()
{
double riseoverrun = 0.0;
slope = (up_stream - down_stream)/ culv_length;
return slope;
}
// Catch Basin function
double cbasin ( )
{
double cb = 0;
cb = culv_length / 60;
cout << endl;
cout << "You need ";
cout << cb;
cout << " catch basins for this job.";
cout << endl;
}
1#QNAN is a string representation for a "quiet NAN". A "NAN" is "not-a-number" and applies only to floats and doubles.
NANs can be very useful actually for representing "null" values (rather than picking some genuine number and hoping for the best you don't need that number for its natural meaning).
Some mathematical operations can return a NAN if the operation is "not valid" (eg taking the log of a negative number).
You can generate a QNAN from C++ using
double d = std::numeric_limits<double>::quiet_NaN();
Any comparison operation (==, <= etc) on a NAN returns false, even comparing its equality to itself, except for != which always returns true (even when comparing to itself).
(The actual bug in your code appears to be a function that returns double but has no return statement).