-
February 18th, 2013, 01:02 PM
#16
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;
}
-
February 18th, 2013, 01:04 PM
#17
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.
-
February 18th, 2013, 01:14 PM
#18
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 ___.
-
February 18th, 2013, 01:24 PM
#19
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.
-
February 18th, 2013, 01:24 PM
#20
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
-
February 18th, 2013, 01:28 PM
#21
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
Originally Posted by psfign
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.
-
February 18th, 2013, 01:30 PM
#22
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
ouch, im a trial and error learner.
-
February 18th, 2013, 01:32 PM
#23
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...'
-
February 18th, 2013, 01:32 PM
#24
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;
}
-
February 18th, 2013, 01:34 PM
#25
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.
-
February 18th, 2013, 01:39 PM
#26
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;
}
-
February 18th, 2013, 01:43 PM
#27
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.
-
February 18th, 2013, 01:45 PM
#28
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.
-
February 18th, 2013, 01:48 PM
#29
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
-
February 18th, 2013, 03:40 PM
#30
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|