pow() returning 0 (C++) - c++

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.

Related

I tried to print the things in a vector, but a switch statement is not working

#include <iostream>
#include <vector>
using namespace std;
void printing(vector<int> numbers);
char ask(char selection);
int main(){
vector<int> numbers{1,1,2};
char selection{};
ask(selection);
switch(selection) {
case 'P':
printing(numbers);
break;
case 'M':
cout << "FF" << endl;
break;
default :
cout << "error" << endl;
}
printing(numbers);
return 0;
}
char ask(char selection)
{
cout << "Enter the selection " << endl;
cout << "\nP - Print numbers" << endl;
cout << "A - Add a number" << endl;
cout << "M - Display mean of the numbers" << endl;
cout << "S - Display the smallest number" << endl;
cout << "L - Display the largest number"<< endl;
cout << "Q - Quit" << endl;
cout << "\nEnter your selection: ";
cin >> selection;
return selection;
}
void printing(vector<int> numbers){
if (numbers.size() == 0){
cout << "[] - the list is empty" << endl;
}
else{
cout << "[ " ;
for (auto num : numbers){ cout << num << " " ; }
cout << "] " << endl;
}
}
Here the switch statement is not working, it's not printing the vector that I need.
You have declared your function char ask(char selection) to take selection by value. Meaning the calling function passes a copy of the char to the ask() function. Any changes to the copy will only be seen in the ask() function and not the function that calls it.
char ask(char selection)
{
cout << "Enter the selection " << endl;
cout << "\nP - Print numbers" << endl;
cout << "A - Add a number" << endl;
cout << "M - Display mean of the numbers" << endl;
cout << "S - Display the smallest number" << endl;
cout << "L - Display the largest number"<< endl;
cout << "Q - Quit" << endl;
cout << "\nEnter your selection: ";
cin >> selection;
return selection;
}
This passing by value is odd since you don't use the value passed into the function and instead want the value from the function. One way to fix this is to change the function to not take a char.
char ask()
{
char selection;
cout << "Enter the selection " << endl;
cout << "\nP - Print numbers" << endl;
cout << "A - Add a number" << endl;
cout << "M - Display mean of the numbers" << endl;
cout << "S - Display the smallest number" << endl;
cout << "L - Display the largest number"<< endl;
cout << "Q - Quit" << endl;
cout << "\nEnter your selection: ";
cin >> selection;
return selection;
}
then in your calling function int main() use the value returned:
char selection{ask()};
instead of:
char selection{};
ask(selection);
For more information about pass by value see this question: What's the difference between passing by reference vs. passing by value?

How do I run by attached a function

now i have create a code to calculate a formula but i have a problem with number 4,5,6,7 it can select but it not running. working principle of code is select a number in the box click enter and it will enter into that formula.i think my problem is a case function but i tried to fix and it can't.
....
#include<iostream>
#include<conio.h>
#include<cmath>
#include<math.h>
#include<iomanip>
#include<windows.h>
#define PI 3.14159
using namespace std;
HANDLE h=GetStdHandle(STD_OUTPUT_HANDLE);
//set Color
void colorText(int i){
SetConsoleTextAttribute(h,i);
}
//Declaring Variable
float density(float mass, float volume){
return mass / (double)volume;
}
int exponent(int base,int index,int result){
return result = pow(base, index);
}
float hypotenuse(float A_side, float B_side,float c_side_result){
return c_side_result = sqrt((A_side*A_side)+(B_side*B_side));
}
float velocity(float velo, float initial, float acceleration, float time){
return velo = initial + (acceleration * time);
}
float mass_en(float mass_value, float speedlight_value, float energy){
return energy = mass_value * (speedlight_value * speedlight_value);
}
float percentage(float percent, float number, float base_per){
return percent = (number/base_per)*100;
}
// Main Menu
void main_Menu(){
colorText(11);
system("CLS");
cout << "----------------------\n";
cout << "* 6 Useful formula *\n";
cout << "----------------------\n";
cout << setw(3)<<"1.finding density \n";
cout << setw(3)<<"2.finding exponent\n";
cout << setw(3)<<"3.finding hypotenuse\n";
cout << setw(3)<<"4.finding velocity\n";
cout << setw(3)<<"5.finding mass\n";
cout << setw(3)<<"6.finding percentage\n";
cout << setw(3)<<"7.member\n";
cout << "----------------------\n";
}
//single Menu
void density_menu(){
system("CLS");
cout << "---------------------\n";
cout << "* finding Density *\n";
cout << "---------------------\n";
}
void exponent_menu(){
system("CLS");
cout << "---------------------\n";
cout << "* finding Exponent *\n";
cout << "---------------------\n";
}
void hypotenuse_menu(){
system("CLS");
cout << "---------------------\n";
cout << "* finding Hypotenuse *\n";
cout << "---------------------\n";
}
void velocity_menu(){
system("CLS");
cout << "---------------------\n";
cout << "* finding Velocity *\n";
cout << "---------------------\n";
}
void mass_menu(){
system("CLS");
cout << "---------------------\n";
cout << "* finding Mass *\n";
cout << "---------------------\n";
}
void percentage_menu(){
system("CLS");
cout << "---------------------\n";
cout << "* finding percentage *\n";
cout << "---------------------\n";
}
int main(){
char choosechoice;
do{
main_Menu();
cout << "Press X or x to exit: ";
cin >> choosechoice;
switch(choosechoice){
case '1':
system("CLS");
float mass, volume;
cout << fixed;
cout << setprecision(2);
density_menu();
cout << "Enter Mass: ";
cin >> mass;
cout << "Enter Volume: ";
cin >> volume;
system("CLS");
density_menu();
cout << setw(10)<<"Mass: " << setw(6)<<mass << " kg"<< endl;
cout << setw(10)<<"Volume: " << setw(6)<<volume << " m^3"<< endl;
cout << setw(10)<<"Density: " << setw(6)<<density(mass,volume)<<" kg/m^3";
cout << "\n----------------------";
cout << "\nPress any key to return to main menu";
getch();
break;
case '2':
system("CLS");
int base, index, result;
cout << fixed;
cout << setprecision(2);
exponent_menu();
cout << "Enter Base number: ";
cin >> base;
cout << "Enter Index number: ";
cin >> index;
system("CLS");
exponent_menu();
cout << setw(15)<<"Base number: " << setw(6)<<base << endl;
cout << setw(15)<<"Index number: " << setw(6)<<index << endl;
cout << setw(15)<<"Result: " << setw(6)<<exponent(base,index,result);
cout << "\n----------------------";
cout << "\nPress any key to return to main menu";
getch();
break;
case '3':
system("CLS");
float A_side,B_side,c_side_result;
cout << fixed;
cout << setprecision(2);
hypotenuse_menu();
cout << "Enter A side: ";
cin >> A_side;
cout << "Enter B side: ";
cin >> B_side;
system("CLS");
hypotenuse_menu();
cout << setw(10)<<"Result: " << setw(6)<<hypotenuse(A_side,B_side,c_side_result);
cout << "\n----------------------";
cout << "\nPress any key to return to main menu";
getch();
break;
case '4':
system("CLS");
float velo, initial, acceleration, time;
cout << fixed;
cout << setprecision(2);
velocity_menu();
cout << "Enter initial velocity: ";
cin >> initial;
cout << "Enter acceleration : ";
cin >> acceleration;
cout << "Enter time : ";
cin >> time;
system("CLS");
velocity_menu();
cout << setw(10)<<"Result: " << setw(6)<<velocity(velo,initial,acceleration,time);
cout << "\n----------------------";
cout << "\nPress any key to return to main menu";
getch();
break;
case '5':
system("CLS");
float mass_value, speedlight_value, energy;
cout << fixed;
cout << setprecision(2);
mass_menu();
cout << "Enter Mass Value: ";
cin >> mass_value;
cout << "Enter Speed of light value : ";
cin >> speedlight_value;
system("CLS");
mass_menu();
cout << setw(10)<<"Result: " << setw(6)<<mass_en(mass_value, speedlight_value, energy);
cout << "\n----------------------";
cout << "\nPress any key to return to main menu";
getch();
break;
case '6':
system("CLS");
float percent, number, base_per;
cout << fixed;
cout << setprecision(2);
mass_menu();
cout << "Type the number: ";
cin >> number;
cout << "Type the base: ";
cin >> base_per;
system("CLS");
percentage_menu();
cout << setw(10)<<"Result: " << setw(6)<<percentage(percent, number, base_per);
cout << "\n----------------------";
cout << "\nPress any key to return to main menu";
getch();
break;
case '7':
system("CLS");
cout << fixed;
cout << setprecision(2);
cout << "---------------------------\n";
cout << " MEMBER \n";
cout << "Nattapon Sripradub M.4/1 No.14\n";
cout << "Natnicha Nuangsupthawee M.4/1 No.22\n";
cout << "Panicha Pornpattarapon M.4/1 No.24\n";
cout << "\n----------------------";
cout << "\nPress any key to return to main menu";
getch();
break;
}
}while(choosechoice!= 'X' && choosechoice != 'x');
return 0;
}

C++ codeblock Linux console menu

I am trying to build a menu on console on C++ with CodeBlock. Is for a course.
I would like to validate garbage enter by the user. For example, the user have to enter a number. If he enters a wrong number, no problem, the program work and continue. But if He enters a letter or some garbage, the program start on infinite loop.
I cannot use system(PAUSE) because I am programming on Linux.
I tried some code like cin.get() or do while with cin.get() but no result.
Here is my code :
#include <iostream>
using namespace std;
void showMenu()
{
cout << "---------------MENU --------------" << endl;
cout << "1- Check balance :" << endl;
cout << "2- Check deposit :" << endl;
cout << "3- Withdraw:" << endl;
cout << "4- Exit" << endl;
cout << "--------------------------------" << endl;
}
int main()
{
int option;
double balance = 500;
do
{
showMenu();
cout << "Option: ";
cin >> option;
cout << "\033[2J\033[1;1H";
switch(option)
{
case 1:
cout << "Balance is: " << balance << " $" << endl;
break;
case 2:
cout << "Deposit amount: " << endl;
double depositAmount;
cin >> depositAmount;
balance += depositAmount;
break;
case 3:
cout << "Withdraw amount: " << endl;
double withdrawAmount;
cin >> withdrawAmount;
if (withdrawAmount <= balance) {
balance -= withdrawAmount;
}
else {
cout << "Not enough money" << endl;
}
break;
default:
cout << "Your choice is invalid ";
do {
cout << '\n' << "Press the Enter key to continue.";
}while (cin.get() != '\n');
}
} while(option != 4);
return 0;
}
Do you have an idea how can I validate easily the garbage enter by the user ?
Thank you for your Help
It works with the code on default part : cin.clear and cin.ignore(). This last one is important. Thank you to user4581301
#include <iostream>
using namespace std;
void showMenu()
{
cout << "---------------MENU --------------" << endl;
cout << "1- Check balance :" << endl;
cout << "2- Check deposit :" << endl;
cout << "3- Withdraw:" << endl;
cout << "4- Exit" << endl;
cout << "--------------------------------" << endl;
}
int main()
{
int option;
double balance = 500;
do
{
showMenu();
cout << "Option: ";
cin >> option;
cout << "\033[2J\033[1;1H";
switch(option)
{
case 1:
cout << "Balance is: " << balance << " $" << endl;
break;
case 2:
cout << "Deposit amount: " << endl;
double depositAmount;
cin >> depositAmount;
balance += depositAmount;
break;
case 3:
cout << "Withdraw amount: " << endl;
double withdrawAmount;
cin >> withdrawAmount;
if (withdrawAmount <= balance) {
balance -= withdrawAmount;
}
else {
cout << "Not enough money" << endl;
}
break;
default:
cout << "\033[2J\033[1;1H"; //for clean screen with Linux
cout << "Your choice is invalid " << endl;
cin.clear();
cin.ignore();
}
} while(option != 4);
return 0;
}

Can Xcode mix buffered and non-buffered input functions?

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";
}

Line 48 and 62 C++ error Expected unqualified id before if?

I am programming a simple calculator that subtracts adds divides or multiplies depending on whether you type 1 2 3 or 4. I keep getting this error. Keep in mind I am newer to C++. It happens with the IF lines in Mode == 3 and Mode == 4
#include <iostream>
int main(){
using namespace std;
int x;
int y;
int x2;
int y2;
int x3;
int y3;
int x4;
int y4;
int Mode;
cout << "Welcome to Brian's Calculator!";
cout << endl;
cout << "Pick a mode. 1 is Addition. 2 Is Subtraction. 3 is Multiplacation. 4 is Division";
cin >> Mode;
if (Mode==1){
cout << "You chose addition.";
cout << endl;
cout << "Pick a number.";
cout << endl;
cin >> x;
cout << endl;
cout << "Pick another.";
cout << endl;
cin >> y;
cout << "The sum of the numbers you chose are: " << x+y <<".";
return 0;
};
if (Mode==2){
cout << "You chose subtraction.";
cout << endl;
cout << "Pick a number.";
cout << endl;
cin >> x2;
cout << endl;
cout << "Pick another.";
cout << endl;
cin >> y2;
cout << "The difference of the numbers you chose are: " << x2-y2 <<".";}
return 0;
};
if (Mode==3){
cout << "You chose Multiplacation.";
cout << endl;
cout << "Pick a number.";
cout << endl;
cin >> x3;
cout << endl;
cout << "Pick another.";
cout << endl;
cin >> y3;
cout << "The product of the numbers you chose are: " << x3*y3 <<".";
return 0;
};
if (Mode==4){
cout << "You chose Division.";
cout << endl;
cout << "Pick a number.";
cout << endl;
cin >> x4;
cout << endl;
cout << "Pick another.";
cout << endl;
cin >> y4;
cout << "The quotient of the numbers you chose are: " << x4/y4 <<".";
return 0;
};
At this line:
cout << "The difference of the numbers you chose are: " << x2-y2 <<".";}
You have an extra }
PROBLEM: you have two end-braces instead of one:
if (Mode==2){
...
// DELETE THE EXTRANEOUS "}"!
cout << "The difference of the numbers you chose are: " << x2-y2 <<".";}
return 0;
};
SUGGESTED ALTERNATIVE:
if (Mode==2){
...
// DELETE THE EXTRANEOUS "}"!
cout << "The difference of the numbers you chose are: " << x2-y2 <<".";
return 0;
}
EVEN BETTER:
switch (Mode) {
case 1 :
...
break;
case 2 :
...
break;
On this line you have a misplaced }:
cout << "The difference of the numbers you chose are: " << x2-y2 <<".";}
^
when you fix that you will need an extra } after the end of your program to close main as well. Also you do not need a ; after the } when you close the if statements for example:
if (Mode==1){
// code...
};
^