always returning 466750944 in c++ calculations script - c++

#include <iostream>
using namespace std;
multiplication() {
int x;
int y;
int sum;
sum = y * x;
cout << "multiplication" << endl;
cout << "enter first number for multiplication: ";
cin >> x;
cout << "enter second number for multiplication: ";
cin >> y;
cout << "your product is: " << sum <<endl;
return 0;
}
void division (){
cout << "division" << endl;
}
void addition (){
int y;
int x;
int sum = x * y;
cin >> x;
cin >> y;
cout << sum;
}
void subtraction (){
}
int main()
{cout << "enter 1 for multiplication, enter 2 for division, enter 3 for addition, and enter 4 for subtraction"<<endl;
int math;
cin >> math;
switch(math){
case 1:
multiplication();
break;
case 2:
division();
default:
cout << "it dont work ooga booga"<<endl;
break;
case 3:
addition ();
break;
case 4:
subtraction();}
return 0;
}
this is the script i am trying to run, im running in code::blocks if something is wrong that would cause it to always return 466750944 please tell me so i could work more on this, this may be my a problem with codeblocks if someone could also run this script in codeblocks or another ide and post their results it would be very much appreciated, thank you

When you say sum = x * y that is evaluated at the point of definition, it's not a formula as in math where later on it's evaluated when rendered.
When the sum = x * y statement is executed, x and y are not initialized so the value of sum is basically garbage.
To see this behaviour in action, step through your code in a debugger and look at the values of x, y and sum.
Either move that to after x and y are properly defined, or move it to a function, like:
int sum(int x, int y) {
return x * y;
}

Related

The function being called to sum or get product is incorrect

When I run this function the sum or the product is grossly wrong. I put in 2 and 3 for the inputs and got like a negative a million. The same is for the product too. Im going to add another cout statement after i ask for which calculation they want to do to make it more natural.
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
class basicCalculator {
public:
int add(int x,int y) {
return add1 + add2;
}
int multiply(int x,int y) {
return multiply1*multiply2;
}
private:
int add1;
int add2;
int multiply1;
int multiply2;
};
int main() {
cout << "What mathematical action do you want?" << endl;
cout << "Press '1' to add two numbers, '2' to multiply two numbers" << endl;
int method;
cin >> method;
int value1;
cin >> value1;
int value2;
cin >> value2;
basicCalculator bc;
switch (method) {
case 1:
cout << "The sum is " << bc.add(value1, value2) << endl;
break;
case 2:
cout << "The product is " << bc.multiply(value1, value2) << endl;
}
}
Inside your add and multiply methods you are using (uninitalized) member variables instead that the actual arguments.
Try with:
int add(int x, int y) { return x + y; }
A suggestion: add and multiply don't need an object state at all, they could be static, I don't see any reason for all the member variables you declared.
Freddy, in your functions you must use the named parameters for your calculations.
So, NOT
int add(int x,int y) {
return add1 + add2;
}
but rather
int add(int x,int y) {
return x + y;
}
Same problem with multiply.

How do I prevent this loop? C++

I'm new to C++ and stack overflow in general so please excuse me if a make a mistake somewhere.
I posted my code down below, but my issue is that when I type either yes or no at after the calculation is complete, no is supposed to end the program (which I was still working on) and yes is supposed to set it up for another calculation.
However I end up with a glitchy loop.
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
bool b;
bool yes = b;
do {
float x;
float y;
float z;
float a;
cout << "Enter The amount you are investing:" << endl;
cin >> x;
cout << "Enter the rate:" << endl;
cin >> y;
cout << "Enter the investment period (years):" << endl;
cin >> z;
cout << "Enter the compounding period:" << endl;
cin >> a;
cout << pow((1 + y / a), (a*z))*x << endl << "Want to do another? (yes/no)";
cin >> b;
cin.ignore();
} while (yes = true); {
cin.clear();
if (b = yes) {
}
else {
}
}
return 0;
}
The behaviour of your code is probably due to:
unintentional reassignment of the termination condition bool value: yes to: true, instead of checking its value, which is done with ==, not with the assignment =.
no modification of the value yes within the while loop.
A possible update is:
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
// initialise the sentinel
bool yes = true;
do {
// define variables
float x, y, z, a;
// read input
cout << "Enter The amount you are investing:" << endl;
cin >> x;
cout << "Enter the rate:" << endl;
cin >> y;
cout << "Enter the investment period (years):" << endl;
cin >> z;
cout << "Enter the compounding period:" << endl;
cin >> a;
cout << pow((1 + y / a), a * z) * x << endl;
// redo calculation or exit
cout << "Want to do another? (yes/no)";
cin >> yes;
// check termination condition
} while (yes == true);
return 0;
}
Additionally, watch out for the uninitialised variables: x, y, z, a and think for a proper default value that will indicate possible wrong result.
Lastly, withing the calculation: 1 + y / a is ambiguous, it could mean both: (1 + y) / a and: 1 + (y / a), put parentheses to enforce precedence in the wanted order.
You are not modifying the value of variable yes. It is always set to true.

C++ functions won't return values

#include<iostream>
#include<cstdlib>
#include<string>
#include<time.h>
using namespace std;
//Functions
// player strategy
int strategy(int user1Strat, int user2Strat);
// player total score per round
int currentScore();
// Display game result
void printResults();
int main()
{
int total_player1 = 0; // player 1 current score
int total_player2 = 0; // player 2 current score
int player1_strat= 0; //player 1 strategy for each turn
int player2_strat = 0; // player 2 strategy for each turn
// seed the random number generator.
srand(static_cast<int> (time(NULL)));
// get strategy for each player using functions <strategy>
strategy(player1_strat, player2_strat);
cout << player1_strat << endl << player2_strat << endl;
system("pause");
return 0;
}
int strategy(int user1Strat, int user2Strat)
{
int x,
y;
cout << "Enter player1's roll until strategy: ";
cin >> user1Strat;
cout << "Enter player2's roll until strategy: ";
cin >> user2Strat;
x = user1Strat;
y = user2Strat;
return x, y;
}
While calling for function strategy in the function main it will execute how it should, but once I ask to return the value it will just return
Enter player1's roll until strategy: 10
Enter player2's roll until strategy: 5
0
0
press any key to contiue...
Does anyone know why this is happening or what is causing it, was my error in the strategy function? Or upon calling it?
strategy(player1_strat, player2_strat); in your main() do nothing after receiving inputs so you won't see any change on player1_strat and player2_strat.
If you want to modify player1_strat and player2_strat in strategy, you could do that by referencing:
void strategy(int& user1Strat, int& user2Strat)
{
cout << "Enter player1's roll until strategy: ";
cin >> user1Strat;
cout << "Enter player2's roll until strategy: ";
cin >> user2Strat;
}
or you could return "multiple value" by using std::pair:
//#include <utility>
std::pair<int, int> strategy(int user1Strat, int user2Strat)
{
int x, y;
cout << "Enter player1's roll until strategy: ";
cin >> user1Strat;
cout << "Enter player2's roll until strategy: ";
cin >> user2Strat;
x = user1Strat;
y = user2Strat;
return std::make_pair(x, y);
}
//main()
std::pair<int, int> result = strategy(player1_strat, player2_strat);
x = result.first;
y = result.second;
You can only return a single object from a function. return x, y; does not return x and y but only y. If you want to update multiple variables, give them to the function as references and change their value inside the function.
Edit:
As mentioned by #Keith Thompson in the comments, the comma actually is an operator in this statement. It evaluates x (not much to do there), discards the result and then evaluates and returns the second argument y.

Adding a Probability Equation Into the Program

