Jump to content

Welcome to Geeks to Go - Register now for FREE

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!

How it Works Create Account
Photo

c++ question


  • Please log in to reply

#1
ali.B

ali.B

    Trusted Helper

  • Malware Removal
  • 3,086 posts
hello,

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" :)
  • 0

Advertisements


#2
mpascal

mpascal

    Math Nerd

  • Retired Staff
  • 3,644 posts
I believe it should be:
if((num1 < num2) && (num1 < num3))
{
...
}

You need open and closing parentheses around the if statements, following the model:
if(condition)
{
...
}

Hope that helps :)
  • 0

#3
ali.B

ali.B

    Trusted Helper

  • Topic Starter
  • Malware Removal
  • 3,086 posts
Hello,

First thank you for replying

and yes this helped :)

another question.

i have this code:

cout<< "(A)dd two numbers: "<<endl;
cout<< "(S)ubtract two numbers: "<<endl;
cout<< "(M)ultiply two numbers: "<<endl;
cout<< "(D)ivide two numbers: "<<endl;


How can i make a condition for example if the user selected the first option it will prompt him/her for a action...
in another way : if ( decision ) i want the decision to be selecting one of the above
  • 0

#4
mpascal

mpascal

    Math Nerd

  • Retired Staff
  • 3,644 posts
If I understand what you're asking, try looking up on the "switch" statement in C++. You want the program to look at multiple conditions and select the correct one?
  • 0

#5
ali.B

ali.B

    Trusted Helper

  • Topic Starter
  • Malware Removal
  • 3,086 posts
let me try to be more clear, i want this menu to appear to the user:

(A)dd two numbers
(S)ubtract two numbers
(M)ultiply two numbers
(D)ivide two numbers

then

Then the program should read two numbers from the user, and display:
Their sum if the user typed a or A as a choice to the above menu
etc..

i am still a newbie with c++ didn't get yet to the switch part..im a practicing...
  • 0

#6
Chopin

Chopin

    Member 2k

  • Member
  • PipPipPipPipPip
  • 2,639 posts
I don't like endl;, I like \n :)

If you want to be really elementary (and rather inefficient) about it, you'd want to declare a char data type to hold the character and use multiple if statements to pass the program onto different functions. The size of the program could be reduced dramatically, but you said you were a beginner and I don't want to confuse you with strange syntaxes :)

#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 :)

(I took a basic C++ class at my school. They never taught us much C++; it was mostly C :) If there are any syntax errors, forgive me; probably me forgetting something basic like a semicolon. )
  • 0

#7
mpascal

mpascal

    Math Nerd

  • Retired Staff
  • 3,644 posts
OK, after the menu the user inputs a letter, and the program determines the operator (addition, subtraction, etc). Then the user inputs two numbers, and then the chosen operator is applied to them?
  • 0

#8
ali.B

ali.B

    Trusted Helper

  • Topic Starter
  • Malware Removal
  • 3,086 posts
exactly!
  • 0

#9
mpascal

mpascal

    Math Nerd

  • Retired Staff
  • 3,644 posts
OK, so something like this:
#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;
}

That should do it if you want it all in your main function. You can also split it up like Chopin did.

Edited by mpascal, 18 September 2009 - 05:39 PM.

  • 0

#10
ali.B

ali.B

    Trusted Helper

  • Topic Starter
  • Malware Removal
  • 3,086 posts
First thanks a lot.

i have a question

cout << "Enter your selection: ";

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 ?

+ how can i use the if (division by zero) in this code.

Thanks again for your patience and help.

thanks chopin too but i am still not ready to understand your code :)
  • 0

#11
mpascal

mpascal

    Math Nerd

  • Retired Staff
  • 3,644 posts

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 ?

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.

+ how can i use the if (division by zero) in this code.

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:
  • Use a nested "if" loop in division section such as:
    if(choice == 4)
    {
    if(b == 0)
    cout << "Error: Cannot divide by zero!";
    else
    c = a / b;
    }
  • Use a single for loop that is only executed if b is non-zero
    if((choice == 4)  && (b != 0))
    c = a / b;

  • 0

#12
ali.B

ali.B

    Trusted Helper

  • Topic Starter
  • Malware Removal
  • 3,086 posts
I am trying to solve this problem, using the switch annd if's

i must a program that takes a number from a user ( 1,2,3,4...till 100 ) and displays it in french... like 1 is un, 100 is cent.

the problem is i can't make 100 cases of course.

they key for solving is how numbers are written , for example 21 is twenty one ( 20 and 1 ) "vingt un" in french.. but i am having difficulties translating that idea into a code.
  • 0

#13
mpascal

mpascal

    Math Nerd

  • Retired Staff
  • 3,644 posts
Hi ali.b,

Well, one way you can do it is separate the integer into different parts: first digit and second digit (for numbers 10 - 99). For each digit you have 9 cases:

Digit One:
  • dix
  • vingt
  • trente
  • quarante
  • cinquante
  • soixant
  • soixant-dix
  • quatrevingt
  • quatrevingt-dix
Digit Two:
  • un
  • deux
  • trois
  • quatre
  • cinq
  • six
  • sept
  • huit
  • neuf
(forgive my spelling, I really can't spell in french :))

I believe what you should do is split the integer in two and make it the corresponding french digit. For example:

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






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP