Thread: Newbie to C

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    17

    Newbie to C

    Hi,
    I just wanted to know if someone could help me with if and else statements.
    Im pretty new to C programming,but im finding some aspects of if and else statements a bit difficult. I'm having a problem with this assignment.

    Write a program which helps an insurance broker determine insurance premiums according to the following table.

    Car Age Engine Size Convictions Message
    >=15 >=2000 >=3 Policy loaded by 45%
    >=15 >=2000 < 3 Policy loaded by 20%
    <15 < 2000 < 3 no loading


    The program first needs to aquire the age of the car, the engine size and the number of convictions from the user, and then it should print out the appropriate message.

    The problem im having is how to set the program up, in terms of if and else statements, and how to write the program so it gets all the information from the user, and then works out which policy they get if any. If it is possible could someone help me with another similiar program, so when I understand it, I can do this write this program myself.

    Any help with be much appreciated.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Check out the homework policy Here

    Show us what you've got so far, and then we'll help you straighten things out. We are not here to write it for you [it would probably take me a little longer than writing this reply - but I've been doing this sort of thing for about 20 years now].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    17
    Hi Matsp,
    Sorry if it sounded like I wanted you to do my work, I didn't mean it to.
    Here is my code so far,

    Code:
    #include<stdio.h>
    
    void main()
    {
    	int CarAge, EngineSize, Convictions, Message;
    	printf("Please enter the age of the car ");
    	scanf("&#37;i", &CarAge);
    	printf("Please enter the size of the engine ");
    	scanf("%i", &EngineSize);
    	printf("Please enter the any convictions ");
    	scanf("%i", &Convictions);
    	
    	if (CarAge >= 15)
    	else 
    	    printf("No Loading");
    
        if (EngineSize >= 2000)
    	else 
    	    printf("No Loading");
    
    	if (Convictions >= 3)
            printf("policy loaded by 45%");
    	else if (Convictions < 3)
    	    printf("policy loaded by 40%");
    	else 
    	    printf("No Loading");
    	
    }
    Problem im having is what do I type into the 'if' part, so if the CarAge is valid, it will store it
    and move onto the Engine Size, and then if the Engine Size is valid, it will store it and move it onto the Convictions. Then if conviction is valid it should print out the appropriate message.

    Thanks for helping.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think all you need to do is to enclose the relevant blocks of code with curly braces
    Code:
    {}
    to make it follow the rules.

    Your first if-statement looks a bit funny - why not reverse the whole thing?
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    17
    Hi matsp

    Is this what you mean about adding those curly brackets

    Code:
    #include<stdio.h>
    
    void main()
    {
    	int CarAge, EngineSize, Convictions, Message;
    	printf("Please enter the age of the car ");
    	scanf("%i", &CarAge);
    	printf("Please enter the size of the engine ");
    	scanf("%i", &EngineSize);
    	printf("Please enter the any convictions ");
    	scanf("%i", &Convictions);
    	
    	{
    	if (CarAge >= 15)
    	else 
    	    printf("No Loading");
    	}
    
    	{
        if (EngineSize >= 2000)
    	else 
    	    printf("No Loading");
    	}
    
    	{
    	if (Convictions >= 3)
            printf("policy loaded by 45%");
    	else if (Convictions < 3)
    	    printf("policy loaded by 40%");
    	else 
    	    printf("No Loading");
    
    	}
    
    	
    }
    At the moment when I try to run the program I get these two errors:

    1>.\workshop3.cpp(15) : error C2181: illegal else without matching if
    1>.\workshop3.cpp(21) : error C2181: illegal else without matching if


    The things is what do I have to write so it checks that the CarAge value, if it is valid it then moves on the Engine Size, if it isnt it gives you the error message

    Thanks for you help.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    1. main returns an int, not void. No exceptions.

    2. 1>.\workshop3.cpp
    You're using a C++ compiler to compile C code. Rename your files to be prog.c, then you should be invoking your C compiler.

    3. illegal else without matching if
    The curly braces would be something like this
    Code:
    if ( condition ) {
    } else if ( anotherCondition ) {
    } else {
    }
    If you do this, then it should be more apparent what is mis-matched.

    A consistent approach to indentation certainly helps as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  2. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  3. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  4. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 12:27 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM