Thread: New guy, syntax error

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

    New guy, syntax error

    Hey everybody, I'm super new at this and toughing my way through a project. Hoping someon ecould spot my syntax error, I feel like its right in front of me and I'm just missing something!
    Code:
    if ( INPUT == 3 ) {
                               printf("Current balance: $%1.2f.\n", CREDIT);
                               printf("Minimum balance: $%1.2f.\n", MIN_BALANCE);
                               }
                                }
    
                               if (INPUT == 4)
                                  {
                                   INTREST = (MIN_BALANCE*0.035)/12.00;
                                   FINAL_BALANCE = CREDIT + INTEREST;
                                   printf("Statement of Account:\n");
                                   printf("Credit transactions: %i,\n", CREDIT_TRANS);
                                   printf("Debit transactions: %i,\n", DEBIT_TRANS);
                                   printf("Current balance: $%1.2f,\n", CREDIT);
                                   printf("Minimum balance: $%1.2f,\n", MIN_BALANCE);                   
                                   printf("Interested computed: $%1.2f,\n", INTREST);
                                   printf("Final balance: $%1.2f,\n", FINAL_BALANCE);
                                   printf("Good Bye!\n");
                                 }
    The syntax error is showing to be right in front of the line
    Code:
    if (INPUT == 4)
    thanks in advance!! I've been going crosseyed trying to figure this out...

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Few things to consider:

    1. Next time give us the syntax error. It's a waste of time to make us guess.
    2. By convention, fully capitalized variables are usually macros in disguise, so if they are really variables, I wouldn't name them like that.
    3. Your indentation sucks, which is probably what is confusing you. You should pick an indentation style that makes sense to you and get used to it.
    4. You have an if statement checking if INPUT is equal to 3, and you properly have an open brace, but you close with 2 braces instead of 1 after the two printf() statements. This has an effect on code before your if statement, which I can't help you with because you didn't give us that code. This extra closing brace is probably your problem. Delete one of those braces.


    And of course.... Welcome to cboard.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    thanks, it turned out to be that extra bracket...

    Im still really confused on things like syntax errors, my compiler is kind of sketchy (imo) and I wish it would give me a little more info....

    I know my indentation is awful, it looked good when i started but got ugly as I started changing things around. I intend to clean it up for sure though, and i need to go back and add notes and stuff too.

    Thanks again.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The problem for the compiler is that it's very hard to determine the difference between "something completely unexpected" and "an early end of a function" (which is the common case for "many and strange errors from the compiler").

    The problem MAY be that you had an extra brace some 25 lines above - but the errors happen when you "run out of braces" (end of function is usually where the errors come, because you now have "something that belongs in a function, but isn't in a function".

    Just like we can sort of read some typos in text, but if I put put "chapter 2" in the middle of a sentence, and you start reading after that as if you expected a new chapter to start, then you'd most likely be somewhat confused to.

    The real problem with this is that there's just about ANYTHING that could be right or wrong at this point - it's very difficult to say "this is definitely a brace too much somewhere above".

    --
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > it looked good when i started but got ugly as I started changing things around
    Any half-decent code editor has features to maintain code indentation.

    Also, keeping it tidy is the way to prevent this kind of mis-match in the first place, so spending a few seconds extra keeping it tidy where necessary will prevent this kind of problem from ever appearing.
    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.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    Okay, heres my (almost) final program... thers only one more thing not working for me.
    Code:
    /* Adam Jones
       COP3223 Sec 0001
       Lab 3, part 2
       10/13/07
    */
      
    
    
     #include <stdio.h>
    
    
    
      int main(void) {
      //Declare all variables  
      float CREDIT, DEPOSIT, NEW_BALANCE, NEW_CREDIT, WITHDRAWL, MIN_BALANCE, INTEREST, FINAL_BALANCE;
      int INPUT, CREDIT_TRANS = 0, DEBIT_TRANS = 0, MONTH, NUMBER_OF_DAYS, DATE1, DATE2;
      
      //Set starting balance
      CREDIT = 2000.00;
    
           //Request user to enter a transaction month
            printf("Enter the transaction month:\n");
            scanf("%i", &MONTH);
                  //Define number of days in month entered
                  if (MONTH == 1) {
                      NUMBER_OF_DAYS = 31;
                      }
                           else if (MONTH == 2) {
                           NUMBER_OF_DAYS = 28;
                           }
                             else if (MONTH == 3) {
                              NUMBER_OF_DAYS = 31;
                              }
                                else if (MONTH == 4) {
                                NUMBER_OF_DAYS = 30;
                                }               
                                  else if (MONTH == 5) {
                                  NUMBER_OF_DAYS = 31;
                                  }
                                    else if (MONTH == 6) {
                                    NUMBER_OF_DAYS = 30;
                                    }
                                      else if (MONTH == 7) {
                                      NUMBER_OF_DAYS = 31;
                                      }
                                        else if (MONTH == 8) {
                                        NUMBER_OF_DAYS = 31;
                                        }
                                          else if (MONTH == 9) {
                                          NUMBER_OF_DAYS = 30;
                                          }
                                            else if (MONTH == 10) {
                                            NUMBER_OF_DAYS = 31;
                                            }
                                              else if (MONTH == 11) {
                                              NUMBER_OF_DAYS = 30;
                                              }
                                                else if (MONTH == 12) {
                                                NUMBER_OF_DAYS = 31;
                                                }
                                 
            //Display number of days for month and current balance
            printf("Number of days in this month: %i\n", NUMBER_OF_DAYS);
            
            printf("Your current balance is $%2.2f.\n", CREDIT);
    
            DATE1 = 1;
            
            while ( INPUT != 4 ) {
            //list transaction options; request user to choose one
            printf("Transaction options:\n");
            printf("1 - Deposit funds (credit transaction) \n");
            printf("2 - Withdraw funds (debit transaction) \n");
            printf("3 - Print statement of account \n");
            printf("4 - Compute interest and exit \n");
            printf("Please indicate your option: \n");
            scanf("%i", &INPUT);
                    
                    //request day of month to be entered
                    if (INPUT != 4) {
                       printf("Please enter a valid date from %i to %i:\n", DATE1, NUMBER_OF_DAYS);
                       scanf("%i\n", &DATE2);
                       }
                        //take care of invalid dates
                        if (DATE2 < DATE1){
                          printf("Please enter a valid date from %i to %i:\n", DATE1, NUMBER_OF_DAYS);
                          scanf("%i\n", &DATE2);
                        //Can't figure out the bug here; Program does not execute to next function, but if user 
                        //enters another number the following function executes. worked on this for three days....
                          if(DATE2 >= DATE1){
                             DATE1 = DATE2;
                             } 
                             }
                          //deposit function
                          if ( INPUT == 1 ) {
                             printf("How much do you want to deposit?\n");
                             scanf("%f", &DEPOSIT);
                             CREDIT = CREDIT + DEPOSIT;
                             printf("Your current balance is $%1.2f.\n", CREDIT);
                             CREDIT_TRANS++;
                             }
    
                           //debit function
                           if ( INPUT == 2 ) {
                              printf("How much do you want to withdrawl?\n");
                              scanf("%f", &WITHDRAWL);
                              CREDIT = CREDIT - WITHDRAWL;
                              printf("Your current balance is $%1.2f.\n", CREDIT);
                              DEBIT_TRANS++;
                              MIN_BALANCE = 2000.00;
    
                           //minimum balance calculator
                           if ( MIN_BALANCE > CREDIT ) {
                               MIN_BALANCE = CREDIT;
                               }
                          }
    
                            //balance function
                            if ( INPUT == 3 ) {
                                printf("Current balance: $%1.2f.\n", CREDIT);
                                printf("Minimum balance: $%1.2f.\n", MIN_BALANCE);
                                }
                                }
                               //statement function
                               if (INPUT == 4){
                                   INTEREST = (MIN_BALANCE*0.035)/12.00;
                                   FINAL_BALANCE = CREDIT + INTEREST;
                                   printf("Statement of Account:\n");
                                   printf("Credit transactions: %i,\n", CREDIT_TRANS);
                                   printf("Debit transactions: %i,\n", DEBIT_TRANS);
                                   printf("Current balance: $%1.2f,\n", CREDIT);
                                   printf("Minimum balance: $%1.2f,\n", MIN_BALANCE);                   
                                   printf("Interest computed: $%1.2f,\n", INTEREST);
                                   printf("Final balance: $%1.2f,\n", FINAL_BALANCE);
                                   printf("Good Bye!\n");
                                   }
                                
            system ("PAUSE");
            return 0;
            }
    in the section where I request the user to "enter a valid date from x to x", after the date is entered, the program simply goes to a new line. Then if you enter any number, it automatically proceeds to carry out the transaction function requested (example: if you type 200 and then enter, it will print ask to enter the deposit amount, but the deposit is already made" Im almost there, what am I missing?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM