Compiling all i understand so far

This is a discussion on Compiling all i understand so far within the C++ Programming forums, part of the General Programming Boards category; I made a simple program that encompasses all of the knowledge that I feel i understand so far. There is ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    35

    Compiling all i understand so far

    I made a simple program that encompasses all of the knowledge that I feel i understand so far. There is one problem though, I tried to break it up into different blocks but after the first block it dosnt seem to move onto the next. I was wondering if someone could help me get it to read from block to block.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int mult ( int x, int y );
    
    int main()
    {
        cout<<"I hope your are doing well today\n";        /*basic hello*/
    }
    
    int questioning()               /*input and if statement*/
    {
        char color;
    
            cout<<"What is your favorite color?\n";
            cin>>color;
            cin.ignore();
            cout<<"I wonder why your favorite color is" << color << "\n";
    
        char reply;
    
            cout<<"Are you sure your favorite color isn't Lavender?  (Y or N)\n";
            cin>>reply;
            cin.ignore();
            if ( reply == 'Y' || 'y' ) {
                cout<<"Your read my mind.\n";
            }
            else if ( reply == 'N' || 'n' ) {
                cout<<"I might just download you a virus, but as a second chance you can halp me solve a problem.\n";
            }
            cin.get();
    }
    
    int gears()            /*loops*/
    {
        cout<<"To help me switch gears.\n";
        cin.get();
    
        for ( int x = 0; x < 15; x++ ) {
            cout<<x<<endl;
        }
        cin.get();
    }
    
    int multiplecation()   /*funtions*/
    {
        int x;
        int y;
    
        cout<<"Can you please tell me two large numbers to divide.\n";
        cin>> x >> y;
        cin.get();
        cout<<"The divident of those numbers are" << ( x / y ) <<"\n";
        cin.get();
    }
    
    int mult ( int x, int y)
    {
        return ( x / y );
    }
    getting to switch blocks might be ahead of where im at but at the moment i am intrigued and would like to learn.

    it get to the program without errors but it just wont continue and stops after
    "I hope you are doing well today"
    any help is appreciated. thank you

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Functions need to be called somewhere (and the prototypes need to come before the place where they are used.

    Declare all the functions (except main) above main, as you have done with mult.

    Then call the functions from main in any order you like.

    Code:
    int main()
    {
        cout<<"I hope your are doing well today\n";        /*basic hello*/
        questioning(); 
        //etc
    
    }
    Also, if you say a function returns an int, you must do so. If you don't want to return an int from a function (or anything else) make the return type void:

    Code:
    void questioning()
    {
        //...
        //now it is OK not to return anything
    }
    main() is the only exception, since 0 will be returned by default if you don't specify otherwise.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    so if i change all of my

    Code:
    int questioning()
    int gears()
    //etc.
    to have void infront like

    Code:
    void questioning()
    void gear()
    the program should run all the way through or do i still have to name everything off before main()

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    No you need to leave it with int because that is defined return type of the function. The prototype is usually at the very top before main(). So yes, you need to name everything off before main().

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    so therefore if i leave it as is and name off all of the

    Code:
    int multiplication()
    int gears()
    //etc
    before the main(); i just have to put return() at the end of each block and that will let the program run all of the way through? or do i just name it all off before main() and thats it. i hope i am following this correctly.
    Last edited by brian75; 12-18-2008 at 07:52 PM.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    If you want to return something then have a return type of int. If you do that then you also need to put 'return variable' at the end of your function where variable is of type int. If you are not returning anything then put void as the return type and do not use return at the end of the function.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    well i wasnt 100% sure on what you were saying but i did change some things around so it will run through the whole program. I also added <string> so the program will repead the full word replies when asking for the favorite color.

    the new working code looks like this:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int mult ( int x, int y );
    
    int main()
    {
        cout<<"I hope your are doing well today\n";             /*basic hello*/
    
    
                   /*input and if statement*/
    {
        string color;
        color = color;
            cout<<"What is your favorite color?\n";
            cin>>color;
            cin.ignore();
            cout<<"I wonder why your favorite color is:  " << color << "\n";
    
        char reply;
    
            cout<<"Are you sure your favorite color isn't Lavender?  (Y or N)\n";
            cin>>reply;
            cin.ignore();
            if ( reply == 'Y' || reply == 'y' ) {
                cout<<"Your read my mind.\n";
            }
            else if ( reply == 'N' || reply == 'n' ) {
                cout<<"I might just download you a virus, but as a second chance you can halp me solve a problem.\n";
            }
            cin.get();
    }
    
                /*loops*/
    {
        cout<<"To help me switch gears.\n";
        cin.get();
    
        for ( int x = 0; x < 15; x++ ) {
            cout<<x<<endl;
        }
        cin.get();
    }
    
       /*funtions*/
    {
        int x;
        int y;
    
        cout<<"Can you please tell me two large numbers to divide.\n";
        cin>> x >> y;
        cin.get();
        cout<<"The divident of those numbers are:   " << ( x / y ) <<"\n";
        cin.get();
    }
    
    {cout<<"THANK YOU. Your are now safe from viruses.\n";   /*basic thank you*/
    }
    }
    
    int mult ( int x, int y)
    {
        return ( x / y );
    }
    not exactly what i wanted but it works

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    43
    The flow of a C++ program isn't to start at the top and work your way down. The program starts (and usually ends) in the main() function. That means that if you want something done after you print your first message, you need to specifically tell the computer to do so.

    Each of your "blocks" of code (called functions) need to be called in order for the program to execute them. So, for example, in order to have your questioning functioning do its thing, you need to call it like so:
    Code:
    int main()
    {
        cout<<"I hope your are doing well today\n";        /*basic hello*/
        questioning();
    }
    Functions can be called from any block of code - you can have gears() called from main(), or from questioning().

    Now, there are two other things you need to do. First, you need to declare your functions before main(). You did this for your mult() function. For your questioning() function, the first line in your definition is:
    Code:
    int questioning()
    You need to add that line (followed by a semi-colon) before your main() function. This tells the compiler that you have a function, called questioning, that takes no parameters, and returns an int, and that this function is defined later (that is, after main()). But here's the other part: your questioning() function does not return an int. When a function doesn't return anything, you need to declare it as returning type void. Now your function prototype looks like this:
    Code:
    void questioning();

  9. #9
    village skeptic
    Join Date
    Aug 2008
    Posts
    31
    Quote Originally Posted by Mostly Harmless View Post

    Now, there are two other things you need to do. First, you need to declare your functions before main(). You did this for your mult() function. For your questioning() function, the first line in your definition is:
    Code:
    int questioning()
    You need to add that line (followed by a semi-colon) before your main() function. This tells the compiler that you have a function, called questioning, that takes no parameters, and returns an int, and that this function is defined later (that is, after main()). But here's the other part: your questioning() function does not return an int. When a function doesn't return anything, you need to declare it as returning type void. Now your function prototype looks like this:
    Code:
    void questioning();

    What this person is saying - in a nutshell, is that functions generally have what are called 'declarations' at the top of the file to let the compiler know a little about the function - most importantly, that it exists. Functions can be 'defined' at the same time they are declared, but they can also be defined anywhere.

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    ok i get it know. that explained it well. Thank you.
    Last edited by brian75; 12-19-2008 at 02:34 PM.

  11. #11
    Registered User
    Join Date
    Feb 2004
    Posts
    29
    you see how it works?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int mult ( int x, int y );
    
    int questioning()               /*input and if statement*/
    {
        char color;
    
            cout<<"What is your favorite color?\n";
            cin>>color;
            cin.ignore();
            cout<<"I wonder why your favorite color is" << color << "\n";
    
        char reply;
    
            cout<<"Are you sure your favorite color isn't Lavender?  (Y or N)\n";
            cin>>reply;
            cin.ignore();
            if ( reply == 'Y' || 'y' ) {
                cout<<"Your read my mind.\n";
            }
            else if ( reply == 'N' || 'n' ) {
                cout<<"I might just download you a virus, but as a second chance you can halp me solve a problem.\n";
            }
            cin.get();
    }
    
    int gears()            /*loops*/
    {
        cout<<"To help me switch gears.\n";
        cin.get();
    
        for ( int x = 0; x < 15; x++ ) {
            cout<<x<<endl;
        }
        cin.get();
    }
    
    int multiplecation()   /*funtions*/
    {
        int x;
        int y;
    
        cout<<"Can you please tell me two large numbers to divide.\n";
        cin>> x >> y;
        cin.get();
        cout<<"The divident of those numbers are" << ( x / y ) <<"\n";
        cin.get();
    }
    
    int mult ( int x, int y)
    {
        return ( x / y );
    }
    int main()
    {
        cout<<"I hope your are doing well today\n";        /*basic hello*/
    	questioning();
    	gears();
    	multiplecation();
    	mult(2,5);
    }
    Now, in your main() function, you can do something like:

    Code:
    // Not real code!!!!
    
    cout<< "Choise 1,2,3,4,5?";
    cin>> int choose;
    
    switch(choose)
    {
    case '1':
    questioning();
    break
    
    case '2':
    mult(2,5);
    break
    Look up switch/case to see how to do it properly
    Last edited by pico; 12-23-2008 at 05:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Compiling Problem
    By Deronius in forum C++ Programming
    Replies: 3
    Last Post: 06-15-2008, 12:23 PM
  2. Problem compiling files that store functions
    By tiachopvutru in forum C++ Programming
    Replies: 10
    Last Post: 05-30-2008, 06:42 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Compiling in Unix vs Visual C++
    By stimpyzu in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2002, 07:41 AM
  5. Compiling syntax
    By Jez_Master in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2002, 09:46 PM

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