Thread: Converting an int main function to void()

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    36

    Converting an int main function to void()

    I really do need help and am trying my hardest to learn. I hope I'm not annoying anyone too much here, anyways.....

    I need to write a program that will be able to determine whether or not the function is a prime number, and if so or not, it will say, "XXX is / is not a prime number" I was able to get the program the exact way its supposed to run but he wants us to use int Prime (void).

    My question is since i have the following program, how do I go about converting this program to use the void function.

    Code:
    #include <iostream>
    using namespace std;
    
    int number;
    
    int main()
    {
    cout << "You have selected the prime option."
            << "\nEnter any number you wish to test.";
     cin >> number;
    if ((number % 2 ==0) || (number % 3 == 0) || (number % 5 == 0))
    cout << "Not a prime number. ";
    else if ((number % 2 !=0) || (number % 3 != 0) || (number % 5 != 0))
    cout << " " <<number << " is a prime number. ";
    				return 0;
    }
    OK, there ya go, all help is welcome and I am at your mercy

    -steve

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Is this what you mean by void function
    Code:
    #include <iostream>
    using namespace std;
    
    void primetest(int number);
    
    int main()
    {
    	int number;
    	cout << "You have selected the prime option."
    		<< "\nEnter any number you wish to test.";
    	cin >> number;
    	primetest(number);
    	return 0;
    }
    
    void primetest(int number)
    {
    	if ((number % 2 ==0) || (number % 3 == 0) || (number % 5 == 0))
    		cout << "Not a prime number. ";
    	else if ((number % 2 !=0) || (number % 3 != 0) || (number % 5 != 0))
    		cout << " " <<number << " is a prime number. ";
    }
    You are not checking wether the number is prime, but merely if it is divisble by 2, 3 or 5.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I was able to get the program the exact way its supposed to run but he wants us to use int Prime (void).
    Is this what you mean by void function
    Code:
    void primetest(int number)
    Here is a very simple function:
    Code:
    #include <iostream>
    using namespace std;
    
    void showNumber()
    {
       cout<<100<<endl;
    }
    
    int main ()
    {
       showNumber();
    
       return 0;
    }
    Functions must specify a 'return type', and the return type precedes the function name. In the function above, there is no return value, and that is designated by 'void'. The function is 'called' in main() by this line:

    showNumber();

    That says, "please go find a function named showNumber, and execute the lines of code contained therein."

    Here is another example:
    Code:
    #include <iostream>
    using namespace std;
    
    void showNumber(int a)
    {
       cout<<a<<endl;
    }
    
    int main ()
    {
       showNumber(100);
       showNumber(25);
    
       return 0;
    }
    Now, instead of always displaying the same number, you can send the function a specific number, and the function will display whatever number you send it. Once again, the function does not return a value, so its return type is 'void'. You should note that the type of the number you send to the function must match the function 'parameter'. The function parameter above is "int a", which means the function is expecting an int type to be 'sent' to it. You send a value to a function like this:

    showNumber(100);

    To send a value to a function, you just put the value between the parentheses after the function name. The function parameter "int a" creates an integer variable named "a" which stores the value you send to the function--that's why the type of the value you send has to match the function parameter. Then, inside the function you can use the variable "a" by referring to it by name:

    cout<<a<<endl;

    Here is another example:
    Code:
    #include <iostream>
    using namespace std;
    
    int addTen(int a)
    {
       int answer = a + 10;
       return answer;
    }
    
    int main ()
    {
       int result1 = addTen(2);
       int result2 = addTen(5);
    
       cout<<"Here are the results: "<<result1<<" "<<result2<<endl;
       
       return 0;
    }
    This time the function returns a value. The return type preceding the function name must match the type of the value in the 'return statement'. The return statement replaces the function call in main(), e.g. addTen(2), with the return value. So, result1 is assigned 12, and result2 is assigned 15.

    To summarize:
    1) A function must list a return type before its name.
    2) The return type must match the type of the value in the return statement--if there is no return statement, then the type is 'void'.
    3) When you call a function, the value you send it must match the function parameter.
    4) The function call in main() is replaced by the function return value.
    5) Sometimes if a function doesn't have any parameters, like in the first example, "void" will be used for the parameter. For instance you might see the function in the first example written like this:
    Code:
    void showNumber(void)
    {
       cout<<100<<endl;
    }
    However, unless typing the word 'void' gives you pleasure, you can omit it when it is a function parameter:
    Code:
    void showNumber()
    {
       cout<<100<<endl;
    }
    (void) is equivalent to ( ).
    Last edited by 7stud; 03-04-2006 at 02:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM