Thread: C Noob : Run time error I cannot figure out.

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    30

    C Noob : Run time error I cannot figure out.

    The issue I am having is that when I run the program it keeps looping at "Enter your first number: ".

    Also when I was toying with it trying to get it to work the prompt asking for what operation would come up immediately after I inputted the value for "Enter your first number:".

    Ha I understand I have probably made some stupid mistakes but this is my first program in C :-D so maybe I can get some slack :P.


    thanks!

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    
    
    double addfunc(double a, double b);       //Add function 
    double subfunc(double a, double b);       //Subtract function
    double divfunc(double a, double b);       //Divide function
    double multifunc(double a, double b);     //Multiply function
    void inputfunc(double *a, double *b);   //Input from console function
    void calculate(double *a, double *b);   //Choice of operation and calling function
    
    
    
    
    main()
    {
    
    
    double answer1 = 0;                     //Holds first value inputted
    double answer2 = 0;                     //Holds second value inputted
    
    
    inputfunc(&answer1, &answer2);          //inputfunc function call passing back answer1/2 
    
    
    calculate(&answer1, &answer2);          //calculate function call passing answer1/2
    
    
    
    
    }
    
    
    
    
    double addfunc(double a, double b)           //Adds two numbers
    {
        return a+b;
    }
    
    
    double subfunc(double a, double b)          //Subtracts two numbers
    {
        return a-b;
    }
    
    
    double divfunc(double a, double b)          //Divides two numbers
    {
        return a/b;
    }
    
    
    double multifunc(double a, double b)         //Multiplies two numbers
    {
        return a*b;
    }
    
    
    void inputfunc(double *a, double *b)         //function to provide input
    {
        
        double temp1 = .12351;                        //temp1 default value 
        double temp2 = .12351;                      //temp1 default value
        
        do                                            //do loop continues to repeat if default value are not changed
        {
            
            
            printf("Enter your first number: ");    //Ask for input
            temp1 = getchar();                        //Collect console input assign to temp1
            
            printf("Enter your second number: ");   //Ask for input2
            temp2 = getchar();                      //Collect console input assign to temp2
            
            
        }
        while(temp1 == .12351 || temp2 == .12351);  
    
    
        
        *a = temp1;                                    //pass values collected back
        *b = temp2;                                    //pass values collected back
    }
    
    
    void calculate(double *a, double *b)            //calculate function that asks for input on operation and calculate/outputs result
    {
            
            char operation = 'Z';                    //preset value also used in while loop
            double temp1 = *a;                      //temp var to hold passed value
            double temp2 = *b;                        //temp var to hold passed value
            
            while (operation == 'Z')                //while loop to keep entry going if value entered doesnt meet if statement condition
            {
                printf("\nwhich operation would you like to use?(A/S/M/D/(Q)uit): "); //prompt for user to enter operation
                operation = getchar();                                                  //operation collecting input
            
                if (operation != 'A' || operation != 'S' || operation != 'M' || operation != 'D' || operation != 'Q')  //checking for bad input
                {
                    continue;                                                                                           //if bad input continues loop
                }
                else
                {
                    switch (operation)                                                                                //switch statement that takes choice and calc/output result
                    {
                        case 'A':
                            printf("Result: %lf",addfunc(temp1,temp2));
                            break;
                        case 'S':
                            printf("Result: %lf",subfunc(temp1,temp2));
                            break;
                        case 'M':
                            printf("Result: %lf",multifunc(temp1,temp2));
                            break;
                        case 'D':
                            printf("Result:  %lf",divfunc(temp1,temp2));
                            break;
                        case 'Q':
                            return;
                    }
                }
                operation = 'Z';                                                                                    //resets var to allow for looping on bad input
                    
            }
    }

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    30
    Oh FWIW this isn't homework.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    On thing I noticed is you can not use getchar() to retrieve floating point numbers. You may want to try scanf().

    Jim

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    30
    Ok I made that edit, and it is working now. Thanks!

    I am now having my original error:

    Enter your first number: 1
    Enter your second number:
    which operation would you like to use?(A/S/M/D/(Q)uit):

    It never gives time for me to input a second number, and just moves directly onto the next function.
    Last edited by Cron; 12-09-2011 at 09:37 AM.

  5. #5
    spaghetticode
    Guest
    Smells like input buffer.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Show your current code.

    Jim

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    30
    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    
    
    double addfunc(double a, double b);   	//Add function 
    double subfunc(double a, double b);   	//Subtract function
    double divfunc(double a, double b);   	//Divide function
    double multifunc(double a, double b); 	//Multiply function
    void inputfunc(double *a, double *b);   //Input from console function
    void calculate(double *a, double *b);   //Choice of operation and calling function
    
    
    
    
    main()
    {
    
    
    double answer1 = 0;                     //Holds first value inputted
    double answer2 = 0;                     //Holds second value inputted
    
    
    inputfunc(&answer1, &answer2);          //inputfunc function call passing back answer1/2 
    
    
    calculate(&answer1, &answer2);          //calculate function call passing answer1/2
    
    
    
    
    }
    
    
    
    
    double addfunc(double a, double b)           //Adds two numbers
    {
    	return a+b;
    }
    
    
    double subfunc(double a, double b)          //Subtracts two numbers
    {
    	return a-b;
    }
    
    
    double divfunc(double a, double b)          //Divides two numbers
    {
    	return a/b;
    }
    
    
    double multifunc(double a, double b)		 //Multiplies two numbers
    {
    	return a*b;
    }
    
    
    void inputfunc(double *a, double *b)		 //function to provide input
    {
    	
    	double temp1 = .12351;						//temp1 default value 
    	double temp2 = .12351;                      //temp1 default value
    	
    	do											//do loop continues to repeat if default value are not changed
    	{
    		
    		
    		printf("Enter your first number: ");	//Ask for input
    		temp1 = scanf();						//Collect console input assign to temp1
    		
    		printf("Enter your second number: ");   //Ask for input2
    		temp2 = scanf();                      //Collect console input assign to temp2
    		
    		
    	}
    	while(temp1 == .12351 || temp2 == .12351);  
    
    
    	
    	*a = temp1;									//pass values collected back
    	*b = temp2;									//pass values collected back
    }
    
    
    void calculate(double *a, double *b)			//calculate function that asks for input on operation and calculate/outputs result
    {
    		
    		char operation = 'Z';					//preset value also used in while loop
    		double temp1 = *a;                      //temp var to hold passed value
    		double temp2 = *b;						//temp var to hold passed value
    		
    		while (operation == 'Z')				//while loop to keep entry going if value entered doesnt meet if statement condition
    		{
    			printf("\nWhich operation would you like to use?(A/S/M/D/(Q)uit): "); //prompt for user to enter operation
    			operation = getchar();												  //operation collecting input
    		
    			if (operation != 'A' || operation != 'S' || operation != 'M' || operation != 'D' || operation != 'Q')  //checking for bad input
    			{
    				continue;																						   //if bad input continues loop
    			}
    			else
    			{
    				switch (operation)																				//switch statement that takes choice and calc/output result
    				{
    					case 'A':
    						printf("Result: %lf",addfunc(temp1,temp2));
    						break;
    					case 'S':
    						printf("Result: %lf",subfunc(temp1,temp2));
    						break;
    					case 'M':
    						printf("Result: %lf",multifunc(temp1,temp2));
    						break;
    					case 'D':
    						printf("Result:  %lf",divfunc(temp1,temp2));
    						break;
    					case 'Q':
    						return;
    				}
    			}
    			operation = 'Z';																					//resets var to allow for looping on bad input
    				
    		}
    }

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Did you even read the documentation for scanf()? This function requires at least 2 parameters. Check the links I provided in my last post.

    Jim

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    30
    Quote Originally Posted by jimblumberg View Post
    Did you even read the documentation for scanf()? This function requires at least 2 parameters. Check the links I provided in my last post.

    Jim
    Sorry about that I didn't realize they were links until just now. scanf() is now correctly implemented thanks for the links.

    The issue now is it repeats from prompt1 and 2:

    Enter your first number: 1
    Enter your second number: 2
    Enter your first number: 1
    Enter your second number: 2
    Enter your first number: 11
    Enter your second number: 2
    Enter your first number: 1
    Enter your second number: 2
    Enter your first number: 1
    Enter your second number: 2
    Enter your first number: 1
    Enter your second number: 2
    Enter your first number: 1
    Enter your second number: 2
    Enter your first number: 1
    Enter your second number: 2
    Enter your first number:

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    
    
    double addfunc(double a, double b);   	//Add function 
    double subfunc(double a, double b);   	//Subtract function
    double divfunc(double a, double b);   	//Divide function
    double multifunc(double a, double b); 	//Multiply function
    void inputfunc(double *a, double *b);   //Input from console function
    void calculate(double *a, double *b);   //Choice of operation and calling function
    
    
    
    
    main()
    {
    
    
    double answer1 = 0;                     //Holds first value inputted
    double answer2 = 0;                     //Holds second value inputted
    
    
    inputfunc(&answer1, &answer2);          //inputfunc function call passing back answer1/2 
    
    
    calculate(&answer1, &answer2);          //calculate function call passing answer1/2
    
    
    
    
    }
    
    
    
    
    double addfunc(double a, double b)           //Adds two numbers
    {
    	return a+b;
    }
    
    
    double subfunc(double a, double b)          //Subtracts two numbers
    {
    	return a-b;
    }
    
    
    double divfunc(double a, double b)          //Divides two numbers
    {
    	return a/b;
    }
    
    
    double multifunc(double a, double b)		 //Multiplies two numbers
    {
    	return a*b;
    }
    
    
    void inputfunc(double *a, double *b)		 //function to provide input
    {
    	
    	double temp1 = .12351;						//temp1 default value 
    	double temp2 = .12351;                      //temp1 default value
    	
    	do											//do loop continues to repeat if default value are not changed
    	{
    		
    		
    		printf("Enter your first number: ");	//Ask for input
    		scanf("%e",temp1);						//Collect console input assign to temp1
    		
    		printf("Enter your second number: ");   //Ask for input2
    		scanf("%e",temp2);                      //Collect console input assign to temp2
    		
    		
    	}
    	while(temp1 == .12351 || temp2 == .12351);  
    
    
    	
    	*a = temp1;									//pass values collected back
    	*b = temp2;									//pass values collected back
    }
    
    
    void calculate(double *a, double *b)			//calculate function that asks for input on operation and calculate/outputs result
    {
    		
    		char operation = 'Z';					//preset value also used in while loop
    		double temp1 = *a;                      //temp var to hold passed value
    		double temp2 = *b;						//temp var to hold passed value
    		
    		while (operation == 'Z')				//while loop to keep entry going if value entered doesnt meet if statement condition
    		{
    			printf("\nWhich operation would you like to use?(A/S/M/D/(Q)uit): "); //prompt for user to enter operation
    			operation = getchar();												  //operation collecting input
    		
    			if (operation != 'A' || operation != 'S' || operation != 'M' || operation != 'D' || operation != 'Q')  //checking for bad input
    			{
    				continue;																						   //if bad input continues loop
    			}
    			else
    			{
    				switch (operation)																				//switch statement that takes choice and calc/output result
    				{
    					case 'A':
    						printf("Result: %lf",addfunc(temp1,temp2));
    						break;
    					case 'S':
    						printf("Result: %lf",subfunc(temp1,temp2));
    						break;
    					case 'M':
    						printf("Result: %lf",multifunc(temp1,temp2));
    						break;
    					case 'D':
    						printf("Result:  %lf",divfunc(temp1,temp2));
    						break;
    					case 'Q':
    						return;
    				}
    			}
    			operation = 'Z';																					//resets var to allow for looping on bad input
    				
    		}
    }

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You need to change your compiler settings so warnings are emitted.
    Here are the errors/warnings I receive when I compile your code:
    |=== c_homework, Debug ===|
    main.c|1|error: extra tokens at end of #include directive|
    main.c|16|error: return type defaults to ‘int’|
    main.c||In function ‘inputfunc’:|
    main.c|72|warning: format ‘%e’ expects type ‘float *’, but argument 2 has type ‘double’|
    main.c|75|warning: format ‘%e’ expects type ‘float *’, but argument 2 has type ‘double’|
    main.c|79|warning: comparing floating point with == or != is unsafe|
    main.c|79|warning: comparing floating point with == or != is unsafe|
    main.c||In function ‘calculate’:|
    main.c|106|warning: switch missing default case|
    ||=== Build finished: 2 errors, 5 warnings ===|
    Jim

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    30
    What is a good compiler to use because I am thinking the one I have may not be sufficient. The compiler I currently have is called Miracle C.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Since you seem to be using Windows, you may want to checkout Code::Blocks (make sure you select the mingw version), or Pelles C.

    Jim

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Cron View Post
    What is a good compiler to use because I am thinking the one I have may not be sufficient. The compiler I currently have is called Miracle C.
    Miracle C is a console only compiler that is somewhat out of date.

    You might want to give Pelles C a try... It is very complete with full documentation of all calls in the help file, a full set of Windows API headers and Resource editors and a very nice IDE. After installing it, be sure to spend some time perusing the help file... it is very complete and even includes a "first project" tutorial to get you started.

  14. #14
    Registered User
    Join Date
    Dec 2011
    Posts
    30
    Thanks for the help. I will work on it with Code::Blocks and see if I can beat this code into submission.

  15. #15
    Registered User
    Join Date
    Dec 2011
    Posts
    30
    Quote Originally Posted by CommonTater View Post
    Miracle C is a console only compiler that is somewhat out of date.

    You might want to give Pelles C a try... It is very complete with full documentation of all calls in the help file, a full set of Windows API headers and Resource editors and a very nice IDE. After installing it, be sure to spend some time perusing the help file... it is very complete and even includes a "first project" tutorial to get you started.
    I will do that! thanks! The issue with Code::Blocks was I was unable to download the installer at work, but I am able to download Pelles C at work. I have been trying to make the linux transition away from Windows for almost 2 years (sigh). I would like to code C on my Slackbox, but thats at home :P guess I could ssh in right? Hah didn't think about that until now.

    To be honest though I am trying to get away from IDE's, and would prefer a console only compiler. When I went to college I was trained in only the .net langauges(vb,c++) (java was refreshing to code in notepad). Linux/Unix do not use .net and quite a bit does not use .net I realized so here I am :-P

    Anyway I am trying to get out of the ".net" mentality.
    Last edited by Cron; 12-09-2011 at 10:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error that I can't figure out.
    By mytrademark in forum C Programming
    Replies: 3
    Last Post: 10-03-2011, 06:54 PM
  2. Odd bus error, can't figure it out.
    By Subsonics in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 11:44 AM
  3. Replies: 8
    Last Post: 04-11-2009, 01:49 AM
  4. Replies: 3
    Last Post: 04-30-2007, 09:53 AM
  5. Can't figure it out the error??? HELP!!!
    By xeneize in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2002, 03:44 PM