Thread: Lesson 4 of Tutorial

  1. #1
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50

    Question Lesson 4 of Tutorial

    Hey guys. Im pretty new to the site and just tryin to work through the tutorials. I have found them difficult but haven't had a big problem understanding them until lesson 4. Ill just copy the bit I dont get.

    <<int a;
    a=random(5);>>

    Do not think that 'a' will change at random, it will be set to the value returned when the function
    is called, but it will not change again.

    I dont c how a=random(5) works so could anyone explain it better plz? also the (5) has me confused
    thankz cya
    Last edited by polonyman; 09-09-2004 at 02:03 AM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The random function returns a random number. It is used when you want to set a variable to something random. Once the variable is set by the random function it will not change on its own.

  3. #3
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50
    also im havin sum throuble with another bit of lesson 4

    << #include <iostream.h>
    int mult(int x, int y);
    int main()
    {
    int x, y;
    cout<<"Please input two numbers to be multiplied: ";
    cin>>x>>y;
    cout<<"The product of your two numbers is "<<mult(x, y);
    return 0;
    }
    int mult(int x, int y)
    {
    return x*y;
    } >>

    basically i dont c y int mult(int x, int y); is at the top and bottom only the bottom 1 doesnt have semicolons. be nice if u could explain it, cya
    Last edited by polonyman; 09-09-2004 at 02:11 AM.

  4. #4
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50

    Angry more stupid probs

    hey, i edited the program, have a look

    <<#include <iostream.h>
    int mult(int x, int y);
    return x*y;
    int main()
    {
    int x, y;
    cout<<"Please input two numbers to be multiplied: ";
    cin>>x>>y;
    cout<<"The product of your two numbers is "<<mult(x, y);
    return 0;
    }
    >>

    it works exactly the same. and it still wont work with cin.get(); to stop it disapearing. where do i put it or what else do i use?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by polonyman
    basically i dont c y int mult(int x, int y); is at the top and bottom only the bottom 1 doesnt have semicolons. be nice if u could explain it, cya
    When the compiler gets to a piece of code that calls a function it likes to know a little about the function being called so it can do proper type checking of the arguments at that moment. In order to do this it either needs to have already read through the whole definition of the function before it reaches that particular part of the code, or it at least needs to know how the function gets called and what type of arguments it takes and what type of value it may return. The second method is done by the use of a function prototype which is demonstrated in that section of code you had. It typically looks just like a function definition except it is missing the body of the function and has the semicolon at the end. This is just letting the compiler know that "hey I'm going to be calling a function called mult later on and it takes two int arguments and it returns an int value as its result.

    So, you can either fully define the function prior to its being called like this:
    Code:
    #include <iostream>
    
    // Function fully defined prior to its being called below, no prototype needed
    int mult(int x, int y)
    {
        return x*y;
    } 
    
    int main()
    {
        int x, y;
        std::cout<<"Please input two numbers to be multiplied: ";
        std::cin>>x>>y;
        std::cout<<"The product of your two numbers is "<< mult(x, y);
        std::cin.get();
        std::cin.get();
        return 0;
    }
    Or you would need the function prototype:
    Code:
     #include <iostream>
    
    // Function prototype needed otherwise compiler won't know about function
    // when it gets to the calling of it in the code below
    int mult(int x, int y);
    
    int main()
    {
        int x, y;
        std::cout<<"Please input two numbers to be multiplied: ";
        std::cin>>x>>y;
        std::cout<<"The product of your two numbers is "<<mult(x, y);
        std::cin.get();
        std::cin.get();
        return 0;
    }
    
    int mult(int x, int y)
    {
        return x*y;
    }
    Quote Originally Posted by polonyman
    it still wont work with cin.get(); to stop it disapearing. where do i put it or what else do i use?
    See either of the above examples.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Before you invent another creative way of highlighting code, read this
    http://cboard.cprogramming.com/showthread.php?t=13473
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My new website
    By joeprogrammer in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 03-17-2006, 07:38 PM
  2. Cprog tutorial: Design Patterns
    By maes in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 01:41 AM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Error with Lesson 9 of the tutorial
    By Sugapablo in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 07:37 AM
  5. Problem with tutorial (Vector class)
    By OdyTHeBear in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2002, 02:49 PM