simple ques, dont no wat these int are doing?

This is a discussion on simple ques, dont no wat these int are doing? within the C++ Programming forums, part of the General Programming Boards category; Hi, im very new to c++ and am trying to learn by actually studying code etc. I took this simple ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    22

    simple ques, dont no wat these int are doing?

    Hi, im very new to c++ and am trying to learn by actually studying code etc.
    I took this simple code from this tutorial website.
    Code:
    #include <iostream>
      
    int
    multiply(int x, int y)
    {
    return x*y;
    }
    int divide(int x, int
    y)
    { return x/y;}
    int add(int x, int y) 
    {
    return
    x+y;
    }
    int subtract(int x, int y)
    { return x-y;}
    
    using namespace std;
    
    int
    main(){ char op='c'; int x, y;
    while(op!='e') {
    
    cout<<"What operation would you like to perform: add(+),subtract(-), divide(/),multiply(*), [e]xit?"; 
    cin>>op;
    
    switch(op)
    {
    case '+':
    cin>>x;
    cin>>y;
    
    cout<<x<<"+"<<y<<"="<<add(x,
    y)<<endl;
    break;
    case '-':
    cin>>x;
    cin>>y;
    
    cout<<x<<"-"<<y<<"="<<subtract(x,
    y)<<endl;
    break;
    case '/':
    cin>>x;
    cin>>y;
    
    cout<<x<<"/"<<y<<"="<<divide(x,
    y)<<endl;
    break;
    case '*':
    cin>>x;
    cin>>y;
    
    cout<<x<<"*"<<y<<"="<<multiply(x,
    y)<<endl;
    break;
    case 'e':
    return 0;
    default:
    
    cout<<"Sorry, try again"<<endl;
    } }
    return
    0;}
    now, i know what nearly all of this is doing. except why the intergers are there at the start.
    Later ion in the program it use the case senario which i understand, then it goes on to tell the computer what to do in each case, i honestly dont no why they are there at the start.

    im sorry for this proabably being a really low question but i dnt no lol

  2. #2
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    which ints are you confused about? could you be more specific because there are a lot at the top of the program, lol

  3. #3
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    are you confused about the function calls to multiply and divide?

    Code:
    int multiply(int x, int y)
    {
    return x*y;
    }
    
    int divide(int x, int y)
    { 
    return x/y;
    }
    int add(int x, int y) 
    {
    return x+y;
    }
    int subtract(int x, int y)
    { 
    return x-y;
    }
    thats what I am getting from your question... and what the means is when you call the function it gives you back an integer. That way is I said

    Code:
    int i = add( 3, 4);

    it would return the integer 7.
    The ints inside the ()'s mean that you are telling the program that you are always sending it integers.


    Now I may have misread what you mean, so please let me know if I did misread it.

  4. #4
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    A method declaration is always like this

    Code:
    return type "name of function" ( as many as arguments as you like ) {
    
      return -whatever type you said it should return ;
    }
    so basically what

    Code:
    int multiply(int x, int y)
    {
    return x*y;
    }
    Means is that the return type of the function is an integer , the name of the function by what you call it from withing your main program or any other function or whatsoever is then multiply, between ( ) are the arguments

    so the function multiply takes two arguments two integers called x and y , then as i said in the beginning it returns an int, since x and y are both intīs we can easily say return x*y;

    You could also write it like this

    Code:
    int multiply(int x, int y)
    {
    int result;
    result = x*y
    return result;
    }
    Thats basically all about it ... I hope that answers your question ( didnt quite understand where you see any problem or what you dont understand ).

    ::edit::

    Didnt see dpro already answered your question actually ( its getting late , better go to sleep )
    Last edited by GanglyLamb; 04-19-2005 at 06:52 PM.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    22
    yeah, i c now
    where i saw

    cout<<x<<"-"<<y...etc

    i thought it was doing the work, , now ive just ran through it i no its just writing it on the screen

    sorry about that
    Last edited by jonezy; 04-20-2005 at 04:00 PM.

  6. #6
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Quote Originally Posted by jonezy
    yeah, i c now
    where i saw

    cout<<x<<"-"<<y...etc

    i thought it was doing the work, , now ive just ran through it i no its just writing it on the screen

    sorry about that
    Yeppers, any time you see anything in quotes, it will always be treated as a string and won't be parsed as a keyword/operator/anything else.

    Also, with the way cin is overloaded using the >> operator, you can actually do this:

    Code:
    cin >> x >> y >> z >> w;  //This allows the input for all 4 in one line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 11:28 PM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 10:16 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

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