Calculations

This is a discussion on Calculations within the C++ Programming forums, part of the General Programming Boards category; I am trying to make a program where I ask the user to input data about an animal and then ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    545

    Calculations

    I am trying to make a program where I ask the user to input data about an animal and then it works out how likely they are to take over the world. How do you make a C++ Program do any sort of calculation: Multiply, Divide, Add, Subtract?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    All mathematical operators are standard in C and C++, just use them in your code.

    Code:
    int a = 5;
    int b = 2;
    int c = a + b;   // This will equal 7
    
    // Standard operators are:
    // for addition:         +
    // for subtraction:      -
    // for multiplication:   *
    // for division:         /
    // for modulus:          %
    Last edited by SlyMaelstrom; 11-08-2005 at 12:57 AM.
    Sent from my iPad®

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I take it you would use * for multiply and / for divide.

    Oh and can you use more than one if statement in a row?

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    You mean nested if statements? Like this:

    Code:
    if (conditions) {
       if (conditions) {
       }
    }
    Yes you can do this.
    Sent from my iPad®

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I know this is a very dum question but how do you show theanswer. I want to have it all in the sam line, The answer is.....

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    I'm having trouble with your questions. They're extremely vague.

    Ok, so you want to show the answer to an equation in output and you want it on the same line as text. A cout statement can accept any number of variables and constants, you just seperate each with "put to" operators and end the statement (like all statements) with a semicolon.

    Code:
    int x = 5;
    int y = 6;
    int z = x + y;
    
    cout << x << " + " << y << " = " << z << endl;
    
    */ or if you prefer */
    
    cout << "The answer is " << z << endl;
    If this isn't what you're asking, explain your question better.
    Sent from my iPad®

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    that is exactly what i was needing. sorry i am very vague.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    One of the problems was that I was posting using my PSP.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doing calculations on a data file
    By bassist11 in forum C Programming
    Replies: 2
    Last Post: 03-30-2009, 07:47 PM
  2. calculations with pointers
    By mackieinva in forum C Programming
    Replies: 2
    Last Post: 09-17-2007, 01:46 PM
  3. C program compile but not doing the calculations
    By abs.emailverify in forum C Programming
    Replies: 8
    Last Post: 11-08-2006, 07:43 AM
  4. simple arithmetic calculations
    By viciousv322 in forum C Programming
    Replies: 8
    Last Post: 11-18-2005, 06:13 AM
  5. How do I get these calculations correct?
    By nadeni0119 in forum C++ Programming
    Replies: 10
    Last Post: 04-07-2003, 11:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21