Solved

line 26: Parse Error, expecting `NUMBER'

Posted on 2004-10-06
10
405 Views
Last Modified: 2010-04-15
I am trying to figure where my error is coming from.  This program is supposed to give the equivalents of five currencies in USD.  Here is the code:

#include <stdio.h>
main ()
{
      float ad;
      float cd;
      float dk;
      float nk;
      float tb;
      
      int ans;
      
      do
      {
            printf("***** CURRENCY CONVERTER *****");
            printf("1 - Australian Dollars \n");
            printf("2 - Canadian Dollars \n");
            printf("3 - Danish Krone \n");
            printf("4 - Norwegian Kroner \n");
            printf("5 - Thai Bhat \n\n");
            scanf(" %d", &ans);
      }
            while ((ans!='1')&&(ans!='2')&&(ans!='3')&&
                   (ans!='4')&&(ans!='5'));
      switch (ans)
      {
            case ('1') : {ad = 1.38274;
                        printf("\n The equivalent in US Dollars is %.2f", ad);
                        break;}
            case ('2') : {cd = 1.2629;
                        printf("\n The equivalent in US Dollars is %.2f", cd);
                        break;}
            case ('3') : {dk = 6.001;
                        printf("\n The equivalent in US Dollars is %.2f", dk);
                        break;}
            case ('4') : {nk = 6.692;
                        printf("\n The equivalent is US Dollars is %.2f", nk);
                        break;}
            case ('5') : {tb = 41.36;
                        printf("\n The equivalent is US Dollars is %.2f", tb);
                        break;}
      }
      getch();
      return;  
}
0
Comment
Question by:JJ7717
10 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 12243287


The code isn't particularly well structured, and that's probably leading to the "hiding" of this problem.

Take the ';' off of the while() statement.  You're probably seeing an "infinite loop" and this is the reason.


Good Luck,
Kent
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 12243427
Couple notes:

main should be declared as either
int main() or int main(int argc, char **argv)

Also you variable ans is declared as an int.  Therefore in your while loop you should not be comparing it against characters.  For instance, it should be while(ans != 1) not while(ans != '1').
Also you while loop is pretty worthless as it stand since it is considered empty.  The structure of a while loop should be:
while(condition)
{
//...
}

You while loop currently looks like:
while(condition);


0
 
LVL 5

Expert Comment

by:libin_v
ID: 12245415
Since there is a 'do' statement preceding the while I do think the ';' is neccessary
0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 125 total points
ID: 12245489
Do-While requires a semi-colon at the end of while.

The problem is the quotes surrounding the integer values 1-5;

Your while statement should be something like:

while ((ans!=1)&&(ans!=2)&&(ans!=3)&&
                (ans!=4)&&(ans!=5));
   
Similarly,remove the single quotes from the case statements as well.

case 1:

case 2:

etc.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12246507
My compiler says to use:

     double ad;
     double cd;
     double dk;
     double nk;
     double tb;

...

Paul
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12246875
Hi Kdo,
> The code isn't particularly well structured

Good point.
JJ7717, you have a lot of redundancy here. Think about using an array of structs instead:

struct currency{
    char* name;
    char* short_name;
    double rate;
}

then you can define all your currencies nicely:

struct currency all_currencies[]={
    {"Australian Dollar", "AUD", 1.38274},
    {"Euro", "EUR", 0.85345435},
};

...now you should know how to proceed: Loop to print the choices, do the appropriate calculations, etc.

Cheers!

Stefan
0
 

Author Comment

by:JJ7717
ID: 12248307
Thanks to everyone who commented on my problem.  I will put together the suggestions and retool my program.

John
0
 

Expert Comment

by:x25
ID: 12265658
The parse error you have is particularly because of the LPAREN `(' character found after the case keyword. The compiler interprets the value after the `case' as a number. And, hence, the Left parentheses you have is unexpected for the compiler.

When you enclose a digit number between single quotes in the case statement like (case '2': ), this is perfectly legal. However, their actual value is not 2 but actually the ASCII code number for the character 2, which is in fact  50. Which can be very misleading and "hidden" in your program.


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now