Thread: Help out a complete beginner

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    3

    Help out a complete beginner

    I started reading the using C++ tutorials a couple days ago, and have had no experience programming in any language before that. I'm attempting to write a very simple program that lets me choose between multiplication and division, then do that function to two numbers. what I have so far is this:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int mult ( int x, int y );
    
    int function; //the variable "function" is what chooses between multiply and divide
    int main()
    {
         cout<<"enter 1 for multiplication, 2 for division.";
         cin>>function; //my trouble is that it uses multiply regardless of what number you enter for "function"
    if (function = 1){ 
      int x;
      int y;
      
      cout<<"Please input two numbers to be multiplied: ";
      cin>> x >> y;
      cin.ignore();
      cout<<"The product of your two numbers is "<< (x * y) <<"\n";
      cin.get();
    }
    else if (function = 2) {
         int a;
         int b;
         cout<<"Please input two numbers to be divided: ";
         cin>> a >> b;
         cin.ignore();
         cout<<"the two numbers divided is " << (a / b) <<"\n";
         cin.get(); 
         }
         }
    can you guys help me out? it works for multiplying, but I can't get it to go to the second string for "if (function = 2). Any other code-writing tips would be helpful as well. thank you very much!

  2. #2
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    to check for equality use two equal signs == one equal sign means you are assigning the variable a value.
    integers are whole numbers. When you divide, you will need to use a variable of type double or float if you want to store the decimal fraction.

    don't forget to check for errors like divide by zero
    Last edited by manofsteel972; 07-28-2007 at 11:10 AM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use == for comparing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Aside from the integer division thing that you are facing...
    Code:
    if (function = 1){
    The code above assigns 1 to function, and then since 1 evaluates to true, the expression is true and so control passes to the code block that follows. What you probably meant to write is:
    Code:
    if (function == 1){
    Also, you should declare function within the main() function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    24
    Other than what everyone else said you also didn't use this line of code. it isn't really necessary to change but you can if you want
    Quote Originally Posted by Jmcrofts View Post
    int mult ( int x, int y );

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    3
    Thanks a lot everyone for your help, it seems to be working fine now.

    Quote Originally Posted by tbca View Post
    Other than what everyone else said you also didn't use this line of code. it isn't really necessary to change but you can if you want
    What does that code do? I saw it in the tutorials, but never really understood what it was for.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is the prototype for a function named mult that takes two arguments, each an int, and returns an int. The tutorial on functions should say more about it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    it's needed if you write your function after code that uses it. For single file projects, you don't really need it, but it comes in handy when you're working on larger projects.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. Complete Beginner [Need Help]
    By Vintik in forum C++ Programming
    Replies: 10
    Last Post: 08-15-2005, 05:08 PM
  3. Complete Beginner, HELP!!
    By britneyspy in forum C++ Programming
    Replies: 19
    Last Post: 06-12-2003, 11:01 AM
  4. Complete programming newbie - C or C++
    By Blobby in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 02-17-2003, 10:14 AM
  5. Doom Lord engine complete!
    By Leeman_s in forum Game Programming
    Replies: 8
    Last Post: 05-12-2002, 12:44 AM