Couple C questions :)

This is a discussion on Couple C questions :) within the C Programming forums, part of the General Programming Boards category; I'm taking a C class for the fun of it. I figured I wanted to learn a little more of ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    32

    Couple C questions :)

    I'm taking a C class for the fun of it. I figured I wanted to learn a little more of the history of the C/C++ language. Anyways I've run into a bit of a compiling problem. I'm using MSVC++6 and the following code 'will' compile as a .cpp file, but not as a .c file. I know the compiler handles the two a little differently. I haven't been able to find any configuration settings that my be giving me trouble, but if any of you guys had suggestions or could point out if something wouldn't be considered "c" that would be most appreciated.

    On that note, I was interested in learning how to insert 'preamble code' (correct terminology?). I tried searching through this nifty site and was not able to find anything about doing that. Thank you in advance!

    Code:
    //============================================================================
    // File: MinMax.c
    // ============================================================================
    // Programmer: Michael Croghan
    // Date: 01/26/2003
    // Class: CSCI 223 ("C for Math and Science")
    // Time: TR 4:00PM - 5:50PM
    // Instructor: Mr. Edwards
    // Project: MinMax
    //
    // Description:
    //      Program to read integers and display the minimum and maximum values 
    //		encountered. See the Announcements page for more details.
    // ============================================================================
    
    // INCLUDES ///////////////////////////////////////////////////////////////////
    #include <stdio.h>
    ///////////////////////////////////////////////////////////////////////////////
    
    // DEFINES & TYPEDEFS /////////////////////////////////////////////////////////
    // Message to prompt user //
    #define PRMPT_MSG "Enter an integer (push ctrl-z followed by enter to stop):\t"
    
    // Declare NumberTracker //
    struct NumberTracker;
    // Function pointers for struct NumberTracker //
    typedef void (* constFunc)(NumberTracker &_numTracker);
    typedef int (* readFunc)(NumberTracker &_numTracker);
    typedef void (* compFunc)(NumberTracker &_numTracker);
    ///////////////////////////////////////////////////////////////////////////////
    
    // STRUCTS ////////////////////////////////////////////////////////////////////
    // Purpose is to read user input and process user input //
    struct NumberTracker
    {
    	signed int currentValue;
    	signed int *maxValue;
    	signed int *minValue;
    
    	constFunc NumberTrackerConstructor;
    	readFunc ReadInput;
    	compFunc CompareInput;
    };
    ///////////////////////////////////////////////////////////////////////////////
    
    // FUNCTION DECLARATIONS //////////////////////////////////////////////////////
    void ConstructNumberTracker (NumberTracker &_numTracker);
    int ReadValue (NumberTracker &_numTracker);
    void CompareValue (NumberTracker &_numTracker);
    ///////////////////////////////////////////////////////////////////////////////
    
    // ==== main ==================================================================
    //
    // ============================================================================
    int main (void)
    {
    	// Create NumberTracker; assign appropriate function pointers //
    	NumberTracker numberTracker;
    	numberTracker.NumberTrackerConstructor = &ConstructNumberTracker;
    	numberTracker.ReadInput = &ReadValue;
    	numberTracker.CompareInput = &CompareValue;
    	// Call NumberTracker constructor function //
    	numberTracker.NumberTrackerConstructor (numberTracker);
    
    	// Result value of input read //
    	auto int result = 0;
    
    	// Main loop //
    	for (;;)
    		{
    		printf (PRMPT_MSG);
    		
    		// Get result of input //
    		result = numberTracker.ReadInput (numberTracker);
    
    		// Check if result is EOF //
    		if (result != EOF)
    			{
    			// Verify result is good //
    			if (result)
    				{
    				// Compare current input //
    				numberTracker.CompareInput (numberTracker);
    				}
    			}
    		// result is EOF, end program
    		else
    			{
    			break;
    			}
    		}
    
    	// Print final results to screen //
    	printf ("Max integer value entered:\t%i\nMin integer value entered:\t%i\n",
    			*numberTracker.maxValue, *numberTracker.minValue);
    
    	return 0;
    }
    
    // FUNCTION DEFINITIONS ///////////////////////////////////////////////////////
    // ==== ConstructNumberTracker ================================================
    //
    // Assigns default values to a NumberTracker struct.
    //
    // Input:
    //      _numTracker    -- reference to a NumberTracker struct.
    //
    // Output:
    //      No output.
    //
    // ============================================================================
    void ConstructNumberTracker (NumberTracker &_numTracker)
    {
    	_numTracker.currentValue = 0;
    	_numTracker.maxValue = NULL;
    	_numTracker.minValue = NULL;
    }
    
    // ==== ReadValue =============================================================
    //
    // Reads input from user.  If an EOF is detected (ctrl-z), method returns EOF
    // otherwise assigns user input the currentValue in the NumberTracker passed.
    //
    // Input:
    //      _numTracker    -- reference to a NumberTracker struct.
    //
    // Output:
    //      No output.
    //
    // ============================================================================
    int ReadValue (NumberTracker &_numTracker)
    {
    	auto int input = 0;
    
    	// Read result of scan //
    	auto int result = scanf ("%i", &input);
    
    	// Verify if data was read properly //
    	if (result)
    		{
    			// Check if EOF; if EOF return EOF //
    			if (input == EOF)
    				{
    				return EOF;
    				}
    			// Input not EOF; assign input to currentValue //
    			else
    				{
    					_numTracker.currentValue = input;
    				}
    		}
    
    	return result;
    }
    
    // ==== CompareValue ==========================================================
    //
    // Compares the current integer value entered by the user to the min and max
    // integer values.
    //
    // Input:
    //      _numTracker    -- reference to a NumberTracker struct.
    //
    // Output:
    //      No output.
    //
    // ============================================================================
    void CompareValue (NumberTracker &_numTracker)
    {
    	// Make sure this isn't the first run through //
    	if (_numTracker.minValue && _numTracker.maxValue)
    		{
    		// Check to see if currentValue is below current min //
    		if (*_numTracker.minValue > _numTracker.currentValue)
    			{
    			*_numTracker.minValue = _numTracker.currentValue;
    			}
    		else
    			{
    			// Check to see if currentValue is above current max //
    			if (*_numTracker.maxValue < _numTracker.currentValue)
    				{
    				*_numTracker.maxValue = _numTracker.currentValue;
    				}
    			}
    		}
    	// There current existing min or max values; instantiate min/max integers
    	// and assign the currentValue to both of them.
    	else
    		{
    		_numTracker.minValue = new int;
    		_numTracker.maxValue = new int;
    
    		*_numTracker.minValue = _numTracker.currentValue;
    		*_numTracker.maxValue = _numTracker.currentValue;
    		}
    }
    ///////////////////////////////////////////////////////////////////////////////

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    What error(s) do you get when compiling as a c file?
    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

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    Suppose that would be helpful.

    Code:
    --------------------Configuration: MinMax - Win32 Debug--------------------
    Compiling...
    minmax.c
    C:\Projects\School CSCI 223\MinMax\minmax.c(27) : error C2143: syntax error : missing ')' before '*'
    C:\Projects\School CSCI 223\MinMax\minmax.c(27) : error C2143: syntax error : missing '{' before '*'
    C:\Projects\School CSCI 223\MinMax\minmax.c(27) : error C2059: syntax error : ')'
    C:\Projects\School CSCI 223\MinMax\minmax.c(28) : error C2143: syntax error : missing ')' before '*'
    C:\Projects\School CSCI 223\MinMax\minmax.c(28) : error C2143: syntax error : missing '{' before '*'
    C:\Projects\School CSCI 223\MinMax\minmax.c(28) : error C2059: syntax error : ')'
    C:\Projects\School CSCI 223\MinMax\minmax.c(29) : error C2143: syntax error : missing ')' before '*'
    C:\Projects\School CSCI 223\MinMax\minmax.c(29) : error C2143: syntax error : missing '{' before '*'
    C:\Projects\School CSCI 223\MinMax\minmax.c(29) : error C2059: syntax error : ')'
    C:\Projects\School CSCI 223\MinMax\minmax.c(40) : error C2061: syntax error : identifier 'constFunc'
    C:\Projects\School CSCI 223\MinMax\minmax.c(41) : error C2061: syntax error : identifier 'ReadInput'
    C:\Projects\School CSCI 223\MinMax\minmax.c(41) : error C2059: syntax error : ';'
    C:\Projects\School CSCI 223\MinMax\minmax.c(42) : error C2061: syntax error : identifier 'CompareInput'
    C:\Projects\School CSCI 223\MinMax\minmax.c(42) : error C2059: syntax error : ';'
    C:\Projects\School CSCI 223\MinMax\minmax.c(43) : error C2059: syntax error : '}'
    C:\Projects\School CSCI 223\MinMax\minmax.c(47) : error C2143: syntax error : missing ')' before '*'
    C:\Projects\School CSCI 223\MinMax\minmax.c(47) : error C2143: syntax error : missing '{' before '*'
    C:\Projects\School CSCI 223\MinMax\minmax.c(47) : error C2059: syntax error : ')'
    C:\Projects\School CSCI 223\MinMax\minmax.c(48) : error C2143: syntax error : missing ')' before '*'
    C:\Projects\School CSCI 223\MinMax\minmax.c(48) : error C2143: syntax error : missing '{' before '*'
    C:\Projects\School CSCI 223\MinMax\minmax.c(48) : error C2059: syntax error : ')'
    C:\Projects\School CSCI 223\MinMax\minmax.c(49) : error C2143: syntax error : missing ')' before '*'
    C:\Projects\School CSCI 223\MinMax\minmax.c(49) : error C2143: syntax error : missing '{' before '*'
    C:\Projects\School CSCI 223\MinMax\minmax.c(49) : error C2059: syntax error : ')'
    C:\Projects\School CSCI 223\MinMax\minmax.c(58) : error C2065: 'NumberTracker' : undeclared identifier
    C:\Projects\School CSCI 223\MinMax\minmax.c(58) : error C2146: syntax error : missing ';' before identifier 'numberTracker'
    C:\Projects\School CSCI 223\MinMax\minmax.c(58) : error C2065: 'numberTracker' : undeclared identifier
    C:\Projects\School CSCI 223\MinMax\minmax.c(59) : error C2224: left of '.NumberTrackerConstructor' must have struct/union type
    C:\Projects\School CSCI 223\MinMax\minmax.c(59) : error C2065: 'ConstructNumberTracker' : undeclared identifier
    C:\Projects\School CSCI 223\MinMax\minmax.c(60) : error C2224: left of '.ReadInput' must have struct/union type
    C:\Projects\School CSCI 223\MinMax\minmax.c(60) : error C2065: 'ReadValue' : undeclared identifier
    C:\Projects\School CSCI 223\MinMax\minmax.c(61) : error C2224: left of '.CompareInput' must have struct/union type
    C:\Projects\School CSCI 223\MinMax\minmax.c(61) : error C2065: 'CompareValue' : undeclared identifier
    C:\Projects\School CSCI 223\MinMax\minmax.c(63) : error C2224: left of '.NumberTrackerConstructor' must have struct/union type
    C:\Projects\School CSCI 223\MinMax\minmax.c(66) : error C2143: syntax error : missing ';' before 'type'
    C:\Projects\School CSCI 223\MinMax\minmax.c(74) : error C2065: 'result' : undeclared identifier
    C:\Projects\School CSCI 223\MinMax\minmax.c(74) : error C2224: left of '.ReadInput' must have struct/union type
    C:\Projects\School CSCI 223\MinMax\minmax.c(83) : error C2224: left of '.CompareInput' must have struct/union type
    C:\Projects\School CSCI 223\MinMax\minmax.c(95) : error C2224: left of '.maxValue' must have struct/union type
    C:\Projects\School CSCI 223\MinMax\minmax.c(95) : error C2224: left of '.minValue' must have struct/union type
    C:\Projects\School CSCI 223\MinMax\minmax.c(112) : error C2143: syntax error : missing ')' before '*'
    C:\Projects\School CSCI 223\MinMax\minmax.c(112) : error C2143: syntax error : missing '{' before '*'
    C:\Projects\School CSCI 223\MinMax\minmax.c(112) : error C2059: syntax error : ')'
    C:\Projects\School CSCI 223\MinMax\minmax.c(113) : error C2054: expected '(' to follow '_numTracker'
    C:\Projects\School CSCI 223\MinMax\minmax.c(131) : error C2143: syntax error : missing ')' before '*'
    C:\Projects\School CSCI 223\MinMax\minmax.c(131) : error C2143: syntax error : missing '{' before '*'
    C:\Projects\School CSCI 223\MinMax\minmax.c(131) : error C2059: syntax error : ')'
    C:\Projects\School CSCI 223\MinMax\minmax.c(132) : error C2054: expected '(' to follow '_numTracker'
    C:\Projects\School CSCI 223\MinMax\minmax.c(168) : error C2143: syntax error : missing ')' before '*'
    C:\Projects\School CSCI 223\MinMax\minmax.c(168) : error C2143: syntax error : missing '{' before '*'
    C:\Projects\School CSCI 223\MinMax\minmax.c(168) : error C2059: syntax error : ')'
    C:\Projects\School CSCI 223\MinMax\minmax.c(169) : error C2054: expected '(' to follow '_numTracker'
    Error executing cl.exe.
    
    MinMax.exe - 52 error(s), 0 warning(s)

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    void CompareValue (NumberTracker &_numTracker)
    There are no "references" in C. This is a C++ feature. For future reference (no pun intended), post the actual error messages you get. No one wants to read a thousand lines of code just because you're too damn lazy to copy/paste in the error message.

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

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    Took out references and am still getting the same 'relative' error.

    Code:
    // Declare NumberTracker //
    struct NumberTracker;
    // Function pointers for struct NumberTracker //
    typedef void (* constFunc)(NumberTracker *_numTracker);
    typedef int (* readFunc)(NumberTracker *_numTracker);
    typedef void (* compFunc)(NumberTracker *_numTracker);
    
    error:
    c:\projects\school csci 223\minmax\minmax.c(27) : error C2143: syntax error : missing ')' before '*'
    *edit* replaced 'new' with malloc.
    Last edited by Divx; 01-27-2003 at 09:16 PM.

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    I got it.

    There were a few other 'C++' things in there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple of simple directdraw questions.
    By Deo in forum Game Programming
    Replies: 3
    Last Post: 05-25-2005, 08:55 AM
  2. Studying for Final, Couple Questions...
    By stewade in forum C Programming
    Replies: 4
    Last Post: 05-10-2004, 06:02 PM
  3. A couple of Questions
    By johnnabn in forum C++ Programming
    Replies: 4
    Last Post: 02-24-2003, 10:10 PM
  4. A couple of PowerPoint questions.
    By sean in forum Tech Board
    Replies: 2
    Last Post: 01-27-2003, 05:26 AM
  5. New to C++/ Couple of questions...
    By danielthomas3 in forum C++ Programming
    Replies: 13
    Last Post: 04-14-2002, 04:46 PM

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