Solved

Program won't compile getting error message

Posted on 2004-09-21
23
276 Views
Last Modified: 2008-01-09
Error message, I cannot figure it out:
Miracle C Compiler (r3.2), written by bts.
Compiling C:\Program Files\Miracle C\MichaelExample2.c
main

C:\Program Files\Miracle C\MichaelExample2.c: line 106: Parse Error, expecting `SEP'
'return 0'
aborting compile


Program:

#define VERSION "1.1"
#define INVALID_VALUE -29833
/******************************************************************************
Program filename: conversion.c
         

The program assumes that the user does not enter anything except valid numbers
(including minus sign and decimal point).  That's a bad assumption in the real
world, but okay for this class assignment.
*******************************************************************************/

#include <stdio.h>

int main ()
{
    /**
     * CAUTION: The following two arrays must match each other.
     * If you add to one, you must add to the other.
     * Otherwise, they'll get out of sync.
     */

    // array of conversion rates of foreign currency / USD$1.00
    float afRate[] = {
      107.10,  // Japanese Yen
      0.5472,  // British pounds sterling
      0.8212,  // Euro
      3.7501,  // Saudi Riyal
      1.2424   // Swiss Franc line 30
    };

    char *aszCurrency[] = {
        "Japanese Yen",
        "British Pound",
        "Euro",
        "Riyal",
        "Swiss Franc"
    };

    float cvtdAmount, foreignAmount;
    int input, userWantsExit = 0, nrCurrencies, i;

    nrCurrencies = sizeof(afRate) / sizeof(afRate[0]);  // nr of currencies

    while(!userWantsExit) {
        printf("\n\n  Currency Conversion, v%s \n\n", VERSION);  //displayes title
        printf("Available currencies and equivalence to US$1.00\n\n"); //title
        for (i=0; i<nrCurrencies; i++) {
            printf("%d: %15s   %8.4f\n", i+1, aszCurrency[i], afRate[i]);
             //loop that goes through the area and prints out the menu
        }
        printf("0: %15s\n", "Exit");  
        //once the loop ends print out the last menu choice. which is 0. exit program.  line 54

        printf("\nWhich currency to convert to from? ");
        input = INVALID_VALUE;  // in case user enters non-numeric
        fflush(NULL);       //I think if you get the bad value the loop will go again and flush out all the values.
        scanf("%d", &input); //get the menu choice.

        if ((input <= nrCurrencies) && (input >= 1)) {
            printf("\nAmount of %s to convert: ", aszCurrency[input-1]);
            foreignAmount = (float) INVALID_VALUE;  
            // same thing if a bad value is entered the loop will continue until it is flushed above
            scanf("%f", &foreignAmount);  //get the amount of foreign moneis you want to convert
            if ((float) INVALID_VALUE == foreignAmount) {
               printf("\n*** Invalid amount (alphabetic?); try again.\n");
            }
            printf("\nWhich currency to convert to US$? ");
        input = INVALID_VALUE;  // in case user enters non-numeric  line 70
        fflush(NULL);       //I think if you get the bad value the loop will go again and flush out all the values.
        scanf("%d", &input); //get the menu choice.
        }
            else {
               cvtdAmount = foreignAmount / afRate[input-1];   //Oh good idea! menu choice is the array number..  line 75
               printf("\n%f %s is equivalent to US$%.2f\n(Rate: %f %s to US$1)",
                 foreignAmount, aszCurrency[input-1], cvtdAmount,
                 afRate[input-1], aszCurrency[input-1]);    //all the amounts are then calculated and displayed here.  line 78
            }
            }
               
              if ((input <= nrCurrencies) && (input >= 1)) {
            printf("\nAmount of %s to convert: ", aszCurrency[input-1]);
            foreignAmount = (float) INVALID_VALUE;  
            // same thing if a bad value is entered the loop will continue until it is flushed above   line 85
            scanf("%f", &foreignAmount);  //get the amount of foreign moneis you want to convert
            if ((float) INVALID_VALUE == foreignAmount) {
               printf("\n*** Invalid amount (alphabetic?); try again.\n");
            }
            else {
               cvtdAmount = foreignAmount / afRate[input-1];   //Oh good idea! menu choice is the array number..  line 91
               printf("\n%f %s is equivalent to US$%.2f\n(Rate: %f %s to US$1)",
                 foreignAmount, aszCurrency[input-1], cvtdAmount,
                 afRate[input-1], aszCurrency[input-1]);    //all the amounts are then calculated and displayed here.  line 94
            }
            }
           
        else if  (0 == input)
            userWantsExit = 1;   // user wants to userWantsExit  line 99
        else {  // invalid input
            if (INVALID_VALUE == input)
                printf("\n*** Invalid input (alphabetic?); try again.\n");
            else
                printf("\n*** Invalid input (%d); try again.\n", input);
        }
    } // while(!userWantsExit)

    return 0;
}
//line 110
0
Comment
Question by:macshawn
  • 10
  • 3
  • 2
  • +3
23 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
it is related to an extra closing bracket  }

You have to make proper indentation to detect it.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
           else {
                  cvtdAmount = foreignAmount / afRate[input-1];
                  printf("\n%f %s is equivalent to US$%.2f\n(Rate: %f %s to US$1)",
                  foreignAmount, aszCurrency[input-1], cvtdAmount,
                  afRate[input-1], aszCurrency[input-1]);    
            }
      }  /* <--------------------------------I think is this */
0
 

Author Comment

by:macshawn
Comment Utility
thanks just found it, is their a way to fix my mess so it will take a second input currency and convert to that?

for example, what this mess is supposed to do is take a currency and figure out the us equivalent and then take a second currency and convert original currency to second.
example
1 yen = 10 us$
second choice
euro, yen converted to euro = 100
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
The answer is: divide and conquer. You must to use functions to avoid the mess.

1st function: Prompt for first currency and value, and validate it.
2nd function: convert to an middle currency, let's say dollar (not a good choice nowadays)
3rd function: Prompt for second currency and value, and validate it
4th function: convert to target currency

This way your main() function will look readable (no more than 10 or 15 lines).
 Also don't forget to indent properly, is the key for clean programming.
0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility

Hi macshawn,

Glad to see that you took my suggestion and have made this table driven.  :)

Jaime's correct.  Split the program so that "logical steps" are performed as functions.  It will make the code much easier to read.

Step 1.  Move afRate and aszCurrency into global storage.  You probably want to do that regarless of other changes.
Step 2.  Split things up.

#define MAX_CONVERION 5
main ()
{
  int Selection;

  while (1)
  {
    PrintMenu ();
    Selection = GetChoice ();
    if (Selection == 0)
      break;
    if (Selection < 0 || Selection > MAX_CONVERSION)
    {
      PrintErrorMessage ();
      continue;
    }
    DoMoreStuff ();
  }
}


Good Luck,
Kent

}
0
 

Author Comment

by:macshawn
Comment Utility
Ok, I feel so dumb, cannot get this to work, I keep trying too many different ways and I think I have really confused myself.

My current error message:

Miracle C Compiler (r3.2), written by bts.
Compiling c:\program files\miracle c\wk6classex.c

c:\program files\miracle c\wk6classex.c: line 41: Parse Error, expecting `SEP'
'{ int Selection'
aborting compile





Here is what I have, tried incorporating Kent's suggestions but got lost on what I was doing.

#define VERSION "1.1"
#define INVALID_VALUE -29833
#define Max-Conversion5

/******************************************************************************

        Purpose : Convert foreign currency to $US
        Version : 1.0

The program assumes that the user does not enter anything except valid numbers
(including minus sign and decimal point).  That's a bad assumption in the real
world, but okay for this.
*******************************************************************************/

#include <stdio.h>


    /**
     * CAUTION: The following two arrays must match each other.
     * If you add to one, you must add to the other.
     * Otherwise, they'll get out of sync.
     */
/*****Moved to Global variables******/
    // array of conversion rates of foreign currency / USD$1.00
    float afRate[] = {
      107.10,  // Japanese Yen
      0.5472,  // British pounds sterling
      0.8212,  // Euro
      3.7501,  // Saudi Riyal
      1.2424   // Swiss Franc
    };

    char *aszCurrency[] = {
        "Japanese Yen",
        "British Pound",
        "Euro",
        "Riyal",
        "Swiss Franc"
    };
main();
{
/**********************Need to break up program into more understandable sections**************/
      int Selection;
      
      while(1)
      {
            PrintMenu();
            Selection = GetChoice();
            if(Selection == 0)
                  break;
            if(Selection < 0 || Selection > Max_Conversion)
            {
               PrintErrorMessage();
               continue;
            }
            DoMoreStuff();
      }
}
    float cvtdAmount, foreignAmount;
    int input, userWantsExit = 0, nrCurrencies, i;

    nrCurrencies = sizeof(afRate) / sizeof(afRate[0]);  // nr of currencies

    while(!userWantsExit) {
        printf("\n\n  Currency Conversion, v%s \n\n", VERSION);
        printf("Available currencies and equivalence to US$1.00\n\n");
        for (i=0; i<nrCurrencies; i++) {
            printf("%d: %15s   %8.4f\n", i+1, aszCurrency[i], afRate[i]);
        }
        printf("0: %15s\n", "Exit");

        printf("\nWhich currency to convert to US$? ");
        input = INVALID_VALUE;  // in case user enters non-numeric
        fflush(NULL);
        scanf("%d", &input);

        if ((input <= nrCurrencies) && (input >= 1)) {
            printf("\nAmount of %s to convert: ", aszCurrency[input-1]);
            foreignAmount = (float) INVALID_VALUE;
            scanf("%f", &foreignAmount);
            if ((float) INVALID_VALUE == foreignAmount) {
               printf("\n*** Invalid amount (alphabetic?); try again.\n");
            }
            else {
               cvtdAmount = foreignAmount / afRate[input-1];
               printf("\n%f %s is equivalent to US$%.2f\n(Rate: %f %s to US$1)",
                 foreignAmount, aszCurrency[input-1], cvtdAmount,
                 afRate[input-1], aszCurrency[input-1]);
            }
       
        else if (0 == input)
            userWantsExit = 1;
              // user wants to userWantsExit
        else {  // invalid input
            if (INVALID_VALUE == input)
                printf("\n*** Invalid input (alphabetic?); try again.\n");
            else
                printf("\n*** Invalid input (%d); try again.\n", input);
        }
    } // while(!userWantsExit)

    return 0;
}

