function problems

This is a discussion on function problems within the C++ Programming forums, part of the General Programming Boards category; I'm trying to combine 3 simple conversion programs I wrote into a larger one, with a menu over all of ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    4

    function problems

    I'm trying to combine 3 simple conversion programs I wrote into a larger one, with a menu over all of them. I suck at functions, and switches for that matter and I know I'm probably doing about 20 things incorrectly but I can't seem to figure it out. As you can guess, I'm a C++ newbie as well as a newbie to this board.

    Errors:

    warning C4508: 'main' : function should return a value; 'void' return type assumed
    error C2143: syntax error : missing ';' before 'while'
    error C2143: syntax error : missing ';' before '}'
    error C2143: syntax error : missing ';' before '}'
    error C2143: syntax error : missing ';' before '}'
    error C2143: syntax error : missing ';' before '{'
    error C2447: missing function header (old-style formal list?)
    fatal error C1004: unexpected end of file found



    Code:
    #include <iostream.h>
    #include <stdio.h>
    
    double weight (void);
    double height (void);
    double temperature (void);
    int main()
    {
    	int result;          /*  restart variable  */
    	int start;        /* start variable */
    	
    	
    	
    	
    
    	
    		
    	switch (start){	   
    		case 1: 
    			weight();
    			break;	 
    		case 2: height();	 
    			break;	
    		case 3:	temperature();
    			break;	
    		default: 	  
            printf ("Error, Please Enter 1,2,or 3:");
    		scanf ( "%i", &start);
    	}
    
    		printf( "\nEnter '1' to continue or '0' to quit.\n");
    		scanf( "%i", &result);
    
    	} while (result != 0);
    
    	
    }
    
    	
    	
    ////////////////////////////////////////////////////////////////////////////////////////
    /*	Function Definitions */
    	
    
    double weight (){
    
    	int s1;            /* local start variable  */
    	double lb;		/* weight in pounds  */
    	double kg;		/* weight in kilograms  */	
    	double f;         /* answer  */
    	
    	
    	puts( " 1) Pounds to Kilograms\n 2) Kilograms to Pounds\n ");
    
    	scanf(  "%i", &s1 ); 
    
    	if ( s1 == 1) {
    		printf( " Enter the weight in pounds:");
    		scanf( "%lg", &lb);
    
    			
    		f = lb / 2.2 ; 
    		printf( " A weight of %lg pounds", lb);
    		printf( " equals a weight of %lg kilograms", f); 
    
    		return f;
    		
    	}
    
    			
    				
    									
    					
    			
    	else if ( s1 == 2) {
    	
    			printf( " Enter the weight in kilograms:");
    			scanf( "%lg", &kg);
    
    			
    			f = kg * 2.2; 
    			printf( " A weight of %lg kilograms", kg);
    			printf( " equals a weight of %lg pounds", f); 
    
    			return f;
    
    		}
    
    	else {
    
    		printf( " Error.");
    
    	}
    
    
    
    	//////////////////////////////////////////////////////////////////////////////////
    
    	double height () {
    		
    		int s2;               /* local start variable */
    		double ih;		/* height in meters  */
    		double eh;		/* height in feet (fraction) */	
    		double hf;         /* answer  */
    
    		puts( " 1) Pounds to Kilograms\n 2) Kilograms to Pounds\n ");
    
    	scanf(  "%i", &s2 ); 
    
    	if ( s2 == 1) {
    		printf( " Enter the height in feet (as a fraction):");
    		scanf( "%lg", &eh);
    
    			
    		hf = eh * 3.25; 
    		printf( " A height of %lg feet", eh);
    		printf( " equals a height of %lg meters", hf); 
    
    		return hf;
    		
    	}
    
    	else if ( s1 == 2) {
    	
    			printf( " Enter the height in meters (as a fraction):");
    			scanf( "%lg", &ih);
    
    			
    			hf = eh / 3.25; 
    			printf( " A height of %lg meters", ih);
    			printf( " equals a height of %lg feet", hf); 
    
    			return hf;
    
    		}
    
    	else {
    
    		printf( " Error.");
    
    	}
    
    	////////////////////////////////////////////////////////////////////////////////
    
    
    	double temperature ()  {
    
    		int s3;       // local start variable
    		double ft     //   temp fahrenheit
    		double ct     //   temp celsius
    		double r      //   result
    
    		puts( " 1) Celsius to Fahrenheit\n 2) Fahrenheit to Celsius\n ");
    
    		scanf(  "%i", &s2 ); 
    
    	if ( s2 == 1) {
    		printf( " Enter the temperature in Celsius:");
    		scanf( "%lg", &ct);
    
    			
    		r = (ct*1.8) + 32; 
    		printf( " A temperature of %lg Celsius", ct);
    		printf( " equals a temperature of %lg Fahrenheit", r); 
    
    		return r;
    		
    	}
    
    	else if ( s1 == 2) {
    	
    			printf( " Enter the temperature in Fahrenheit:");
    			scanf( "%lg", &ft);
    
    			
    			r = (ft-32)/ 1.8; 
    			printf( " A temperature of %lg Fahrenheit", ft);
    			printf( " equals a temperature of %lg Celsius", r); 
    
    			return r;
    
    		}
    
    	else {
    
    		printf( " Error.");
    
    	}

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    The two errors u will get compiling this code is because u need to pass, ie reference, s1 and s2 in the functions in which they are not declared. Your trying to use them in a function that does not know they exist, pass them as paramteres and you should be fine. Id do it for you but that wouldnt help you. The errors were the missing return 0; in main, a missing } on each function, and missing ; after a few variable declarations. I also added the use of namespace std;

    Compiled in .Net with two errors:

    c:\Documents and Settings\Steve\My Documents\Visual Studio Projects\cboardhelp\main.cpp(439): error C2065: 's2' : undeclared identifier
    c:\Documents and Settings\Steve\My Documents\Visual Studio Projects\cboardhelp\main.cpp(402): error C2065: 's1' : undeclared identifier
    Code:
    #include <iostream>
    #include <cstdio>
    
    using namespace std;
    
    double weight (void);
    double height (void);
    double temperature (void);
    int main()
    {
    	int result;          /*  restart variable  */
    	int start;        /* start variable */
    	
    	
    	
    	
    
    	
    	do
    	{
    	switch (start){	   
    		case 1: 
    			weight();
    			break;	 
    		case 2: height();	 
    			break;	
    		case 3:	temperature();
    			break;	
    		default: 	  
            printf ("Error, Please Enter 1,2,or 3:");
    		scanf ( "%i", &start);
    	}
    
    		printf( "\nEnter '1' to continue or '0' to quit.\n");
    		scanf( "%i", &result);
    
    	} while (result != 0);
    
    	return 0;
    }
    
    	
    	
    ////////////////////////////////////////////////////////////////////////////////////////
    /*	Function Definitions */
    	
    
    double weight ()
    {
    
    	int s1;            /* local start variable  */
    	double lb;		/* weight in pounds  */
    	double kg;		/* weight in kilograms  */	
    	double f;         /* answer  */
    	
    	
    	puts( " 1) Pounds to Kilograms\n 2) Kilograms to Pounds\n ");
    
    	scanf(  "%i", &s1 ); 
    
    	if ( s1 == 1) 
    	{
    		printf( " Enter the weight in pounds:");
    		scanf( "%lg", &lb);
    
    			
    		f = lb / 2.2 ; 
    		printf( " A weight of %lg pounds", lb);
    		printf( " equals a weight of %lg kilograms", f); 
    
    		return f;
    		
    	}
    
    			
    				
    									
    					
    			
    	else if ( s1 == 2) 
    	{
    	
    			printf( " Enter the weight in kilograms:");
    			scanf( "%lg", &kg);
    
    			
    			f = kg * 2.2; 
    			printf( " A weight of %lg kilograms", kg);
    			printf( " equals a weight of %lg pounds", f); 
    
    			return f;
    
    	}
    
    	else 
    	{
    
    		printf( " Error.");
    
    	}
    }
    
    
    	//////////////////////////////////////////////////////////////////////////////////
    
    	double height () 
    	{
    		
    		int s2;               /* local start variable */
    		double ih;		/* height in meters  */
    		double eh;		/* height in feet (fraction) */	
    		double hf;         /* answer  */
    
    		puts( " 1) Pounds to Kilograms\n 2) Kilograms to Pounds\n ");
    
    	scanf(  "%i", &s2 ); 
    
    	if ( s2 == 1) 
    	{
    		printf( " Enter the height in feet (as a fraction):");
    		scanf( "%lg", &eh);
    
    			
    		hf = eh * 3.25; 
    		printf( " A height of %lg feet", eh);
    		printf( " equals a height of %lg meters", hf); 
    
    		return hf;
    		
    	}
    
    	else if ( s1 == 2) 
    	{
    	
    			printf( " Enter the height in meters (as a fraction):");
    			scanf( "%lg", &ih);
    
    			
    			hf = eh / 3.25; 
    			printf( " A height of %lg meters", ih);
    			printf( " equals a height of %lg feet", hf); 
    
    			return hf;
    
    	}
    
    	else 
    	{
    
    		printf( " Error.");
    
    	}
    	
    }
    	
    	////////////////////////////////////////////////////////////////////////////////
    
    
    	double temperature ()  
    	{
    
    		int s3;       // local start variable
    		double ft;     //   temp fahrenheit
    		double ct;     //   temp celsius
    		double r;      //   result
    
    		puts( " 1) Celsius to Fahrenheit\n 2) Fahrenheit to Celsius\n ");
    
    		scanf(  "%i", &s2 ); 
    
    	if ( s2 == 1) 
    	{
    		printf( " Enter the temperature in Celsius:");
    		scanf( "%lg", &ct);
    
    			
    		r = (ct*1.8) + 32; 
    		printf( " A temperature of %lg Celsius", ct);
    		printf( " equals a temperature of %lg Fahrenheit", r); 
    
    		return r;
    		
    	}
    
    	else if ( s1 == 2) 
    	{
    	
    			printf( " Enter the temperature in Fahrenheit:");
    			scanf( "%lg", &ft);
    
    			
    			r = (ft-32)/ 1.8; 
    			printf( " A temperature of %lg Fahrenheit", ft);
    			printf( " equals a temperature of %lg Celsius", r); 
    
    			return r;
    
    	}
    
    	else 
    	{
    
    		printf( " Error.");
    
    	}
    }

  3. #3
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    the first warning is there because "int main" is expecting you to return a value, just add return 0 at the end of main( ).

    Also in main() it looks like you have a do..while() loops without the do bit, this will probably give you some of the missing ; errors. Try making sure you always have pair of curly braces
    Couldn't think of anything interesting, cool or funny - sorry.

  4. #4
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Thats the other thing i did. I knew there was something else, i added the do part of the loop, thnx endo!

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    4
    Thanks for the help guys. I got it working like a champ.
    Last edited by money; 07-16-2003 at 11:15 AM.

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    A much better structure would be to accept the input outside of the calculation functions. Then pass variables to the functions indicating which calculation is desired.

    General rule of programming is that each function should do one and only one thing. It should do it well and efficiently. There are exceptions but this is generall a good programming practice.

    Another one is that there should be only one entry point and one exit point from any given function. Exiting in the middle of a function is not a good practice and makes your code harder to read.

    But your code can be reduced significantly into one function - violating certain rules.

    Code:
    #define LB_TO_KG (x)  ((double)x/2.2)
    #define KG_TO_LB(x) ((double)x*2.2)
    #define FT_TO_M(x) ((double)x*3.25)
    #define M_TO_FT(x) ((double)x/3.25)
    #define C_TO_F(x) (((double)x*1.8)+32.0)
    #define F_TO_C(x) (((double)x-32.0)/1.8)
    
    #define LBTOKG 0x0000
    #define KGTOLB 0x0001
    #define FTTOM  0x0002
    #define MTOFT  0x0004
    #define CTOF   0x0008
    #define FTOC   0x0010
    After you get the input you simply call one of these conversion macros - which is really not a function call at all.

    Code:
    void GetInput(void)
    {
      ....
      Calc(inputmode,inputvalue);
    
    }
    
    void Calc(int calcmode,double value)
    {
       double result=0.0;  
       char *txt1=0,*txt2=0;
       switch (calcmode)
      {
        case LBTOKG: 
        {
           txt1="pounds";txt2="kilograms";
           result=LB_TO_KG(value);
           break;
         }
        case KGTOLB: 
        {
           txt1="kilograms";txt2="pounds";
           result=KG_TO_LB(value);
           break;
        }
        case FTTOM:
        {
           txt1="feet";txt2="meters";
           result=FT_TO_M(value);
           break;
        }
        case MTOFT: 
        {
           txt1="meters";txt2="feet";       
           result=M_TO_FT(value);
           break;
        }
        case CTOF:
        {
           txt1="celsius";txt2="fahrenheit";
           result=C_TO_F(value);
           break;
        }
        case FTOC:
        {
           txt1="fahrenheit";txt2="celsius";
           result=F_TO_C(value);
           break;
        }
      }
       printf("%d %s converts to %d %s.\n",value,txt1,result,txt2);
    }
    There is an easier way to find out what calculation is needed inside of calc() using bitwise operations. But I leave that to you to figure out. The switch is rather ugly - there is a much better and more efficient way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. Problems with str.replace function
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 03:35 AM

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