'Else without a previous if' - What Am I doing wrong?

This is a discussion on 'Else without a previous if' - What Am I doing wrong? within the C Programming forums, part of the General Programming Boards category; Hello programmers! I am new to c programming and just started a course 3 weeks ago, and now we need ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    6

    'Else without a previous if' - What Am I doing wrong?

    Hello programmers!
    I am new to c programming and just started a course 3 weeks ago, and now we need to make a project where it will be asking you a few things and then do the progress...
    But I am having a few problems, could anybody help me please?


    Code:
    void PellerK(double *Produkt, double *Kvot){
      printf("Produkt eller Kvot?\n");    /*Asking to multiply or divide*/
      scanf("%lf %lf",&Produkt,&Kvot);
    
    if (PellerK == Produkt); {
    printf("Produkten blir = %d\n");
    }
    else (PellerK == Kvot); {
    printf("Kvoten blir = %d\n");
    }
    This part above is when I encounter my problem...
    When I try to run this I get an error where it says;
    'Else without a previous if' - What am I doing wrong? Please help me out

    If you see that I am doing anything else which is wrong, please feel free to help me out with that aswell!

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    Drop the semicolons directly after the if condition and the else condition.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,650
    The correct syntax for "if-else" is:

    Code:
    if(argument)
    {
        // ...
    }
    else
    {
        // ...
    }
    Don't put an argument after "else".

    Also, remove the semi-colon after the "if()" - it does not belong there.

    Code:
    scanf("%lf %lf",&Produkt,&Kvot);
    "scanf()" expects an address of the variables you're scanning. The two variables you're storing to are pointers (already addresses), so you don't need the & before them.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    > if (PellerK == Produkt);
    Remove the trailing ;

    > else (PellerK == Kvot);
    An else doesn't have an expression.
    What this is is the statement which is evaluated when the else part is taken.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    Wow, Thanks for the great quick respond!
    But when I remove the semicolons I get a new error...

    "Error: expected ";" before "{" token"

    Edit: I found out what I was doing wrong...
    Last edited by Cofex; 02-26-2013 at 02:08 PM. Reason: Found out the solution

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    Once again, thanks for the quick resonse and the great answers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple 'else' without a previous 'if'
    By Hazuki in forum C++ Programming
    Replies: 5
    Last Post: 07-03-2012, 12:38 PM
  2. previous definition is here
    By AaronP in forum C Programming
    Replies: 4
    Last Post: 04-02-2012, 09:21 AM
  3. My Previous Project...
    By thecrazycoerian in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2004, 10:42 AM
  4. previous example required
    By darfader in forum Windows Programming
    Replies: 1
    Last Post: 09-01-2003, 06:00 AM
  5. First Last Next and Previous
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 04-17-2002, 11:05 AM

Tags for this Thread


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