Thread: slightly stuck with C code

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    4

    slightly stuck with C code

    Hey guys im having a bit of trouble compiling my code. I dont know where im going wrong.. Can anyone please help me out?
    the error codes im getting are:
    Error E2109 lab8.c 68: Not an allowed type in function readType.
    Error E2109 lab8.c 71: Not an allowed type in function readType.
    Error E2193 lab8.c 82: Too few parameters in call to 'readTemp' in function main.
    Error E2193 lab8.c 83: Too few parameters in call to 'readType' in function main.
    Code:
    #include <stdio.h>
    /*
    This program is designed to convert kelvin into
    fahrenhiet or celsius.
    */
    
    
    
    
    void convertC(int kelvin, int celsius)
    {
        (celsius = (kelvin - 273));
         if(celsius < 1)
            {
                printf("The water is in a solid state at %d degrees celsius.\n", celsius);
            }
            else if(celsius > 0 && celsius < 100)
            {
                printf("The water is in a liquid state at %d degrees celsius.\n", celsius);
            }
            else
            {
                printf("The water is in a gas state at %d degrees celsius.\n", celsius);
            }
    }
    void convertF(int fahrenhiet, int celsius, int kelvin)
    {
        (celsius = (kelvin - 273));
        (fahrenhiet = (9 * celsius / 5) + 32);
         if(fahrenhiet < 33)
            {
                printf("The water is in a solid state at %d degrees fahrenhiet.\n", fahrenhiet);
            }
            else if(fahrenhiet > 32 && fahrenhiet < 212)
            {
                printf("The water is in a liquid state at %d degrees fahrenhiet.\n", fahrenhiet);
            }
            else
            {
                printf("The water is in a gas state at %d degrees fahrenhiet.\n", fahrenhiet);
            }
    }
    void readTemp(int kelvinNumber)
    {
        int kelvin, *ptr;
        char type;
        ptr = &kelvin;
        printf("Please enter a the sample temperature in Degrees Kelvin:>345\n");
        scanf("%d", &kelvin);
        if(kelvin <= 345)
        {
            printf("You did not enter the correct value\n");
            exit(0);
        }
        else
        {
            printf("You entered %dK\n", *ptr);
        }
    }
    
    
    void readType(char type)
    {
        int kelvin, celsius, fahrenhiet;
        printf("Do you wish to convert the temperature to (c) for Celsius, or (f) for Fahrenheit");
        getchar();
        scanf("%c", &type);
        switch(type)
        {
            case 'c' :
            celsius = convertC(kelvin, celsius);
            break;
            case 'f' :
            fahrenhiet = convertF(fahrenhiet, celsius, kelvin);
            break;
            default:
            printf("You did not enter correct letter!");
            break;
        }
    }
    
    
    int main()
    {
       printf("Welcome to the temperature conversion program!\n");
       readTemp(kelvin);
       readType(type);
    }

  2. #2
    Registered User PenCzar's Avatar
    Join Date
    Apr 2016
    Location
    New York
    Posts
    7
    You need to declare the variables before you can pass them to a function.

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    4
    Quote Originally Posted by PenCzar View Post
    You need to declare the variables before you can pass them to a function.
    could you give me an example please?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Quote Originally Posted by suspense View Post
    could you give me an example please?
    Code:
    #include <stdio.h>
    
    void print_int(int a);
    
    int main(void)
    {
        int x = 5;  // declare and initialize variable named "x"
    
        print_int(x);  // pass variable named "x" to the function
    
        return 0;
    }
    
    void print_int(int a)
    {
        printf("%d\n", a);
    }

  5. #5
    Registered User
    Join Date
    May 2016
    Posts
    6

    converting temperatures

    Code:
    void convert_kelvin_to_c(int kelvin) {
        int celsius = (kelvin - 273);
        printf("%d\n", celsius);
    }
    
    
    main() {
        convert_kelvin_to_c(500);
    }
    This function takes an integer, subtracts 273 from it, assigns the new value to the variable 'celsius' and then prints it out.
    this program prints "227"

    500 K = 226,85 NOT 227
    so you must use a floating point value instead.

    Code:
    void convert_kelvin_to_c(float kelvin) {
        /* "%.2f" means a float with only 2 decimals trailing printed */
        float celsius = (kelvin - 273.15);
        printf("%.2f\n", celsius);
    }
    
    
    main() {
        convert_kelvin_to_c(500);
    }

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by wrvc View Post
    500 K = 226,85 NOT 227
    Absolute scientific accuracy is not the point of this exercise, and is therefore off topic.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    May 2016
    Posts
    6
    Quote Originally Posted by Elkvis View Post
    Absolute scientific accuracy is not the point of this exercise, and is therefore off topic.
    Fair enough.

    Code:
    #include <stdio.h>
    
    
    /* add this logic */
    float kelvin_to_celsius(int kelvin)
    {
    	printf("%d kelvin in celsius is ???\n", kelvin);
    	return 0.0f;
    }
    
    
    /* add this logic */
    float kelvin_to_fahrenheit(int kelvin)
    {
    	printf("%d kelvin in fahrenheit is ???\n", kelvin);
    	return 0.0f;
    }
    
    
    int read_kelvin_temp()
    {
    	int kelvin;
    	puts("Enter temperature in Kelvin:");
    	/* if scanf() successfully scanned an integer */
    	if (scanf("%d", &kelvin))
    	{
    		return kelvin;
    	}
    	/* if scanf() couldn't read format -> stdin */
    	else {
    		return 0;
    	}
    }
    
    
    void read_type(int kelvin)
    {
    	puts("Convert to (C)elsius or (F)ahrenheit?");
    	char type;
    	/* if scanf() fails to find a char, set type to 'C' */
    	if (!scanf(" %c", &type))
    	{
    		type = 'C';
    	}
    
    
    	switch(type)
    	{
    		case 'C':
    		case 'c':
    		{
    			kelvin_to_celsius(kelvin);
    			break;
    		}
    		case 'F':
    		case 'f':
    		{
    			kelvin_to_fahrenheit(kelvin);
    			break;
    		}
    		default:
    		puts("Error tbh");
    	}
    }
    
    
    int main()
    {
    	puts("Temperature conversion program");
    	int my_kelvin = read_kelvin_temp();
    	/* if the user didn't input an integer correctly */
    	if (my_kelvin == 0)
    	{
    
    
    	}
    	read_type(my_kelvin);
    	return 0;
    }
    This is what OP wanted but he has to add logic and stuff himself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. slightly stuck. would love your input
    By suspense in forum C Programming
    Replies: 4
    Last Post: 04-01-2016, 09:08 AM
  2. help please, my output is slightly wrong
    By bcd6f in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2012, 11:30 AM
  3. Slightly Disoriented.
    By CrazyNorman in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-11-2005, 12:24 PM
  4. Slightly depraved humor
    By misplaced in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 04-29-2005, 01:34 PM
  5. a slightly different triangle
    By sweetly in forum C++ Programming
    Replies: 7
    Last Post: 10-13-2003, 01:46 AM

Tags for this Thread