This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Can you please fix this program. It will not cout z.
int main()
{
using namespace std;
int x, y, z, a;
cout << "Please enter a number" << endl;
cin >> x;
cout << "Please enter another number" << endl;
cin >> y;
cout << "What do you want to do with these numbers?" << endl;
cout << "1 = +" << endl;
cout << "2 = -" << endl;
cout << "3 = *" << endl;
cout << "4 = /" << endl;
cin >> a;
do {
z = add(x, y);
} while (a == 1);
do {
z = sub(x, y);
} while (a == 2);
do {
z = mul(x, y);
} while (a == 3);
do {
z = dis(x, y);
} while (a == 4);
cout << z;
return 0;
}
I tried using the do while statement, but I can not get it to work.
---EDIT---
Added proper indenting, why wont it do so atomatically?
Whichever option you enter, its going to enter that do-while loop and never exit because the condition will always be satisfied. Ex. if i enter 1 then it will enter :
do {
z = add(x, y);
} while (a == 1);
and since a will always be 1 it will never exit this. Same is the case with the other conditions. Instead you could use a switch statement. Something like :
switch(a) {
case 1 : z = add(x, y);
break;
case 2 : z = sub(x, y);
break;
case 3 : z = mul(x, y);
break;
case 4 : z = div(x, y);
break;
default : cout<<"Please choose a valid option to proceed.";
}
cout << z;
return 0;
Also, you do not use do-while in such cases. Even if you exit the loop, it will always divide(x, y) since do-while is such that it enters and then sub-sequently checks for the condition. You could however use a while loop something like this :
while(a==1) {
z = add(x, y);
a = -1;
}
while(a==2) {
z = sub(x, y);
a = -1;
}
while(a==3) {
z = mul(x, y);
a = -1;
}
while(a==4) {
z = div(x, y);
a = -1;
}
The statement:
while( 1 >= a <= 4)
is incorrect [well, it's correct C syntax, but it's most likely not what you wanted, since it does]:
while( (1 >= a) <= 4);
And since the result of (1 >= a) is either 0 or 1, it is always <= 4. You need to do:
while ( (1 >= a) && (a <= 4) );
[Parenthesis simply for clarity, you can remove the two inner sets of parenthesis]
Your code is entering an infinite loop, modify it like so
int main()
{
using namespace std;
int x, y, z, a;
do
{
cout << "Please enter a number" << endl;
cin >> x;
cout << "Please enter another number" << endl;
cin >> y;
cout << "What do you want to do with these numbers?" << endl;
cout << "1 = +" << endl;
cout << "2 = -" << endl;
cout << "3 = *" << endl;
cout << "4 = /" << endl;
cin >> a;
switch(a)
{
case 1:
z = add(x, y);
break;
case 2:
z = sub(x, y);
break;
case 3:
z = mul(x, y);
break;
case 4:
z = dis(x, y);
break;
}
cout << z;
} while( 1 >= a <= 4)
return 0;
}
modified as per lokoko's suggestion
Related
I have created a project that breaks a big number to its roots, it works very well, but it prints an extra * at the end of the last root.
#include <iostream>
using namespace std;
int multinum(int a, int b);
int primeOp(int a);
int main()
{
char ch;
do {
int a=0, b=0;
multinum(a,b);
cout << "\n\nDo you want to continue?(y/n)";
cin >> ch;
} while (ch=='y');
return 0;
}
int multinum(int num1, int num2)
{
cout<< "\nPlease enter the first number : ";
cin >> num1;
cout << "\nPlease enter the second number : ";
cin >> num2;
cout << num1 <<" = ";
primeOp(num1);
cout << endl;
cout << num2 <<" = ";
primeOp(num2);
return 0;
}
int primeOp(int a)
{
int i, x, power;
x=a;
if (a%2==0)
{
power=0 ;
while(a%2==0)
{
a/=2;
power++;
}
cout << 2 <<"^"<<power<< "*";
}
for (i=3; i<=x/2; i+=2)
{
power=0 ;
while(a%i==0)
{
a/=i;
power++;
}
if (power!=0)
cout << i <<"^"<< power << "*";
if (power!=0 && a%i== 0)
cout << "*";
}
if(a==x)
cout<< x << "^" << 1;
return 0;
}
I tried to print * in different ways but none of them had any effect, I also tried to stop printing by the use of the last "i" or "power" but it was useless.
What should I do, to stop the * bring printed when it's not needed?
Example: 24 = 2^3 * 3^1 * --- it should become: 24 = 2^3*3^1
To be able to print something only sometimes you need to print it under an if, and you need a condition that will control that print. A bool flag should do the trick. The other part of the trick is to print the asterisk before the next component, not after.
void PrintComponent(int root, int power, bool& printStar)
{
if (printStar)
cout << " * ";
cout << root << "^" << power;
printStar = true;
}
int primeOp(int a)
{
int i, x, power;
bool printStar = false;
x = a;
if (a % 2 == 0)
{
...
PrintComponent(2, power, printStar);
}
for (i = 3; i <= x / 2; i += 2)
{
...
if (power != 0)
PrintComponent(i, power, printStar);
}
if (a == x)
PrintComponent(x, 1, printStar);
return 0;
}
If finding the last print is not easy make the first print special.
Print the first power like this:
cout << 2 <<"^"<<power;
Then print all the rest via
cout << "*2^"<<power;
I dont understand your code fully, but to know it is the first print you can use a boolean flag.
You can suppres this issue by printing backspaces at the end of the result.
In primeOp add:
cout <<"\b \b";
Just above return statement
the problem that in the main body the getwhattheywant function is executing twice what I want is this
getwhattheywant execute then the user entered one then if the user entered one do the summation operation but what is happening with me that it's reasking the user to enter a number.
#include <iostream>
using namespace std;
double dothesum(int x, int y)
{
int sum = x + y;
return sum;
};
int getwhatheywant()
{
int choice;
cout << "1- for sum " << endl;
cout << "2- for quit ";
cin >> choice;
return choice;
}
void the_sum()
{
int x, y;
cout << " enter the first number " << endl;
cin >> x;
cout << " enter the second number " << endl;
cin >> y;
cout << " the sum of the two number is " << dothesum(x, y) << endl;
}
int main()
{
int;
while (getwhatheywant() != 2) {
if (getwhatheywant() == 1) {
the_sum();
}
}
return 0;
}
Change your main():
int main()
{
int whatTheyWant;
while ( (whatTheyWant = getwhatheywant()) != 2) {
if (whatTheyWant) == 1) {
the_sum();
}
}
return 0;
}
This stores the value from a single call to getwhattheywant() so you can first see if they're asking to quit, and if not, you can see what else they might want. Now, I'd write it slightly differently:
bool working = true;
while(working) {
int choice = getWhatTheyWant();
switch(choice) {
case 1: the_sum(); break;
case 2: working = false; break;
}
}
I have this problem, I would like to display that if 0/0, Output is that : "Cannot divide 0 by itself". How can I tweak my code so that I can display that output? If so, what code should I be using in order to make my goal come into fruition?
Here's my code below:
#include <iostream>
using namespace std;
double isAdd(double x, double y);
double isSub(double x, double y);
double isMult(double x, double y);
double isDiv(double x, double y);
int main() {
cout << "Calculator\n";
double oneV, twoV;
char choice = 'a';
cout << "Enter 2 Values : \n";
cin >> oneV;
cin >> twoV;
cout << "Enter Operation to Use: ( a / s / m / d) \n";
cout << "a = addition, s = subtraction, m = multiply, d = divide\n";
cin >> choice;
if (choice == 'a') {
cout << "The Sum is : " << isAdd(oneV, twoV);
}
if (choice == 's') {
cout << "The Difference is : " << isSub(oneV, twoV);
}
if (choice == 'm') {
cout << "The Product is " << isMult(oneV, twoV);
}
if (choice == 'd') {
cout << "The Quotient is " << isDiv(oneV, twoV);
}
}
double isAdd(double x, double y) {
double answer = x + y;
return answer;
}
double isSub(double x, double y) {
double answer = x - y;
return answer;
}
double isMult(double x, double y) {
double answer = x * y;
return answer;
}
double isDiv(double x, double y) {
double answer = x / y;
return answer;
}
if ('d' == choice) {
if (0 == twoV)
//cout << "Cannot divide 0 by itself\n";
cout << "Division by zero.\n";
else
cout << "The Quotient is " << isDiv(oneV, twoV);
}
Tips:
get rid of all of those ifs and use a switch.
Comparing to 0 will work in this case, but if you were working with a calculated value, you'd need to check for "nearly zero". See https://en.cppreference.com/w/cpp/types/numeric_limits/epsilon . This is due to floating point precision limitations.
If the user enters something other than a, s, m or d, your code silently exits. You should display something like "Invalid input."
You're also not taking into account input case: ie, A vs a.
if (choice == 'd')
{
if (twoV == 0)
{
//this one for cannot dividing 0
cout << "Undefined value (" << oneV << "/0)"<<endl;
}
else
{
//this one for normal other values
cout << "The Quotient is " << isDiv(oneV, twoV);
}
}
Tips:-
In here you use only conditional statements. But you can use switch case also. So, if you use them for implementing this it would be very attractive than using conditional statements. Refer them.
i want to use switch by i can't !!
#include <iostream>
using namespace std;
int main() {
char op ;
float x, y, z;
cout << "Enter the three angles : \n ";
cin >> x >> y >> z;
switch (op){
case '+' :
cout << x + y + z == 180;
break;
default:
cout << "A triangle is Not valid !! \n ";
}
system("pause");
}
You can use switch as a replacement for if like this:
#include <math.h>
switch(static_cast<int>(round(x + y + z))) {
case 180:
cout << "The triangle is valid\n";
break;
default:
cout << "The triangle is not valid\n";
break;
}
I use round() to mitigate the problem that floating point math is approximate.
If the assignment is to switch based on result Baramr's approach is what you want.
For completeness:
If the assignment is to switch based on op to learn fall through you'd probably use the break in a conditional and fall through to default, or go to a label if you have multiple ops.
Both fall through and explicit jumps are considered bad practice, but when you learn switch you should know they exist at the very least to debug them.
switch (op){
case '+' :
if( (static_cast<int>(round(x + y + z))) == 180 ){
cout << "The triangle is valid\n";
break;
}
default:
cout << "A triangle is Not valid !! \n ";
}
Bonus validation: Check that the angle values are not negative.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float x, y, z;
cout << "Enter the three angles : \n ";
cin >> x >> y >> z;
if (x <= 0 || y <= 0 || z <= 0) {
cout << "Invalid angles" << endl;
system("pause");
return 0;
}
switch (static_cast<int>(round(x + y + z))){
case 180:
cout << "Valid triangle" << endl;;
break;
default:
cout << "A triangle is Not valid !! \n ";
}
system("pause");
}
I am having this problem with my calculator I made. See, when I type in a calculation it always adds a 0 to the end. I don't know how to fix this do you have any ideas?
Here's the code:
#include <iostream>
using namespace std;
void Input(float &x, float &y);
float a = 1.0, b = 1.0, result;
char op;
int main() {
cout << "Welcome to Foxy's calculator" << endl;
cout << "----------------------------" << endl;
cout << "Please input a calculation operation (eg. 1+1): ";
cin >> a >> op >> b;
Input(a, b);
cout << result << endl;
system("pause");
return 0;
}
void Input (float &x, float &y) {
a = x;
b = y;
switch (op)
{
case '+':
cout << x + y;
break;
case '-':
cout << x - y;
break;
case '*':
cout << x*y;
break;
case '/':
cout << x / y;
break;
default:
cout << "Error! Operator is not correct" << endl;
cout << "Please input your calculation with a proper operator: ";
cin >> a >> op >> b;
}
}
result is a global static variable that gets zero - initialized and is never changed. So cout << result << endl; will always print "0". To fix this you should make a, b, result and op local to main (global variables are bad), pass a, b andop to calculating function and store returned calculation result in result. It will look something like this:
float result = Input(a, b, op);
cout << result << endl;
You call cout << result << endl; in the caller. and result is always 0. This is because it is never explicitly set to anything and the C++ compiler kindly zero-initialises it since it's at global scope.
In such instances, your line by line debugger is your best friend. The fact that you've mashed up your 1) input, 2) calculation, and 3) output stages is not helping: ideally they should all be separate parts of your program.
Remove cout << result << endl;