Thread: void function that builds but error when trying to run

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    73

    void function that builds but error when trying to run

    here is my error: LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    Debug/hw07.exe : fatal error LNK1120: 1 unresolved externals

    The program will build but when I try to run it I get that error.

    Problem:

    Write a void function that reads in a specified number of float values and returns their average. A call to this function might look like
    GetMeanof (5, mean)
    where the first argument specifies the number of values to be read, and the second argument contains the results. Document the data flow of each parameter with /* in */, /* out */, or /* inout */.

    here is what I have so far. what does this error mean?

    thanks

    Bryan

    Code:
    #include <iostream>
    
    using namespace std;
    
    void GetMeanOf(int count,
    			   float mean)
    int main()
    {
    	int totalNumber = count;
    	float num1;
    	float num2;
    	float num3;
    	float num4;
    	float num5;
    	float sum;
    
    	{
    		cout << "Enter 5 numbers: ";
    		cin >> num1 >> num2 >> num3 >> num4 >> num5;
    	}
    	{
    	sum = num1 + num2 + num3 + num4 + num5;
    	count++;
    }
    mean = sum / totalNumber;
    
    	cout << "Average is " << mean << endl;
    }
    Last edited by romeoz; 07-18-2003 at 04:05 PM.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Your program needs something to run it, namely, 'int main()'. The system expects this to be there as that is where it will enter the program.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    And this will cause an infinite loop:
    Code:
    	while(count > 0)
    	{
    		cout << "Enter 5 numbers: ";
    		cin >> num1 >> num2 >> num3 >> num4 >> num5;
    	}
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Note: The reason it compiles okay is that the compiler realizes that the function may be in a different source file somewhere, so it happily builds the object file. The linker is responsible for creating the executable though, and therefore will notice that there is no main in any of the object files.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    I can create a program that gives me the average of 5 integers but I am having problem with this void stuff....could someone explain a little more cause the book has me lost.

    Thanks
    Last edited by romeoz; 07-18-2003 at 04:10 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by romeoz
    I can create a program that gives me the average of 5 integers but I am having problem with this void stuff....could someone explain a little more cause the book has me lost.

    Thanks
    A 'void' function returns no value to the calling function. That's all. Nothing more. A void parameter means it takes no arguments when you call the function.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    ok, I'm still trying to understand this. I just created a program that gives an average. So if I add a void in here its gonna look pretty much the same? except it wont have a return statement?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    	float A;
    	float B;
    	float C;
    	float D;
    	float E;
    	float Average;
    	float Sum;
    
    	cout << "Please enter the 1st integer ";	
    	cin >> A;
    	cout << "Please enter the 2nd integer ";	
    	cin >> B;
    	cout << "Please enter the 3rd integer ";	
    	cin >> C;
    	cout << "Please enter the 4th integer ";	
    	cin >> D;
    	cout << "Please enter the 5th integer ";	
    	cin >> E;
    
    	Average = A + B + C + D + E;
    	Sum = Average / 5;
     
    	cout << "The average of the following numbers are " << Sum << endl;	
    		 
    	return 0;
    }

  8. #8
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    You're using the wrong variables for sum and average, they are the other way around. What you want is just a simple function, with a return type of void (nothing). Here's an example.
    Code:
    #include <iostream>
    using namespace std;
    
    void func(); // function prototype
    
    int main()
    {
              func(); // call func()
              return 0;
    }
    
    void func() // function definition
    {
              cout << "func() called.\n";
              return; // optional, used to leave function and return to previous scope, ie main()
    }
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM