#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    3
    Rep Power
    0

    if else statement


    I am writing a code that enables the operator to choose between buying a truck or car at their price. If they choose to purchase a truck, they must decide if the truck should come with two wheel drive or four wheel drive. The price will vary and I've included tax. I have a strong feeling that my code is off but need assistance being that my school is teaching C++ but gives us a C compiler. I'm still trying to figure that one out. Anyway, here's my code so far (and I have no idea what the stuff is above 'int main(void)')-



    Code:
    typedef unsigned int size_t;
    void exit(int errstatus);
    void *malloc(int n);
    void  free(void *ptr);
    char *getenv(char *str1);
    void srand(unsigned int seed);
    int rand();
    char *itoa(int value,char *str1,int radix);
    char *ltoa(long value, char *str1, int radix);
    long labs(long n);
    int abs(int n);
    long atol(char *str1);
    int atoi(char *str1);
    long strtol(char *nptr, char **endptr, int base);
    unsigned long strtoul(char *nptr, char **endptr, int base);
    double strtod(char *nptr, char **endptr);
    double atof(char *ptr);
    int main(void)
    {
    int choice;
    int drive_type;
    float price;
    const float tax = 0.07;				
    const float twd_price = 2000.00;
    const float fwd_price = 4500.00;	
    cout << "Please enter your desired price for your dream vehicle!: ";
    cin >> price;
    cout << endl;
    if(price < 0)
    {
    cout << "ERROR -- You have entered a negative amount!"<< endl;
    cout << " Please try again"<< endl;
    exit(1); 
    }
    cout << "Please enter your vehicle selection (enter 1 for truck or 2 for car):";
    cin >> choice;
    if( (choice != 1) && (choice != 2) )
    {
    cout << "ERROR -- You have entered invalid data"<< endl;
    cout << "Please try again."<< endl;
    exit(1);
    }
    if(choice == 1)
    {
    cout << "What drive type is this truck? (enter 2 for 2wd and 4 for 4wd:";
    cin >> drive_type;
    }
    if( (drive_type != 2) && (drive_type != 4) )
    {
    cout << "ERROR -- You have not entered a correct drive type!"<< endl;
    cout << "Please try again."<< endl;
    exit(1);
    }
    if (drive_type == 2)
    {
    cout << "You entered 2 wheel drive"<< endl;
    cin >> price == (price + twd_price) * tax;
    }
    if (drive_type == 4)
    {
    cout << "You entered 4 wheel drive... you are a bit on the wild side"<< endl;
    cin >> price == (price + fwd_price) * tax;
    }
    else (choice == 2)
    {
    cout << "Enjoy your new car!"<< endl;
    cin >> price == price * tax;
    }
    cout << setiosflags(ios::fixed | ios::showpoint); 		
    cout << "The total price of your car is: "<< price;
    }
    return 0; }
    Last edited by damonbrinkley; September 11th, 2003 at 08:35 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    68
    Rep Power
    12
    >my school is teaching C++ but gives us a C compiler
    That's really dumb, are you sure it's a C compiler and not a C++ compiler that also compiles C?

    >and I have no idea what the stuff is above 'int main(void)'
    Those are function and type declarations that also happen to be silly. Such declarations are already in the standard libraries (most of them that is).

    Doing this in C is very simple and the C++ conversion just requires you to change to cout and cin instead of printf and getchar. Here's the C version (stripped down of functionality just as an example):
    Code:
    #include <stdio.h>
    
    #define TAX .07
    #define TWD 2000.00
    #define FWD 4500.00
    
    int main(void)
    {
      int choice;
      int drive_type;
      float price;
    
      printf("What type do you want (2 for 2wd, 4 for 4wd): ");
      fflush(stdout);
      if ((choice = getchar()) != EOF) {
        if (choice == '2') {
          printf("You entered 2 wheel drive\n");
          price = TWD + (TWD * TAX);
        }
        else if (choice == '4') {
          printf("You entered 4 wheel drive\n");
          price = FWD + (FWD * TAX);
        }
        printf("The total price is %.2f\n", price);
      }
    
      return 0;
    }
    And the C++ version:
    Code:
    #include <iostream>
    
    using namespace std;
    
    const double TAX = .07;
    const double TWD = 2000.00;
    const double FWD = 4500.00;
    
    int main()
    {
      int choice;
      int drive_type;
      float price;
     
      cout<<"What type do you want (2 for 2wd, 4 for 4wd): "<<flush;
      if ((choice = cin.get()) != EOF) {
        if (choice == '2') {
          cout<<"You entered 2 wheel drive\n"<<endl;
          price = TWD + (TWD * TAX);
        }
        else if (choice == '4') {
          cout<<"You entered 4 wheel drive\n"<<endl;
          price = FWD + (FWD * TAX);
        }
        cout<<"The total price is "<< price <<endl;
      }
    }
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    3
    Rep Power
    0
    Thanks KFM. I am sure that the compiler is C and not C++. Your code is a big help. Thanks again.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    Originally posted by sagrin
    Thanks KFM. I am sure that the compiler is C and not C++. Your code is a big help. Thanks again.
    If the compiler is C and not C++, then your program should not compile at all. AFAIK, C cannot even begin to handle iostream, since that requires operator overloading which C++ has but C does not. IOW, you could not use cout with a purely C compiler.

    I know, let's play "Name that Compiler!".
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Jakarta
    Posts
    8
    Rep Power
    0
    what compiler do u use?
    C++ Compiler can handle C codes to but pure C compiler can't
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    3
    Rep Power
    0
    I'm using a C++ compiler at home. I was just making a point that the intelligent staff at my university is giving us C++ code yet teaching C and giving us a C compiler. I'm sure once I get a few years experience it'll be a snap for me, however, I'm just now learning how to spell "C".

IMN logo majestic logo threadwatch logo seochat tools logo