Results 1 to 3 of 3

Thread: IF statements

  1. #1
    Join Date
    Oct 2005
    Posts
    1

    Unanswered: IF statements

    I am trying to error check the user's input, but I can seem to get any IF statements to work. I've tried a simple statement and still get the same error, looking "(". Can someone help. Thanks.


    /* Currency Conversion Program
    Team Project by
    */

    #include <stdio.h>
    #include <stdlib.h>

    #define peso 0.09174;
    #define naira 0.007634;
    #define nuevosol 0.29512;
    #define ppeso 0.01793;
    #define euro 1.20130;

    int main ()
    { //declare variables
    int response;
    int quit;
    int choice;
    int amt;
    int country;
    int total;
    float conversion;

    //Title of Program
    printf("\t\t\tCurrency Conversion Program\n\n");

    //description of purpose
    printf(" The following menu will allow you to select a country's currency to convert\n\n");
    printf("\n\t\t\t\t into U.S. dollar\n\n");

    while (choice != 6) //while menu option does not equal 6
    {
    total = 0; amt = 0;
    total = total + amt; //keep running total
    choice = 1,2,3,4,5,6;

    //display menu option for user
    printf("\n\t 1. Mexico's Pesos currency.\n");
    printf("\n\t 2. Nigeria's Nairas currency.\n");
    printf("\n\t 3. Peru's Nuevo Soles currency.\n");
    printf("\n\t 4. Philippines' Pesos currency.\n");
    printf("\n\t 5. Spain's Euro currency.\n");
    printf("\n\t 6. Quit!\n");
    //input a menu option
    printf("\n\t Please enter the country of your choice. ");
    // IF statement to error check range
    if scanf("%d", &choice <= 0 || choice =>7)
    {
    printf("Invalid value, please try again!"\n);
    continue;
    }
    // IF statement if user quits program
    else if scanf("%d", &choice = 6)
    {
    printf("Thank you for using Currency Conversion Program ver 3.0.\n");
    printf("Here is a total of converted currency in U.S. dollars: %.2f", total);
    printf("\nPlease come back soon!\n");
    }

    printf("\n Now enter a currency amount: ");
    scanf("%d", &amt);
    total = total + amt;
    switch (choice)
    {
    case 1:
    printf("\n You have selected Mexico\n");
    conversion = amt*peso;
    printf("\n Your conversion amount is $ %.2f U.S. dollars.", conversion);
    printf("\n\n\n");
    break;
    case 2:
    printf("\n You have selected Nigeria\n");
    conversion = amt*naira;
    printf("\n Your conversion amount is $ %.2f U.S. dollars.", conversion);
    printf("\n\n\n");
    break;
    case 3:
    printf("\n You have selected Peru\n");
    conversion = amt*nuevosol;
    printf("\n Your conversion amount is $ %.2f U.S. dollars.", conversion);
    printf("\n\n\n");
    break;
    case 4:
    printf("\n You have selected Philippines.\n");
    conversion = amt*ppeso;
    printf("\n Your conversion amount is $ %.2f U.S. dollars.", conversion);
    printf("\n\n\n");
    break;
    case 5:
    printf("\n You have selected Spain.\n");
    conversion = amt*euro;
    printf("\n Your conversion amount is $ %.2f U.S. dollars.", conversion);
    printf("\n\n\n");
    break;
    }

    }
    }

  2. #2
    Join Date
    Oct 2005
    Posts
    1
    looks normal to me

  3. #3
    Join Date
    Dec 2005
    Location
    Reading, UK
    Posts
    1
    OK a couple of things here:

    firstly &choice means the address of choice in the memory. A statement like:
    &choice <= 6
    Just means "is the address (which is a big number) where choice is in the memory less than 6". You don’t want to be testing for this. You mean "&& choice <=6" where && is the and operator and there is a space between it and choice.

    Secondly, you cant put conditional statements inside a scanf like that, remember its just a simple function like any you write. It cant handle the truth. Therefore you need to separate your evaluation. I suggest you put the scanf in an if a line above and do the limmit tests separately inside the original if statement.

    Thirdly anything you give to an if statement must be contained within brackets. Most compilers don’t know what your talking about if you don’t do that.

    The performance gain for doing stuff all on one line really isn’t that big, and it makes your code more likely to go wrong and harder for you to read later. Remember compilers these days optimise it beyond recognition so if you can think of it they probably already do it.

    To summarise you may wish to try:
    if ((scanf("%d", &choice)) && (choice <= 0 || choice >= 7))
    else if ((scanf("%d", &choice)) && (choice == 6))

    Oh and there is a stray newline character a couple lines bellow.
    Last edited by LowCostGroup; 01-22-06 at 11:01.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •