CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 4 FirstFirst 1234 LastLast
Results 16 to 30 of 55
  1. #16
    Join Date
    Jan 2013
    Posts
    71

    Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

    I'm having a new problem with a new program. The program is called Tollbooth and the design is to have it where the program asks the user to pay a toll based on the vehicle they drive. And the user would input a single letter, like M for Motorcycle, then the program would output the amount owed for the toll. But my problem is, no matter the letter that is entered, it lists all the toll prices. Here is what I have for the program so far. Any thoughts that would lead me to figuring out what im doing wrong is greatly appreciated!

    #include <iostream>
    #include <iomanip>

    using namespace std;
    int main()
    {

    double type;
    double m;
    double s;
    double c;
    double l;
    double t;
    double v;
    double b;

    cout << "Enter the type of vehicle in which you are traveling" << endl;
    cout << "\n\tM - Motorcycle" ;
    cout << "\n\tS - 2-seater sports car" ;
    cout << "\n\tC - 4-5 passenger car/SUV" ;
    cout << "\n\tL - 6-9 passenger SUV/mini van" ;
    cout << "\n\tT - pickup truck" ;
    cout << "\n\tV - 10-15 passenger van" ;
    cout << "\n\tB - Bus or motorhome\n" ;
    cin >> type;

    if (type = m)
    cout << "Your toll is 0.50." << endl;
    else if (type = s)
    cout << "Your toll is 1.00." << endl;
    else if (type = c)
    cout << "Your toll is 2.00." << endl;
    else if (type = l)
    cout << "Your toll is 3.00." << endl;
    else if (type = t)
    cout << "Your toll is 3.50." << endl;
    else if (type = v)
    cout << "Your toll is 4.00." << endl;
    else if (type = b)
    cout << "Your toll is 7.50." << endl;

    system ("pause");
    return 0;
    }

  2. #17
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

    And what exactly does = do, and what should you be using instead to test for equality?

    Why are you using a double to hold alpha data?

    What are all those other unitialized doubles supposed to be doing?

    If you want somebody to enter an alpha character, your data type should be a char, and you should be testing for equality against character literals, not unitialized doubles.
    Last edited by GCDEF; February 18th, 2013 at 01:06 PM.

  3. #18
    Join Date
    Jan 2013
    Posts
    71

    Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

    Yea, I admit. Im still confused on when to do double, char, int...etc.

    But some of this has been trial and error. When i do like 'char m' the program isnt functioning properly.

    as far as what = does. Im using it for the equation...if type(user input) equals m (or whichever letter) then there toll would be ___.

  4. #19
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

    That's not what = does.

    Basically you want something like

    char type;
    if(type == 'M')

    FWIW, you will never, ever learn to program C++ by trial and error. What you posted isn't even close.

  5. #20
    Join Date
    Jan 2013
    Posts
    71

    Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

    also ive tried to use == but when i use that, it doesnt display anything

  6. #21
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

    Quote Originally Posted by psfign View Post
    also ive tried to use == but when i use that, it doesnt display anything
    That's because you had other issues. As I said, inputting character data into a double then comparing it to other unitialized doubles couldn't possibly work. That's why I said guessing won't work and what you had wasn't even close. Look at my previous post.

  7. #22
    Join Date
    Jan 2013
    Posts
    71

    Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

    ouch, im a trial and error learner.

  8. #23
    Join Date
    Jan 2013
    Posts
    71

    Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

    well, ive changed it to this and now when i input the letter, it only displays 'Enter Any key to continue...'

  9. #24
    Join Date
    Jan 2013
    Posts
    71

    Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

    oops, to this:

    if(type == 'm')
    cout << "Your toll is 0.50." << endl;
    else if(type == 's')
    cout << "Your toll is 1.00." << endl;
    else if(type == 'c')
    cout << "Your toll is 2.00." << endl;
    else if(type == 'l')
    cout << "Your toll is 3.00." << endl;
    else if(type == 't')
    cout << "Your toll is 3.50." << endl;
    else if(type == 'v')
    cout << "Your toll is 4.00." << endl;
    else if(type == 'b')
    cout << "Your toll is 7.50." << endl;

    system ("pause");
    return 0;
    }

  10. #25
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

    That should work. How is type defined? Are you entering upper or lower case letters? Post the entire program.

  11. #26
    Join Date
    Jan 2013
    Posts
    71

    Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

    i got it now. I had changed all the individual letters to char but still had type as double. So after reading again what you said about the types again, i tried changing type as well and walla!

    #include <iostream>
    #include <iomanip>

    using namespace std;
    int main()
    {

    char type;
    char m;
    char s;
    char c;
    char l;
    char t;
    char v;
    char b;

    cout << "Enter the type of vehicle in which you are traveling" << endl;
    cout << "\n\tM - Motorcycle" ;
    cout << "\n\tS - 2-seater sports car" ;
    cout << "\n\tC - 4-5 passenger car/SUV" ;
    cout << "\n\tL - 6-9 passenger SUV/mini van" ;
    cout << "\n\tT - pickup truck" ;
    cout << "\n\tV - 10-15 passenger van" ;
    cout << "\n\tB - Bus or motorhome\n" ;
    cin >> type;

    if(type == 'm')
    cout << "Your toll is 0.50." << endl;
    else if(type == 's')
    cout << "Your toll is 1.00." << endl;
    else if(type == 'c')
    cout << "Your toll is 2.00." << endl;
    else if(type == 'l')
    cout << "Your toll is 3.00." << endl;
    else if(type == 't')
    cout << "Your toll is 3.50." << endl;
    else if(type == 'v')
    cout << "Your toll is 4.00." << endl;
    else if(type == 'b')
    cout << "Your toll is 7.50." << endl;

    system ("pause");
    return 0;
    }

  12. #27
    Join Date
    Jan 2013
    Posts
    71

    Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

    so at the end of the day, i need to become more familiar with the declarations. I think that's been my achilles heel.

  13. #28
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

    I would get yourself a good tutorial book and work through it.

  14. #29
    Join Date
    Jan 2013
    Posts
    71

    Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

    Good idea.

  15. #30
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).

    You don't need these declarations. They are declared but never used.

    char m;
    char s;
    char c;
    char l;
    char t;
    char v;
    char b;

    Is there any reason why you used multiple if statements? Was it part of the exercise specification? as it can be done using fewer statements. Also you output the type of vehicle in uppercase letters (M S etc) but only test for lowercase letters (m s etc).

    You should learn how to use the debugger to find out what is actually happening when your code doesn't work as expected. Time invested in learning how to debug programs using the debugger is time well spent. It will repay itself many times over as you start to write more complicated programs.

    If you are thinking about investing in a good tutorial book I would advise against any published prior to about 2001 as the c++ language changed before then to become the ANSI c++ standard.

Page 2 of 4 FirstFirst 1234 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured