Thread: having problem with function

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    9

    having problem with function

    I am working on a final project that is a unit conversion program. I am having trouble getting the first part of the program working. I left out most of the fuctions so I could test what I had already. It just displays a lot of random numbers when I run it. Sorry about the long code, I did not know how much of my code I should post to make it clear.

    Code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void menu(void); //menu function
    void length(void); //length funtction
    void mm(void); //millimeter function
    
    int main(int argc, char *argv[])
    {
        char check = 'y';
        
        while(check == 'y' || check == 'Y'){ //loop to rerun program
                   menu();
                   printf("Do you want to continue? (y for yes): ");
                   scanf("%c", &check);
                   }
      system("PAUSE");	
      return 0;
    }
    void menu(void){                                //menu function
         char start;
         printf("Enter:\n1 for Length Conversion\n");
         printf("2 for Area Conversion\n");
         printf("3 for Volume Conversion\n");
         printf("4 for Time Conversion\n");
         printf("5 for Angle Conversion\n");
         printf("6 for Speed Conversion\n");
         printf("7 for Acceleration Conversion\n");
         printf("8 for Mass Conversion\n");
         printf("9 for Force Conversion\n");
         printf("10 for Pressure Conversion\n");
         printf("11 for Energy Conversion\n");
         printf("12 for Power Conversion\n");
         scanf("%c",&start);
    
         switch(start){
         case '1':
              length();
              break;
         
         default:
                 printf("Not valid entry\n");
                 }
                 }     
    
    void length(void){                          //length function
         char start;
         printf("Enter starting unit: \n");
         printf("1 for millimeters\n");
         printf("2 for centimeters\n");
         printf("3 for meters\n");
         printf("4 for kilometers\n");
         printf("5 for inches\n");
         printf("6 for feet\n");
         printf("7 for yards\n");
         printf("8 for miles\n");
         scanf("%c", &start);
    
          switch(start){
         case '1':
              mm();
              break;
         default:
                 printf("Not valid entry\n");
                 }
                 }  
              
    void mm(void){                              //millimeter function
         char end;
         double mm;
         double ans;
         printf("Enter the number of millimeters: \n");
         scanf("%f",&mm);
         printf("Enter ending unit: \n");
         printf("1 for millimeters\n");
         printf("2 for centimeters\n");
         printf("3 for meters\n");
         printf("4 for kilometers\n");
         printf("5 for inches\n");
         printf("6 for feet\n");
         printf("7 for yards\n");
         printf("8 for miles\n");
         scanf("%c", &end);
    
         switch(end){
         case '1':
              ans = mm;
              printf("%lf millimeters equals %lf millimeters\n",mm, ans);
              break;
         case '2':
              ans = mm*10;
               printf("%f millimeters equals %f centimeters\n",mm, ans);
              break;
         case '3':
              ans = mm*1000;
               printf("%f millimeters equals %f meters\n",mm, ans);
              break;
         case '4':
              ans = mm*1000000;
               printf("%f millimeters equals %f kiloimeters\n",mm, ans);
               break;
         case '5':
              ans = mm*25.4;
               printf("%f millimeters equals %f inches\n",mm, ans);
              break;
         case '6':
              ans = mm*304.8;
               printf("%f millimeters equals %f feet\n",mm, ans);
              break;
         case '7':
              ans = mm*1914.4;
               printf("%f millimeters equals %f yards\n",mm, ans);
              break;
         case '8':
              ans = mm*1609000;
               printf("%f millimeters equals %f miles\n",mm, ans);
              break;
         
         default:
                 printf("Not valid entry\n");
         }
                 
                 }
    Thanks in advance,
    Casey

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    26
    Hey, I went over your program and made a few corrections. I'm going to bold them in the code. They're pretty simple mistakes but if you have any questions about what I did or why feel free to ask. I changed the spacing to make it easier for me to read. Here's the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void menu();
    void length();
    void mm();
    
    int main(int argc, char *argv[])
    {
    	char check[5];
    
    	/* loop to rerun program */
    	do{
    		menu();
    		printf("Do you want to continue (y for yes)? ");
    		scanf("%s", check);
    		
    	}while(strcmp("y", check) == 0);
    
    	system("PAUSE");	
    	return 0;
    }
    
    /* Menu Function */
    void menu(){
    	int start;
    
    	printf("Enter:\n1 for Length Conversion\n");
    	printf("2 for Area Conversion\n");
    	printf("3 for Volume Conversion\n");
    	printf("4 for Time Conversion\n");
    	printf("5 for Angle Conversion\n");
    	printf("6 for Speed Conversion\n");
    	printf("7 for Acceleration Conversion\n");
    	printf("8 for Mass Conversion\n");
    	printf("9 for Force Conversion\n");
    	printf("10 for Pressure Conversion\n");
    	printf("11 for Energy Conversion\n");
    	printf("12 for Power Conversion\n");
    	scanf("%d", &start);
    
    	switch(start){
    		case 1:
    			length();
    			break;
         
    		default:
    			printf("Not valid entry\n");
    			break;
    	}
    }     
    
    /* Length Function */
    void length(){
    	int start;
    
    	printf("Enter starting unit: \n");
    	printf("1 for millimeters\n");
    	printf("2 for centimeters\n");
    	printf("3 for meters\n");
    	printf("4 for kilometers\n");
    	printf("5 for inches\n");
    	printf("6 for feet\n");
    	printf("7 for yards\n");
    	printf("8 for miles\n");
    	scanf("%d", &start);
    
    	switch(start){
    		case 1:
    			mm();
    			break;
    
    		default:
    			printf("Not valid entry\n");
    			break;
    	}
    }  
    
    /* mm Function */
    void mm(){
    	int end;
    	double mm;
    	double ans;
    
    	printf("Enter the number of millimeters: ");
    	scanf("%lf", &mm);
    	
    	printf("Enter ending unit: \n");
    	printf("1 for millimeters\n");
    	printf("2 for centimeters\n");
    	printf("3 for meters\n");
    	printf("4 for kilometers\n");
    	printf("5 for inches\n");
    	printf("6 for feet\n");
    	printf("7 for yards\n");
    	printf("8 for miles\n");
    	scanf("%d", &end);
    
    	switch(end){
    		case 1:
    			ans = mm;
    			printf("%lf millimeters equals %lf millimeters\n", mm, ans);
    			break;
    
    		case 2:
    			ans = mm/10;
    			printf("%lf millimeters equals %lf centimeters\n", mm, ans);
    			break;
    
    		case 3:
    			ans = mm/1000;
    			printf("%lf millimeters equals %lf meters\n", mm, ans);
    			break;
    
    		case 4:
    			ans = mm/1000000;
    			printf("%lf millimeters equals %lf kiloimeters\n", mm, ans);
    			break;
    
    		case 5:
    			ans = mm/25.4;
    			printf("%lf millimeters equals %lf inches\n", mm, ans);
    			break;
    
    		case 6:
    			ans = mm/304.8;
    			printf("%lf millimeters equals %lf feet\n", mm, ans);
    			break;
    			
    		case 7:
    			ans = mm/1914.4;
    			printf("%lf millimeters equals %lf yards\n", mm, ans);
    			break;
    
    		case 8:
    			ans = mm/1609000;
    			printf("%lf millimeters equals %lf miles\n", mm, ans);
    			break;
         
    		default:
    			printf("Not valid entry\n");
    			break;
    	}
    }

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    Code:
    system("PAUSE");
    dont use this system function to pause.

    s.s.harish

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by ssharish
    Code:
    system("PAUSE");
    dont use this system function to pause.

    s.s.harish
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Pressure
    Hey, I went over your program and made a few corrections. I'm going to bold them in the code.
    You do know that all of these "corrections", really aren't. Right?

    Quote Originally Posted by Pressure
    They're pretty simple mistakes but if you have any questions about what I did or why feel free to ask. I changed the spacing to make it easier for me to read. Here's the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void menu();
    void length();
    void mm();
    
    int main(int argc, char *argv[])
    {
    	char check[5];
    
    	/* loop to rerun program */
    	do{
    		menu();
    		printf("Do you want to continue (y for yes)? ");
    		scanf("%s", check);
    		
    	}while(strcmp("y", check) == 0);
    
    	system("PAUSE");	
    	return 0;
    }
    Ok, now that's just horrible. You went from reading one letter, which checked for both upper and lower case, to a string which can definately crash your program if you type too much, which only checks for lower case.

    Quote Originally Posted by Pressure
    Code:
    /* Menu Function */
    void menu(){
    	int start;
    
    	printf("Enter:\n1 for Length Conversion\n");
    	printf("2 for Area Conversion\n");
    	printf("3 for Volume Conversion\n");
    	printf("4 for Time Conversion\n");
    	printf("5 for Angle Conversion\n");
    	printf("6 for Speed Conversion\n");
    	printf("7 for Acceleration Conversion\n");
    	printf("8 for Mass Conversion\n");
    	printf("9 for Force Conversion\n");
    	printf("10 for Pressure Conversion\n");
    	printf("11 for Energy Conversion\n");
    	printf("12 for Power Conversion\n");
    	scanf("%d", &start);
    
    	switch(start){
    		case 1:
    Again, why? Reading a single character and using it correctly, as they were, isn't "incorrect". It is perfectly fine to do it the way they were doing. Your code doesn't "correct" anything here.

    Quote Originally Posted by Pressure
    Code:
    		default:
    			printf("Not valid entry\n");
    			break;
    	}
    }
    As the last case in a switch, the break is unnecessary. Furthermore, it isn't even actually required in any switch statements. Leaving it out causes that switch case to drop through to the next one on down the line until it hits a break or the switch ends. Since this is in fact the last case in the switch, it's not needed at all. So again, it wasn't "incorrect".

    Quote Originally Posted by Pressure
    Code:
     
    /* Length Function */
    void length(){
    	int start;
    
            ... snip ...
    
    		default:
    			printf("Not valid entry\n");
    			break;
    	}
    }
    See above.

    Quote Originally Posted by Pressure
    Code:
    /* mm Function */
    		case 1:
    			ans = mm;
    			printf("%lf millimeters equals %lf millimeters\n", mm, ans);
    This is "corrected" how exactly?

    Here's the origional for comparison:
    Code:
            ans = mm;
              printf("%lf millimeters equals %lf millimeters\n",mm, ans);
    Yep. Looks the same to me.

    [edit]
    Removed math related mental lapse. I'll blame lack of sleep. Your math correction was right.
    [/edit]

    To the origional poster: Probably what is happening is that the newline is getting left in the input stream, and as such, it skips over your next menu choices. Like so:
    Code:
    #include <stdio.h>
    
    int main( void )
    {
        char c;
    
        printf("Enter a letter and press enter. >");
        fflush( stdout );
        scanf("%c", &c );
    
        printf("This input prompt will get skipped. >");
        fflush( stdout );
        scanf("%c", &c );
    
    
        printf("The next stop should be here. >");
        fflush( stdout );
        scanf("%c", &c );
    
        return 0;
    }
    
    /*
        My output/input:
    
    Enter a letter and press enter. >d
    This input prompt will get skipped. >The next stop should be here. >
    
    */
    
    You need to clear out the input buffer to get rid of the extra input.

    Code:
    int clearer;
    
    ...stuff...
    scanf( "%c", &c );
    while( (clearer = getchar()) != '\n' );
    Just do something like that after each scanf call if you're going to use scanf to clear out all the extra pending input.

    Quzah.
    Last edited by quzah; 04-22-2005 at 12:40 PM. Reason: Tag fixing. Removed a mental lapse I had regarding math. :rolleyes:
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    26
    All of that was made because it didn't run in my compiler the way he had it set. The reading in single characters was skipping past the loop for me and that was that only way I could get it to stay in. Same with reading the characters with the switch cases. The original for mm I admit wasn't a correction it was a mistake on my part. Maybe there's something wrong with my compiler and that's why it wasn't working. I apologize for anything I've done, I am currently running on 92 hours with no sleep and I'm not thinking straight. I'm going to bed.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Pressure
    The reading in single characters was skipping past the loop for me and that was that only way I could get it to stay in.
    Kind of like what I described in the last half of my post?

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM