+ Reply to Thread
Results 1 to 4 of 4

Thread: Multiple functions in a C++ program

  1. #1
    Guru nicckk has a spectacular aura about nicckk has a spectacular aura about nicckk's Avatar
    Join Date
    Oct 2008
    Location
    San Diego
    Posts
    571

    Multiple functions in a C++ program

    This is my first day of C++ and I was wondering how to get multiple functions in a C++ program. Any help would be appreciated. Here is my source.

    Code:
    // Value selector
    // Nick Dichev 2009
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
        char number;
        cout << "Please input a value\n"; 
        cin >> number;
        cout << " You selected: " << number; 
        second(); // This should work in a C program, how do I do it in C++
        system("PAUSE");
        return 0;
    }
          
    int second() // This is where I run into problems
    {
        char number;
        cout << "Please select another value...";
        cin >> number;
        cout << "You selected: " << number;
    }
    Thanks

  2. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,689
    Blog Entries
    57

    Re: Multiple functions in a C++ program

    You have to declare second() before you can call it.
    Code:
    // Value selector
    // Nick Dichev 2009
    
    #include <iostream>
    using namespace std;
    
    int second() ;
    int main()
    {
        char number;
        cout << "Please input a value\n"; 
        cin >> number;
        cout << " You selected: " << number; 
        second(); // This should work in a C program, how do I do it in C++
        system("PAUSE");
        return 0;
    }
          
    int second() // This is where I run into problems
    {
        char number;
        cout << "Please select another value...";
        cin >> number;
        cout << "You selected: " << number;
    }
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  3. #3
    Guru nicckk has a spectacular aura about nicckk has a spectacular aura about nicckk's Avatar
    Join Date
    Oct 2008
    Location
    San Diego
    Posts
    571

    Re: Multiple functions in a C++ program

    Thanks Winged, that worked. But the compiler then told me I needed a semi-colon before the int main(). I plugged it in and it worked fine. Can you describe why? I thought that a semi-colon ended a line of code?

    Thanks

  4. #4
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,689
    Blog Entries
    57

    Re: Multiple functions in a C++ program

    Code:
    int second();
    declares a function, and must end with a semi-colon, just like any other declaration (such as int
    Code:
    i;
    ).
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Replies: 8
    Last Post: 12-16-2009, 01:53 PM
  2. Tutorial: Starting C# with C# 2008 Express Edition
    By Jordan in forum CSharp Tutorials
    Replies: 20
    Last Post: 07-27-2009, 04:45 AM
  3. Debugging a C++/C File with GDB Debugger
    By awesome001 in forum C and C++
    Replies: 2
    Last Post: 01-01-2009, 07:07 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts