Newbie baffled

This is a discussion on Newbie baffled within the C Programming forums, part of the General Programming Boards category; Hello everyone! This is my 1st post here, in fact I just stumbled across these forums as I attempted to ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    UK
    Posts
    6

    Newbie baffled

    Hello everyone! This is my 1st post here, in fact I just stumbled across these forums as I attempted to solve a problem I have come across. I am a novice programmer trying to teach myself C using IvorHorton's Beginning C 3rd ed.

    I'm only at the end of chapter 3 and although I've been enjoying things I'm stumped. One of the exercises in Chapter 3 is to write a simple fahrenheit to celsius converter program. I made what I though was a pretty good fist of it but I can't get it to compile using either Visual C++.Net 2003 or Dev C++. Interestingly, both compilers seem to be complaining not necessarily about my code, but about some lines in the #included stdio header file.

    Specifically, VC++ reports error C2059: syntax error : 'type' and the offending line it flags up in stdio.h is:

    "typedef _W64 unsigned int size_t;"

    Does this have anything to do with the fact that my PC is an Athlon64? Is there perhaps updated header fils I should download, or is my code just borked?

    Anyway, here's my source code:

    Code:
    /**************************************
     *                                      *
     * Solution to Exercise 3.1, page 140 *
     * Temperature converter program      *
     * By Steven MacDiarmid. 16/2/05      *
     *                                      *
     ************************************/*
     
     #include <stdio.h>
     #include <stdlib.h>
     
     int main(void)
     {
        int choice;
        double temp = 0.0, fahr = 0.0, celsius = 0.0;
        
        printf("\n*******************************************************");
        printf("\nTemperature Converter\n");
        printf("\n(C) 2005 SGM Systems");
        
        printf("\n\n1. Convert temperature from degrees Celsius to Fahrenheit.");
        printf("\n2. Convert temperature from degrees Fahenheit to Celsius.");
        scanf("%d", &choice);
        
        switch(choice)
        {
            case 1:
                printf("\n\nPlease enter a temperature in Celsius: ");
                scanf("%lf", temp);
                
                fahr = temp * 1.8 + 32.0;
                printf("\n\n%lf degrees F = %lf degrees C", temp, celsius);
                break;
                
            case 2:
                printf("\n\nPlease enter a temperature in Fahrenheit: ");
                scanf("%lf", temp);
                
                celsius = (temp - 32.0) * 5.0 / 9.0;
                printf("\n\n%lf degrees C = %lf degrees F", temp, fahr);
                break;
                
            default:
                printf("\n\nInvalid selection, enter 1 or 2.");
                break;
        }
        
        system("pause");
        
        return 0;
    }


    I'm sure it's a pretty ham-fisted attempt but given my limited knowledge I think this should compile. If anyone can spot something amiss I'd be very grateful.

    Many thanks, Steve

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    /**************************************
     *                                      *
     * Solution to Exercise 3.1, page 140 *
     * Temperature converter program      *
     * By Steven MacDiarmid. 16/2/05      *
     *                                      *
     ************************************/*
    The error is in your commenting above. The commenting should be terminated by */ not /*

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    I tryed this program, this program crashes when the user enters any kind of input when the user is prompted to enter the temperature.

    someone correct me if im wrong but you have not used anything that resmbles user input.

    when you enter a value, temp is always initlaized as 0 and the program cant do that division. so you need to initialize temp to the value the user inputs.

    edit: try this change
    Code:
    	int temp;
    and
    Code:
    scanf("%lf", &temp);
    Last edited by InvariantLoop; 02-16-2005 at 03:51 PM.
    When no one helps you out. Call google();

  4. #4
    Registered User
    Join Date
    Feb 2005
    Location
    UK
    Posts
    6
    Quote Originally Posted by bithub
    Code:
    /**************************************
     *                                      *
     * Solution to Exercise 3.1, page 140 *
     * Temperature converter program      *
     * By Steven MacDiarmid. 16/2/05      *
     *                                      *
     ************************************/*
    The error is in your commenting above. The commenting should be terminated by */ not /*
    How embarrassing! Thanks. Now to get he damn thing to stop crashing! Thanks for all your patience!

    Cheerd, Steve

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    read the above post
    When no one helps you out. Call google();

  6. #6
    Registered User
    Join Date
    Feb 2005
    Location
    UK
    Posts
    6
    Quote Originally Posted by InvariantLoop
    edit: try this change
    Code:
        int temp;
    and
    Code:
    scanf("%lf", &temp);
    Thanks! Reading your comments made me notice I'd left out those pesky "&" signs. It works now, thanks to you and bithub. Thanks a million folks, I'm sure I'll be back to bug you all some more - there's only another 481 pages to go!

    Cheers, Steve

    PS, here's the final working code if anyone's interested, feel free to criticise and rip to shreds - it's the only way to learn!


    Code:
    /**************************************
     *                                      *
     * Solution to Exercise 3.1, page 140 *
     * Temperature converter program      *
     * By Steven MacDiarmid. 16/2/05      *
     *                                      *
     **************************************/
     
     #include <stdio.h>
     #include <stdlib.h>
     
     int main(void)
     {
        int choice;
        double temp = 0.0, fahr = 0.0, celsius = 0.0;
        
        printf("\n*******************************************************");
        printf("\nTemperature Converter\n");
        printf("\n(C) 2005 SGM Systems");
        
        printf("\n\n1. Convert temperature from degrees Celsius to Fahrenheit.");
        printf("\n2. Convert temperature from degrees Fahenheit to Celsius.");
        scanf("%d", &choice);
        
        switch(choice)
        {
            case 1:
                printf("\n\nPlease enter a temperature in Celsius: ");
                scanf("%lf", &temp);
                
                fahr = temp * 1.8 + 32.0;
                printf("\n\n%.lf degrees F = %.2lf degrees C\n\n", temp, fahr);
                break;
                
            case 2:
                printf("\n\nPlease enter a temperature in Fahrenheit: ");
                scanf("%lf", &temp);
                
                celsius = ((temp - 32.0) * 5.0) / 9.0;
                printf("\n\n%.lf degrees C = %.2lf degrees F\n\n", temp, celsius);
                break;
                
            default:
                printf("\n\nInvalid selection, enter 1 or 2.");
                break;
        }
        
        system("pause");
        
        return 0;
    }

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    feel free to criticise and rip to shreds
    Not bad, and nicely formatted too. The only complaint I have is your use of system. system takes a pointer to a char (if you don't know what this is - nevermind) as a parameter - you can also supply a string literal - just a string of text in quotes. It executes the command you give it as though you were typing it yourself at the command prompt. So there is a "pause" program somwhere in the system folders on your PC. Well what happens if this program isn't there? It could've been deleted or you could be on a different system. It could also have been replaced with something else, which would cause some interesting problems. Refer to the FAQ for some alternatives.

    edit: Specifically, here:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    Last edited by sean; 02-16-2005 at 04:15 PM.

  8. #8
    Registered User
    Join Date
    Feb 2005
    Location
    UK
    Posts
    6
    Quote Originally Posted by sean_mackrory
    Not bad, and nicely formatted too. ....

    edit: Specifically, here:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    Thanks for your kind words Sean. I sort of suspected system would be deprecated for being non-portable code. I checked out the link and when I try to implement the C specific solution using getchar() VC++ throws errors about a missing semicolon on the line int ch; (even though there's one there) and about ch being undefined (even though Visual Studio reports ch as "int ch" in the tooltip when I hover my mouse over it. The funny thying is, if I copy and paste the code into a blank program skeleton it compiles fine, but not if I paste into my own code.

    Here's where I'm at now:

    Code:
    /**************************************
     *									  *
     * Solution to Exercise 3.1, page 140 *
     * Temperature converter program	  *
     * By Steven MacDiarmid. 16/2/05      *
     *									  *
     **************************************/
     
     #include <stdio.h>
      
     int main(void)
     {
    	int choice;
    	double temp = 0.0, fahr = 0.0, celsius = 0.0;
    	
    	printf("\n*******************************************************");
    	printf("\nTemperature Converter\n");
    	printf("\n(C) 2005 SGM Systems");
    	
    	printf("\n\n1. Convert temperature from degrees Celsius to Fahrenheit.");
    	printf("\n2. Convert temperature from degrees Fahenheit to Celsius.");
    	scanf("%d", &choice);
    	
    	switch(choice)
    	{
    		case 1:
    			printf("\n\nPlease enter a temperature in Celsius: ");
    			scanf("%lf", &temp);
    			
    			fahr = temp * 1.8 + 32.0;
    			printf("\n\n%.lf degrees F = %.2lf degrees C\n\n", temp, fahr);
    			break;
    			
    		case 2:
    			printf("\n\nPlease enter a temperature in Fahrenheit: ");
    			scanf("%lf", &temp);
    			
    			celsius = ((temp - 32.0) * 5.0) / 9.0;
    			printf("\n\n%.lf degrees C = %.2lf degrees F\n\n", temp, celsius);
    			break;
    			
    		default:
    			printf("\n\nInvalid selection, enter 1 or 2.");
    			break;
    	}
    	
    	int ch;
    	printf ("Press [Enter] to continue");
    	while ((ch = getchar()) != '\n' && ch != EOF);
    	
    	return (0);
    }
    Thanks for everyone's help. btw, is the last line right - return (0);?
    I've never seen brackets placed round the argument to return like that.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > int ch;
    If you're getting compile errors, maybe your compiler wants to see this declaration at the top. The newer standard allows what you have.

    >is the last line right - return (0);?
    I think it's a style issue. I prefer just return 0;.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  2. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 11:53 AM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

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