+ 1
Can anyone help me in my first code
can any one tell be thw miatake i made in this code i cant find anything there is no output https://code.sololearn.com/cFJ0JqBqkj0A/?ref=app
8 Answers
+ 2
Something like this
#include <iostream>
using namespace std;
int main() {
int x, y, z;
string oper;
cin >> x;
cin >> y;
cin >> z;
cin >> oper;
if(oper == "sum")
cout << x+y+z;
else if(oper == "mult")
cout << x*y*z;
else if(oper == "div")
cout << x/(y/z);
else
cout << "Invalid operator";
return 0;
}
+ 2
this is my code
#include <iostream>
using namespace std;
int main() {
int x;
cin >> x;
int y;
cin >> y;
int z;
cin >> z;
int sum;
sum=(x+y+z);
int multiple;
multiple=(x*(y*z));
int divide;
divide=(x/(y/z));
int function;
cin >> function;
if(function==sum){
cout << sum <<endl;}
else{if(function==multiple){
cout << multiple << endl;}
else{
if(function==divide){
cout << divide << endl;
}
else(cout << "plz enter a function" << endl);}
}
return 0;
}
+ 2
For the most part your code is fine. The only issue is your last nested else statement:
else(cout << "plz enter a function" << endl);}
Organize your code lining up your closing curly braces directly below the beginning of your functions, if statements, loops etc, and use white space to help the readability of your code and these issues will be much easier to spot:
#include <iostream>
using namespace std;
int main() {
int x;
cin >> x;
int y;
cin >> y;
int z;
cin >> z;
int sum;
sum = (x+y+z);
int multiple;
multiple = (x*(y*z));
int divide;
divide = (x/(y/z));
int function;
cin >> function;
if(function == sum) {
cout << sum <<endl;
} else {
if(function == multiple) {
cout << multiple << endl;
} else {
if(function == divide) {
cout << divide << endl;
} else {
cout << "plz enter a function" << endl;
}
}
}
return 0;
}
+ 2
thank you very much for spending your valuable time in answering my questions
+ 1
so think I made a mistake
I was going to make a code Whit this input
5 7 8 sum
will give me the sum the 3 variables I was hoping to get it work but now from your answer I think I messed up everything
+ 1
I think I have made a mistake I was going to write a code with the input
3 6 7 sum .
will give me the sum of the 3 variable
but I think I messed it up can u suggest me a way
0
But with code u modified I can't still spot the error I am still getting no output
0
It's working fine for me. If you're using the playground are you putting all 4 inputs in at the same time at the prompt?
Example:
If you enter:
8
4
2
14
the output is 14 // sum
If you enter:
8
4
2
64
the output is 64 // multiple
If you enter:
8
4
2
4
the output is 4 // divide
and if you enter:
8
4
2
9 // or any other number besides 14, 64 or 4
for these x, y, z numbers
the output is "plz enter a function"