/********************************************************************************/
 

0
 
LVL 4

Expert Comment

by:pankajtiwary
Comment Utility
I don't understand something. Everytime I see the problem is with the other files than the poster has posted. The poster has posted code conversion.c (first post) and then the second time also may be the same thing. But the error shows the error in the files MichaelExample2.c (in the first post) and wk6classex.c (in the second post). Is the poster sure he is posting the correct code?
0
 
LVL 4

Expert Comment

by:pankajtiwary
Comment Utility
I am not able to figure out the second code. The first one is okay for me. So, going with that:

Do not use C++ style comments (//) when you are writing C code please. I beg you.

Below is a completely compilable working program. From the text of the code, I really could not figure out what is the idea of the poster. But he can make appropriate changes (very minimal for invalid input or anything else). But I really want to know the intention of the user. Another thing is user is asking to input the choice twice, I have no idea why. From my perspective, the code should be as follows.

#include <stdio.h>

#define VERSION "1.1"

float afRate[] = {
      107.10,
    0.5472,
    0.8212,
    3.7501,
    1.2424
};

char *aszCurrency[] = {
    "Japanese Yen",
    "British Pound",
    "Euro",
    "Riyal",
    "Swiss Franc"
};

int main ()
{
    float cvtdAmount, foreignAmount;
    int input, nrCurrencies, i, userWantsExit = 0;

    nrCurrencies = sizeof(afRate) / sizeof(afRate[0]);

    while(!userWantsExit) {
        printf("\n\n  Currency Conversion, v%s \n\n", VERSION);
        printf("Available currencies and equivalence to US$1.00\n\n");
        for (i=0; i<nrCurrencies; i++) {
            printf("%d: %15s   %8.4f\n", i+1, aszCurrency[i], afRate[i]);
        }
        printf("0: %15s\n", "Exit");
        printf("\nWhich currency to convert to from? ");
        input = INVALID_VALUE;
        fflush(NULL);
        scanf("%d", &input);

        if ((input <= nrCurrencies) && (input >= 1)) {
            printf("\nAmount of %s to convert: ", aszCurrency[input-1]);
            foreignAmount = (float) INVALID_VALUE;
            scanf("%f", &foreignAmount);

            cvtdAmount = foreignAmount / afRate[input-1];
            printf("Debug: %f = %f / %f\n", cvtdAmount, foreignAmount, afRate[input - 1] ) ;
            printf("\n%f %s is equivalent to US$%.2f\n(Rate: %f %s to US$1)", foreignAmount, aszCurrency[input-1], cvtdAmount, afRate[input-1], aszCurrency[input-1]);
            continue ;
        }
        else if  (0 == input)
            userWantsExit = 1;
        else {
            if (INVALID_VALUE == input)
                printf("\n*** Invalid input (alphabetic?); try again.\n");
            else
                printf("\n*** Invalid input (%d); try again.\n", input);
        }
    }
    return 0;
}
0
 

Author Comment

by:macshawn
Comment Utility
Thanks for your help what I am trying to turn this mess into is a program that:
ask "which currency to convert from?"
   then
ask "amount? "
   then
ask "which currency to convert to? "
   then
display for example "100 Japan Yen is equivalent to 125 US$"
   then
display for example the second selected currency "100 Japan Yen is equivalent to 200 Euro"

hope that makes sense.

about the other question. I keep getting lost trying to add some friends suggestions to my code and incorporate the Expert suggestion that you all come up with.  
I just get lost.
thanks for your help.
Can you help me chage what we have above to incorporate this by suggesting some starting points?
Thank you again



0
 

Author Comment

by:macshawn
Comment Utility
Are the double // C++ ? sorry I did not realize that my Miracle C compiler does not know the difference, will change on next post :0)
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:macshawn
Comment Utility
When I run the code above I get an error, that I must be overlooking, message is:

Miracle C Compiler (r3.2), written by bts.
Compiling C:\Program Files\Miracle C\TeacherEx6.c
main

C:\Program Files\Miracle C\TeacherEx6.c: line 36: variable 'INVALID_VALUE' not found
'input = INVALID_VALUE'
aborting compile

complete code is:

#include <stdio.h>

#define VERSION "1.1"

float afRate[] = {
     107.10,
    0.5472,
    0.8212,
    3.7501,
    1.2424
};

char *aszCurrency[] = {
    "Japanese Yen",
    "British Pound",
    "Euro",
    "Riyal",
    "Swiss Franc"
};

int main ()
{
    float cvtdAmount, foreignAmount;
    int input, nrCurrencies, i, userWantsExit = 0;

    nrCurrencies = sizeof(afRate) / sizeof(afRate[0]);

    while(!userWantsExit) {
        printf("\n\n  Currency Conversion, v%s \n\n", VERSION);
        printf("Available currencies and equivalence to US$1.00\n\n");
        for (i=0; i<nrCurrencies; i++) {
            printf("%d: %15s   %8.4f\n", i+1, aszCurrency[i], afRate[i]);
        }
        printf("0: %15s\n", "Exit");
        printf("\nWhich currency to convert to from? ");
        input = INVALID_VALUE;
        fflush(NULL);
        scanf("%d", &input);

        if ((input <= nrCurrencies) && (input >= 1)) {
            printf("\nAmount of %s to convert: ", aszCurrency[input-1]);
            foreignAmount = (float) INVALID_VALUE;
            scanf("%f", &foreignAmount);

            cvtdAmount = foreignAmount / afRate[input-1];
            printf("Debug: %f = %f / %f\n", cvtdAmount, foreignAmount, afRate[input - 1] ) ;
            printf("\n%f %s is equivalent to US$%.2f\n(Rate: %f %s to US$1)", foreignAmount, aszCurrency[input-1], cvtdAmount, afRate[input-1], aszCurrency[input-1]);
            continue ;
        }
        else if  (0 == input)
            userWantsExit = 1;
        else {
            if (INVALID_VALUE == input)
                printf("\n*** Invalid input (alphabetic?); try again.\n");
            else
                printf("\n*** Invalid input (%d); try again.\n", input);
        }
    }
    return 0;
}
0
 

Author Comment

by:macshawn
Comment Utility
Another dumb nubie question, when counting lines, like in the example for the error code the compiler returned, do you just count the code lines or are spaces include in line numbers?
0
 

Author Comment

by:macshawn
Comment Utility
I know it is late but any help would be really, really appreciated. :)
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 250 total points
Comment Utility
Try either:

       input = -1;