I've been working on this program in which it should calculate the probability based on the following formula:
𝑃(𝑥) = (𝑁!) / (𝑥!) * (𝑁−𝑥)!) * (p^x) * ((1-p)^(N-x))
Also, when the user types in a value, N must be an integer, x must be an integer which can be between 0 and N, and p must be a positive real number between 0 and 1. Till now this part works just fine but I don't know how to properly add the probability formula in the program.
The following is my code so far:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
long int factorial (int N, int x, int p);
int main ()
{
double N, x, p;
cout << "Input N value" << endl;
cin >> N;
cout << "Input x Value" << endl;
cin >> x;
while(x<=0 || x>=N){
cout << "x value is NOT between 0 and N." << endl;
cout << "Input x Value" << endl;
cin >> x;
}
cout << "Input p value" << endl;
cin >> p;
while(p<=0 || p>=1){
cout << "p value is NOT a real number between 0 and 1." << endl;
cout << "Input p value" << endl;
cin >> p;
}
return 0;
}
Can anyone help me out just to understand how to properly add an equation in my program?
Thank you!
This is my new code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double factorial (double N, double x, double p);
int main ()
{
double N;
double x;
double p;
cout << "Input N value" << endl;
cin >> N;
cout << "Input x Value" << endl;
cin >> x;
while(x<=0 || x>=N){
cout << "x value is NOT between 0 and N." << endl;
cout << "Input x Value" << endl;
cin >> x;
}
cout << "Input p value" << endl;
cin >> p;
while(p<=0 || p>=1){
cout << "p value is NOT a real number between 0 and 1." << endl;
cout << "Input p value" << endl;
cin >> p;
}
double Probability;
Probability = factorial(N, x, p);
cout << "Probability= " << Probability << endl;
return 0;
}
double factorial (double N, double x, double p){
double answer = ((tgamma(N+1))/((tgamma(x+1)) * (tgamma((N-x)+1)))) * (pow(p,x)) * (pow((1-p),(N-x)));
return answer;
}
The program recognizes the values I put in the system but when it calculates the answer, it gives a really small number. I tried out each section of the formula to make sure their was not a mistake but everything works fine when I tested it independently. Does anyone know what's wrong with the equation?
Thank you!
First you need to write a factorial function, check out this stackoverflow link:
How do you implement the factorial function in C++?
Then just write a function for your calculation. Assuming your factorial function is called getFact(int n) then:
double solve(int N, int x, double p) {
double answer = ( getFact(N)/getFact(x) )*getFact((N-x))* pow(p,x)* pow((1-p),(N-x));
return answer;
}
Then call the solve function in your main after having set your values.
double P_x;
P_x = solve(N,x,p);
Also, I use doubles because they can be more accurate, especially for p since its is 0 <= p <= 1.

Returning value from function into a value being tested by a while loop. C++

I am creating a basic calculator program using constructors by making classes in other ".cpp" files and calling them in header files. The program worked perfectly fine the way I wrote it. I decided to then add a question at the end asking if the user would want to continue by pressing "c" or "C". I created a function to test for this. I am able to return two different values based on if c was pressed or if it was not. How do I assign that value to "x" which is being tested in the while loop? I want this to either end the program or continue it.
I did not include the header files or ".cpp" files necessary for the constructors, but I don't think they are necessary to find how to set x to the returned value of the function.
#include <iostream>
#include "Add.h"
#include "Sub.h"
#include "Mult.h"
#include "Div.h"
using namespace std;
int cont(char input);
int main()
{
char operatr;
int x = -2;
char y;
while (x = 1) {
cout << "Enter operator (+, -, *, or /): ";
cin >> operatr;
if(operatr =='+'){
Add ob;
cout << "Would you like to continue? (Press \"c\" to continue and any key to escape.)";
cin >> y;
cout << (cont(y));
x = (cont(y));
}
if(operatr== '-'){
Sub ob;
cout << "Would you like to continue? (Press \"c\" to continue and any key to escape.)";
cin >> y;
cout << (cont(y));
x = (cont(y));
}
if(operatr=='*'){
Mult ob;
cout << "Would you like to continue? (Press \"c\" to continue and any key to escape.)";
cin >> y;
cout << (cont(y));
x = (cont(y));
}
if(operatr=='/'){
Div ob;
cout << "Would you like to continue? (Press \"c\" to continue and any key to escape.)";
cin >> y;
cout << (cont(y));
x = (cont(y));
}
}
return (0);
}
int cont(char input) {
int a;
switch(input) {
case 'c':
a = -2;
break;
case 'C':
a = -2;
break;
default:
a = 0;
break;
}
return(a);
}
Couple of issues:
You're testing x being equal 1 in the loop but using -2 in your conf(int) method.
Your while loop is actually not testing x bit "assigning" 1 to x.
You meant to do:
while (x == -2)
You could also.just save on method calls to #cont with:
x = cont(y);
cout << x;