i started learning c++ few weeks ago, i am writting a program which will read 3 numbers and displays the smallest value
is this wrong:
if (num1 < num2) && (num1 < num3)
because that's where i am getting the error "expression syntax"
Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!
if((num1 < num2) && (num1 < num3)) { ... }
if(condition) { ... }
cout<< "(A)dd two numbers: "<<endl; cout<< "(S)ubtract two numbers: "<<endl; cout<< "(M)ultiply two numbers: "<<endl; cout<< "(D)ivide two numbers: "<<endl;
#include <iostream.h> //necessary float input1, input2, answer //declare calculation vars char input; //declare input var float add(); float subtract(); float multiply();//declare funtions. This could be collapsed into one statement float divide(); //"float add(), subtract(), multiply(), divide();" but not all compilers parse that. int main() { cout << "(A)dd two numbers: \n"; //prompt for input cout << "(S)ubtract two numbers: \n"; cout << "(M)ultiply two numbers: \n"; cout << "(D)ivide two numbers: \n"; cin >> input; if ((input == 'a') || (input == 'A')) //call the correct function add(); if ((input == 's') || (input == 's')) subtract(); if ((input == 'm') || (input == 'M')) multiply(); if ((input == 'd') || (input == 'D')) divide(); cout << "The answer is " << answer << ".\n"; cin.ignore(1,'\n'); //to view result return 0; } <<insert operation functions here>>Obviously, this doesn't include any error checking. The multiple if statements could also be replaced with a single switch( structure. You should really learn about it; it makes life easier in situations like these
#include <iostream> using namespace std; int main() { int choice, a, b, c; cout << "1. Add two numbers\n"; cout << "2. Subtract two numbers\n"; cout << "3. Multiply two numbers\n"; cout << "4. Divide two numbers\n\n"; cout << "Enter your selection: "; cin >> choice; cout << "Enter first number: "; cin >> a; cout << "Enter second number: "; cin >> b; if(choice == 1) c = a + b; if(choice == 2) c = a - b; if(choice == 3) c = a * b; if(choice == 4) c = a / b; cout << "The result is: " << c << endl; return 0; }
Edited by mpascal, 18 September 2009 - 05:39 PM.
Not quite sure I understand. I chose to use numbers as the selection method because it is much easier to handle: you don't have to worry about capital / lowercase. You can still use (A) or (a) for addition, but you would have to change the code slightly.why when you enter a number as a selection the program acts that you are selection from the above cout's ? is this just the syntax ?
A few ways to do this. Since A is always being divided by B, you can assume that B cannot be zero. You can either:+ how can i use the if (division by zero) in this code.
if(choice == 4) { if(b == 0) cout << "Error: Cannot divide by zero!"; else c = a / b; }
if((choice == 4) && (b != 0)) c = a / b;
input: 46 digit1: 4 digit2: 6 4 -> quarante 6 -> six therefore, you are left with "quarante six"Make any sense? There may be a better way to do this, but this is just what came to mind first.
0 members, 0 guests, 0 anonymous users
Community Forum Software by IP.Board
Licensed to: Geeks to Go, Inc.