or

#define INVALID_VALUE -1

>>Another dumb nubie question, when counting lines, like in the example for the error code the compiler returned, do you just count the code lines or are spaces include in line
Not dumb at all!! The line number is the exact line number in the original file. If your editror shows the line numbger then thats the number it will report.

Paul
 
 
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
Sorry, I missed a previous posting. Youve deleted the line:

#define INVALID_VALUE -29833

Paul
0
 

Author Comment

by:macshawn
Comment Utility
Thanks Paul, that made it compile.  This is no longer a homework problem already turned a version in. I completed the assignment using switch and case statments and this solution was suggested earlier as easier but I don't get the flow, logic or what not.  Just want to see if I can figure out how to make this accept input such as:

ask "which currency to convert from?"
   then
ask "amount? "
   then
ask "which currency to convert to? "
   then
display for example "100 Japan Yen is equivalent to 125 US$"
   then
display for example the second selected currency "100 Japan Yen is equivalent to 200 Euro"

hope that makes sense.
Thanks again for your help.

0
 
LVL 8

Expert Comment

by:ssnkumar
Comment Utility
You can use the printf() itself to display whatever you want.
Or I have not understood your question....more clarification needed!!

-ssnkumar
0
 

Author Comment

by:macshawn
Comment Utility
How to build the the section that will do the calculations , table structure, what do I need to expand?  I can add the extra printf lines and scanf lines to the code later, or later today.  I was just told that this was an easier program to adapt to a second input that will compare it to the first and I don't see how?  I need a push in the right direction.
thanks.
0
 
LVL 8

Assisted Solution

by:ssnkumar
ssnkumar earned 250 total points
Comment Utility
I think you are getting confused and making it look complicated.
Now stop looking at the code. Just think of the whole problem. Write it down.
Put it into steps. This is the algorithm. If you could achieve this, then the code will become very simple.
If you show us your algorithm, then we could try to help you more.

-ssnkumar
0
 

Author Comment

by:macshawn
Comment Utility
ok, thanks, will look at this later and see what I can do.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 opening and reading files in the C programming language.

772 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

10 Experts available now in Live!

Get 1:1 Help Now