+ 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

27th Jul 2017, 8:27 PM
Nikhil Robinson
Nikhil Robinson - avatar
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; }
28th Jul 2017, 5:55 AM
ChaoticDawg
ChaoticDawg - avatar
+ 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; }
27th Jul 2017, 8:39 PM
Nikhil Robinson
Nikhil Robinson - avatar
+ 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; }
27th Jul 2017, 9:57 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
thank you very much for spending your valuable time in answering my questions
28th Jul 2017, 5:58 AM
Nikhil Robinson
Nikhil Robinson - avatar
+ 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
28th Jul 2017, 5:26 AM
Nikhil Robinson
Nikhil Robinson - avatar
+ 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
28th Jul 2017, 5:31 AM
Nikhil Robinson
Nikhil Robinson - avatar
0
But with code u modified I can't still spot the error I am still getting no output
28th Jul 2017, 4:54 AM
Nikhil Robinson
Nikhil Robinson - avatar
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"
28th Jul 2017, 5:20 AM
ChaoticDawg
ChaoticDawg - avatar