Thread: stuck on "else if ()" situation, can't figure out the problem. all looks correct.

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    3

    stuck on "else if ()" situation, can't figure out the problem. all looks correct.

    i wrote this code which gives error to the condition else if. i tried everything. if i remove this part the code works fine.

    here is the code
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <cs50.h>
    int main(void)
    {
    
      float n;
      do {
        // prompt user for change
        n = get_float("change owed: ");
      }
      while (n < 0);
      int cents = round(n * 100);
      // perform division
      // computes quotient
      int quotient = cents / 25;
    
      // computes remainder
      int remainder = cents % 25;
    
      printf("Quotient = %d\n", quotient);
    
      printf("Remainder = %d\n", remainder);
    
      if (remainder >= 10) {
        int quotient2 = remainder / 10;
        int remainder2 = remainder % 10;
        printf("Quotient2 = %d\n", quotient2);
        printf("Remainder2 = %d\n", remainder2);
        if (remainder2 >= 5) {
          int quotient4 = remainder2 / 5;
          int remainder4 = remainder2 % 5;
          printf("Quotient4 = %d\n", quotient4);
          printf("Remainder4 = %d\n", remainder4);
          int addition, subtraction, multiplication, division, modulus;
          addition = quotient + quotient4 + remainder4;
          printf("number of coins: %d\n", addition);
        } else {
          int addition, subtraction, multiplication, division, modulus;
          addition = quotient + quotient2 + remainder2;
          printf("number of coins: %d\n", addition);
        }
    
        else
      if (remainder < 10 && >=5) {
        int quotient3 = remainder / 5;
        int remainder3 = remainder % 5;
        printf("Quotient3 = %d\n", quotient3);
        printf("Remainder3 = %d\n", remainder3);
        int addition, subtraction, multiplication, division, modulus;
        addition = quotient + remainder;
        printf("number of coins: %d\n", addition);
      } else {
        printf("%d\n", remainder);
      }
      }
    
    }
    thank you in advance for your input.
    #
    Last edited by Salem; 04-02-2020 at 01:15 PM. Reason: removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,968
    This is why indentation is so important.
    If you have lousy indentation, it's too hard to track what belongs where.
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <cs50.h>
    int main(void)
    {
      float n;
      do {
        // prompt user for change
        n = get_float("change owed: ");
      }
      while (n < 0);
      int cents = round(n * 100);
      // perform division
      // computes quotient
      int quotient = cents / 25;
      // computes remainder
      int remainder = cents % 25;
    
      printf("Quotient = %d\n", quotient);
      printf("Remainder = %d\n", remainder);
    
      if (remainder >= 10) {
        int quotient2 = remainder / 10;
        int remainder2 = remainder % 10;
        printf("Quotient2 = %d\n", quotient2);
        printf("Remainder2 = %d\n", remainder2);
        if (remainder2 >= 5) {
          int quotient4 = remainder2 / 5;
          int remainder4 = remainder2 % 5;
          printf("Quotient4 = %d\n", quotient4);
          printf("Remainder4 = %d\n", remainder4);
          int addition, subtraction, multiplication, division, modulus;
          addition = quotient + quotient4 + remainder4;
          printf("number of coins: %d\n", addition);
        } else {
          int addition, subtraction, multiplication, division, modulus;
          addition = quotient + quotient2 + remainder2;
          printf("number of coins: %d\n", addition);
        }
        //else
        if (remainder < 10 && >=5) {
          int quotient3 = remainder / 5;
          int remainder3 = remainder % 5;
          printf("Quotient3 = %d\n", quotient3);
          printf("Remainder3 = %d\n", remainder3);
          int addition, subtraction, multiplication, division, modulus;
          addition = quotient + remainder;
          printf("number of coins: %d\n", addition);
        } else {
          printf("%d\n", remainder);
        }
      }
    }
    You can't have another else at line 40, because you've already got one at line 35.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    3
    Hi Salem; thank you very much for taking the time to help. if you check the indentations, you will find that the else in line 35 belongs to the nested if (remainder2>=5) while the else if in line 40 belongs to the main if in line 22. also, i tried to change else if to if and tried many other changes in vain. i even used while. all the same, the code does not execute this part although it generates error with some of the changes (error: expected expressions). i even tried changing the value of 5 and made it 4 just to check if it will be executed.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,719
    Quote Originally Posted by SELKERDANY
    while the else if in line 40 belongs to the main if in line 22
    No, it doesn't. That else is a syntax error. If you do want that else to match up with the very first if, you need to precede it by a closing brace so that the block associated with that if would end.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    3
    Quote Originally Posted by laserlight View Post
    No, it doesn't. That else is a syntax error. If you do want that else to match up with the very first if, you need to precede it by a closing brace so that the block associated with that if would end.
    Thank you very much laserlight, i was actually able to detect this error and solved the problem (finally) and then read your post and found that you were the only one who detected the error thank you very much for this. after i detected the error i was astonished that i did not see it in any of the tens of times i checked the code.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,968
    I just love the innuendo that chopped liver over here made no contribution at all to the proceedings.
    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. A "Simple" Array/String Problem. "Help" ASAP needed
    By AirulFmy in forum C Programming
    Replies: 10
    Last Post: 08-19-2015, 04:30 AM
  2. Replies: 2
    Last Post: 12-08-2014, 08:12 PM
  3. Replies: 10
    Last Post: 08-09-2012, 12:48 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread