Thread: Temperature converter program with functions

  1. #16
    Banned
    Join Date
    Aug 2017
    Posts
    861
    yeah Its bunk I started really going after it after it ran Okey DOkey then posted it. Then seen how that if statement inside of the first if statement was not really doing anything . the scanf inside of it was getting lucky , new rewrite. I was hoping to get to it before time ran out , but I see you're awake, and you caught it, you have not been in here in a few days. nice nap?
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    int main (void)
    {
        printf("Would you like to see a dog chasing his tail? y/n\n");
        
        char what = '\0';
        int whatnow = 0;
        
        scanf(" %c", &what);
        
        while (tolower(what) != 'n')
        {
            printf("I'm chasing my tail\n");
            whatnow++;
            
            if (whatnow == 10)
            {
                printf("Do you still want to see me\nchasing my tail? y/n\n");
                getchar();
                what = fgetc(stdin) ; 
                
                 
                
                switch (what)
                {
                    case 'n':
                        break;
                    default:
                        whatnow = 0;
                        break;
                }
                 
             
            }
          }    
    return 0;    
    }
    OP needed to see how to use a switch inside of a loop anyways, oh well, I was hoping to see if one could figure that part out.
    Last edited by userxbw; 12-04-2017 at 08:49 PM.

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    This is still no different than writing
    Code:
    char what = 'y';
    int whatnow = 0;
    while (tolower(what) != 'n') {
       whatnow++;
       if (whatnow == 10) {
          printf("I chased my tail %d times!\n"
                  "Do you still want to see me chase my tail y/n? ", whatnow);
          scanf(" %c", &what);
          whatnow = 0;
       }
    }
    The while loop breaks when the person using the program types the right thing, nothing else matters, not even the switch.

    I wouldn't be so critical, but I think if you want to force someone to notice a feature is missing, you need to make such a thing actually necessary.
    Last edited by whiteflags; 12-04-2017 at 09:02 PM.

  3. #18
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    This is still no different than writing
    Code:
    char what = 'y';
    int whatnow = 0;
    while (tolower(what) != 'n') {
       whatnow++;
       if (whatnow == 10) {
          printf("I chased my tail %d times!\n"
                  "Do you still want to see me chase my tail y/n? ", whatnow);
          scanf(" %c", &what);
          whatnow = 0;
       }
    }
    The while loop breaks when the person using the program types the right thing, nothing else matters, not even the switch.

    I wouldn't be so critical, but I think if you want to force someone to notice a feature is missing, you need to make such a thing actually necessary.
    what a loop or switch?
    With that code you wrote I was trying to do something else. that is why it was getting screwed up. I was thinking one thing and my brain was typing something else, due to motor memory whatever they call it, when you get stuck on something (besides stupid) and it needs to be a different response.Kind of like muscle memory reflects. well whatever I was trying to do it was not working, time was an issue.

    the switch was to change the it to the right conditions. why not utilize it if you're going to be using it anyways inside of a loop? that was the only thing here on the switch and loop criteria. Because they have already finished theirs. I'll post mine to show what I am talking about.
    because I already know about what you're saying, I was just pressed for time last night. so I got crap results. Okay
    Code:
    #include <stdio.h>
    
    double celsius_to_fahrenheit(double input) 
    { return input * 1.8 + 32; }
    double fahrenheit_to_celsius(double input) 
    { return  (input - 32) * .5556; }
     //K = C + 273.15
    double fahrenheit_to_kelvin (double input) 
    { return (fahrenheit_to_celsius(input) + 273.15); }
    double celsius_to_kelvin(double input) 
    { return input + 273.15; }
    
    int main (void)
    {
    
    char choice;
    double temp;
        
            while ( choice != 'q' )
            {
                printf("Enter a selection to convert temperatures.\n"
                "1. Celsius to Fahrenheit\n"
                "2. Fahrenheit to Celsius.\n"
                "3. Celsius to Kelvin\n"
                "4. Fahrenheit to Kelvin\n"
                "5. Quit\n")    ;
                
                scanf(" %c", &choice);
                
                switch (choice)
                {
                    case '1' :
                        printf("enter tempiture in Celsius\n");
                        scanf(" %lf", &temp);
                        printf("\nCelsius %.2lf\nFahrenheit %.2lf\n",temp, celsius_to_fahrenheit(temp));
                        break;
                    case '2':
                        printf("enter tempiture in Fahrenheit\n");
                        scanf(" %lf", &temp);
                        printf("\nFahrenheit %.2lf\nCelsius %.2lf\n",temp, fahrenheit_to_celsius(temp));
                        break;
                    case '3':
                        printf("enter tempiture in Celsius\n");
                        scanf("%lf", &temp);
                        printf("\nCelsius %.2lf\nKelvin %.2lf\n",temp, celsius_to_kelvin(temp));
                        break;
                    case '4':
                        printf("enter tempiture in Fahrenheit\n");
                        scanf(" %lf", &temp);
                        printf("\nFahrenheit %.2lf\nKelvin %.2lf\n",temp, fahrenheit_to_kelvin(temp));
                        break;
                    case '5':
                        printf("\naddio\n");
                        choice = 'q';
                        break;
                    default:
                    break;
            }
            printf("\n"); // formating
                        
        }
    return 0;    
        
    }
    because choice is already not equal to 'q' it just drops straight into the loop. Now everything can be contained within it, instead of the print message then go into loop then repeat the same message again and again until loop ends scenario.

    selection 5 then assigns choice to 'q' therefore meeting the requirements to stop the loop.
    Last edited by userxbw; 12-05-2017 at 11:05 AM.

  4. #19
    Banned
    Join Date
    Aug 2017
    Posts
    861
    There, that is what I was shooting for. Assigning the var to a value that is needed regardless of the input. usually y or n but needs q to quit.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    
    int main (void)
    {
        char what = '\0';
        int howmany = 0;
        
        while (what != 'q')
        {
            printf("look at me chasing my tail %d\n", howmany+1);
            howmany++;
            if ( howmany == 3 )
            {
                printf("Do you want to see that again? y/n\n");
                scanf(" %c", &what);
                printf("what = %c\n", what);
                if(tolower(what) == 'n')
                    what = 'q';
                else
                    howmany = 0;
            }
            
        } 
        printf("what = %c\n", what);
        
    return 0;    
    }
    Code:
    look at me chasing my tail 1
    look at me chasing my tail 2
    look at me chasing my tail 3
    Do you want to see that again? y/n
    n
    what = n
    what = q
    I do not know why it was not working last night ( that was p issing me off) when it is working now. I got to look closer at that other code. It was not making the what = q, so it was failing. here anyways. Don't ask me what I was thinking last night when I wrote that other bunk.
    Last edited by userxbw; 12-05-2017 at 11:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Temperature analyzer C program help
    By BlackUmbreon in forum C Programming
    Replies: 12
    Last Post: 02-17-2017, 04:17 PM
  2. Temperature conversion program bug.
    By prafiate in forum C Programming
    Replies: 6
    Last Post: 08-13-2012, 09:40 AM
  3. Temperature Conversion Program
    By llind212 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2011, 11:37 PM
  4. Need help with temperature conversion program
    By Aequitas in forum C Programming
    Replies: 4
    Last Post: 03-06-2011, 08:28 PM
  5. military time converter using functions
    By rfoor in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2010, 09:52 PM

Tags for this Thread