Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
Thanks. I see that now. I had them declared at one point because I wrote the code a little different at first and had the display like..
cout << "\nYour toll is " << m << endl;
The statements are that way because I cant think of how to simplify it. And the prof example was like that with the lower case and uppercase. But if you can give me a hint on how to simplify it, that would be cool.
I will work on taking advantage of the debugger. I have so far been trying to decipher what the compiler log error spits out.
I'm also trying to figure out how to make it where if the user doesnt enter in the letter properly, that the default toll would be 15.00
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
The function tolower(ch) returns the lowercase equivalent of ch if ch is a letter otherwise ch is returned unchanged. So you could use something like
Code:
cin >> type;
type = tolower(type);
The you could enter either v or V or c or C etc.
I don't know what your tutor has already taught you about the c++ language, but instead of using the multiple if statement cascades which are hard to read and follow, you could use the switch statement something like this
Code:
float toll = 0;
switch (type) {
case 'm':
toll = 0.50;
break;
case 's':
toll = 1.0;
break;
..........
default:
toll = 15.00;
break;
}
cout << "Your toll is " << toll << endl;
You could also use what's called arrays to hold the various tolls to be paid for the vechicle types. This gives code like this
Code:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
const char veh[] = {'m', 's', 'c', 'l', 't', 'v', 'b', 0}; //or = "mscltvb";
const float toll[] = {0.5, 1.0, 2.0, 3.0, 3.5, 4.0, 7.5};
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" ;
if (char *ind = strchr(veh, tolower(cin.get())))
if ((ind - veh) < (sizeof(toll) / sizeof(toll[0])))
cout << "Your toll is " << toll[ind - veh] << endl;
else
cout << "Unknown toll" << endl;
else
cout << "Your toll is 15.0" << endl;
return (0);
}
which is probably a bit too advanced for you to understand properly at present - but it gives an idea of the things that can be done with c++. When your tutor covers arrays and pointers you should then be able to follow this. Basically it finds the position in the veh array of the character entered and then gets the corresponding toll from the toll array. So if you want to change the vehicle code, the amount of toll paid or to add more vechicle/toll combinations (with changes to the cout statements as needed) then there are only two lines at the beginning of the program to change and the following code will still work. You could even put these into another array and then use these to print out the vechicle code, name and toll charge, So any changes to code, description or charge just requires a change to these arrays. Of course, the next step is then to hold this data in a file rather in the program and read them in as part of the program. Then the file containing the codes, tolls etc can be modified separately to changing the program everytime something changes!
Code:
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
int main()
{
const float unknown = 15.0;
const char veh[] = {'m', 's', 'c', 'l', 't', 'v', 'b', 0}; //or = "mscltvb";
const float toll[] = {0.5, 1.0, 2.0, 3.0, 3.5, 4.0, 7.5};
const char *desc[] = {"Motorcycle", "2-seater sports car", "4-5 passenger car/SUV",
"6-9 passenger SUV/mini van", "pickup truck", "10-15 passenger van",
"Bus or motor home"};
const int noveh = strlen(veh),
notoll = sizeof(toll) / sizeof(toll[0]),
nodesc = sizeof(desc) / sizeof(char*);
cout << "Enter the type of vehicle in which you are traveling\n" << endl;
cout << fixed << setprecision(1);
for (int i = 0; i < noveh; i++) {
cout << "\t" << veh[i];
if (i < nodesc)
cout << " - " << setw(25) << left << desc[i];
if (i < notoll)
cout << "\ttoll: " << toll[i] << endl;
}
if (char *ind = strchr(veh, tolower(cin.get())))
if ((ind - veh) < notoll)
cout << "Your toll is " << toll[ind - veh] << endl;
else
cout << "Unknown toll" << endl;
else
cout << "Your toll is " << unknown << endl;
return (0);
}
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
Thanks 2kaud. That helped a ton! The program is basically done now, but there is one other silly issue, when it reads "Your Toll is ...." it displays the toll as like '7.5' instead of '7.50'. Not sure how to correct that.
My next project is displaying a random card in 52 card deck. You input a number between 1-52 and it will display a card.
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
If you want to change the number of digits displayed to the right of the decimal point, then use the setprecision manipulator. In the last section of code in my previous post there is
Code:
cout << fixed << setprecision(1);
Just change the 1 to a 2 if you want 2 digits after the decimal point.
Have fun with the card project!
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
This new project is frustrating me too.
So my problem is...how do i make it where if the user inputs the number 15, the program will display "2 of Diamonds" or whatever it would be? I cant figure out how to make it assign one number to the face number + the suit.
I know the code is incomplete, I feel like my problem again is declarations, but im not positive.
Here's the instructions:
Displaying Card in a Deck
There are 52 cards in a deck. Each deck contains 4 suits, with 13 cards in each suit. The cards are
1 thru 10, jack, queen, and king. The 4 suits are diamonds, hearts, spades and clubs.
Write a program that has the user enter a number between 1 and 52, representing the position
of the card in a deck. Then determine the card, the suit and face value. You do this by
determining what the number represents.
The table for the cards is as follows:
Suits:
Diamonds 1 - 13.
Hearts 14 - 26
Spades 27 – 39
Clubs 40 – 52
Face Value:
1 1 14 27 40
2 2 15 28 41
3 3 16 29 42
4 4 17 30 43
5 5 18 31 44
6 6 19 32 45
7 7 20 33 46
8 8 21 34 47
9 9 22 35 48
10 10 23 36 49
Jack 11 24 37 50
Queen 12 25 38 51
King 13 26 39 52
Since the user is entering numeric data, make sure you account for possible input failure,
accidentally entering non-numeric data.
Perform a range check; check to make sure the user entered is a valid number (1 – 52). If the
user entered a value outside the range, end the program.
-------------------------------------------------------------------------------------------
So my problem is...how do i make it where if the user inputs the number 15, the program will display "2 of Diamonds" or whatever it would be? I cant figure out how to make it assign one number to the face number + the suit.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int card = 0;
int suit = 0;
char Diamonds;
char Hearts;
char Spades;
char Clubs;
char face;
cout << "Enter the number of a card in a shuffled deck (1-52) ";
cin >> card; cin.clear(); cin.ignore(10,'\n');
if (card == 1-13)
suit = Diamonds;
if (card == 14-16)
suit = Hearts;
if (card == 27-39)
suit = Spades;
if (card == 40-52)
suit = Clubs;
if (card == 1, 14, 27, 40);
face = "1 of ";
if (card == 2, 15, 28, 41);
face = "2 " << endl;
if (card == 3, 16, 29, 42);
face = "3 " << endl;
if (card == 4, 17, 30, 43);
face = "4 " << endl;
if (card == 5, 18, 31, 44);
face = "5 " << endl;
if (card == 6, 19, 32, 45);
face = "6 " << endl;
if (card == 7, 20, 33, 46);
face = "7 " << endl;
if (card == 8, 21, 34, 47);
face = "8 " << endl;
if (card == 9, 22, 35, 48);
face = "9 " << endl;
if (card == 10, 23, 36, 49);
face = "10 " << endl;
if (card == 11, 24, 37, 50);
face = "11 " << endl;
if (card == 12, 25, 38, 51);
face = "12 " << endl;
if (card == 13, 26, 39, 52);
face ="13 " << endl;
cout << face "of" suit
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
This code shows that you haven't really understood much of what has been pointed out to you over the course of this thread. Examples:
This means 'if the contents of the variable card is equal to the value -12'. You are trying to say if the value of card is in the range 1 to 13. This is not how it's expressed in c++!
Code:
if (card >= 1 && card <= 13)
This is how you write it. This means if the value of card is greater than or equal to 1 and the value of card is less than or equal to 13.
Code:
if (card == 1, 14, 27, 40);
This means 'if the contents of the variable card is equal to 1' You are trying to say if the value of card is either 1 or 14 or 27 or 40. Again, this is not how it's expressed in c++!
Code:
if (card == 1 || card == 14 || card == 27 || card == 40)
This is how this is written in c++.
Code:
face = "2 " << endl;
This is just plain wrong! face is defined to be a char, yet you are trying to assign a string to it which is not allowed. Also you are trying to use stream output insertion with an assignment.
We're not going to keep writing code for you so you can pass assignments when it's plain you don't understand. You really, really must learn how to write correct c++ statements. As GCDEF said, you need to get a good c++ tutorial book and work through it. What you have posted here is not even close to being a proper c++ program.
Hint for getting suits and face numbers. Look up integer division (/) and integer remainder (%).
When you're figured out how to write correct c++ syntax and have re-written your program to use the c++ language as it's defined to be used, post it back here if you want for comment.
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
2kaud already addressed it, but again, that code isn't even close to valid. Not even remotely. Guessing isn't going to work. You're wasting everybody's time, yours and ours. Hit the books.
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
Quote:
Originally Posted by
psfign
This new project is frustrating me too.
Code:
if (card == 1-13)
suit = Diamonds;
if (card == 14-16)
suit = Hearts;
Code:
if (card == 1, 14, 27, 40);
You cannot learn a language such as C++ by guessing. To prove that you're wasting your time with this assignment, how about this simple one:
Quote:
"Write a program that inputs 2 numbers and checks if the first number is between 1 and 100, and that the second number is either 3, 16, or 45".
If you can't write that program by yourself without our help, then there is absolutely no way you can complete the larger assignment.
To be blunt, there is no way you can even start to write the larger program if you can't complete the simple one I gave. The assignment I mentioned uses exactly the same elementary concepts of C++ that you seem not to have learned as of now.
Get a book, understand the concepts, go through the examples. C++ and programming is an exact science. You can't guess and hope that the compiler knows what's going on in your head. I know this makes sense to you:
Code:
if (card == 1, 14, 27, 40);
But a computer language has rules, syntax, etc. that you must understand and follow. That line compiled OK just because the compiler assumes you are using the comma operator, and a comma operator has absolutely nothing to do with comparing multiple values for equality. This is the danger of guessing in such a language like C++ -- what you think is OK and compiles with no errors may have absolutely nothing to do with the code you intended to write.
Regards,
Paul McKenzie
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
Thanks for the input guys. I recognize that a lot of things are over my head. I have gone back and reread the first couple of chapters to see if I can recognize my failings. I realized a few things, but I know there's still more i need to overcome. Again, I appreciate the input. I dont expect you guys to do the work for me, but I do try to get some general guidance of what I've done wrong.
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
The best way (in my humble opinion) to learn some new construct in a language like c++ is that after you read about it in a book (or covered in a lecture etc) is to try coding a simple example that uses what you have just read. Most of the teaching books for c++ that I'm seen include graded programming exercises. Try some of them. Don't just write programs for the assigments set for the course. Start with something very simple. Get that program to compile then get it to run as expected. Once that one works, try to make it a bit more complicated by adding function to it. Read a bit more of the book, write some more programs etc etc. You'll soon learn how to write c++ code that compiles and how to debug a program that compiles but doesn't work as expected. I've being in the programming business for over 30 years, yet when I want to use a c++ construct that I haven't used for a while so am slightly rusty (eg sets from the STL) then I too write a simple test program to make sure I fully understand it before trying it in a larger and more complicated program. You learn by doing. The more you do, the better you'll become.
Paul suggested a program to write.
Quote:
"Write a program that inputs 2 numbers and checks if the first number is between 1 and 100 inclusive, and that the second number is either 3, 16, or 45".
Try it. Start with a program that just reads in two integer numbers and prints then out. Then prints their sum left and right justifed in different field widths. Then prints a message if the first number is greater than or equal to 1. Print a message if its less than or equal to 100. Then start checking the second number etc etc. At each stage you should have a working program that does what it's supposed to do. Then add the next stage and the next etc until you have a program that does what's required. Don't go on to the next stage until the current one works.
I know learning to program in a language like c++ from scratch can be difficult but once you grasped some of the basics of the language it comes easier to learn more of it. Keep trying and it'll come to you.:thumb:
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
Well here's the code that I submitted for the assignment. This isn't how I envisioned I'd write the code, but I guess I completed it based on my limited skill set. I couldnt figure out some things, so I wrote it this way. I'll try that code tonight or tomorrow. Also the book we're using for class is C++ Programming, 4th Ed. By D.S. Malik. Anyone familiar with this book. I'm wondering if maybe a different book would do me better?
Anyhow, the code I submitted:
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int card;
string face;
cout << "Enter the number of a card in a shuffled deck (1-52) ";
cin >> card; cin.clear(); cin.ignore(10,'\n');
if (card == 1 || card == 14 || card == 27 || card == 40)
face = "Ace";
if (card == 2 || card == 15 || card == 28 || card == 41)
face = "2";
if (card == 3 || card == 16 || card == 29 || card == 42)
face = "3";
if (card == 4 || card == 17 || card == 30 || card == 43)
face = "4";
if (card == 5 || card == 18 || card == 31 || card == 44)
face = "5";
if (card == 6 || card == 19 || card == 32 || card == 45)
face = "6";
if (card == 7 || card == 20 || card == 33 || card == 46)
face = "7";
if (card == 8 || card == 21 || card == 34 || card == 47)
face = "8";
if (card == 9 || card == 22 || card == 35 || card == 48)
face = "9";
if (card == 10 || card == 23 || card == 36 || card == 49)
face = "10";
if (card == 11 || card == 24 || card == 37 || card == 50)
face = "Jack";
if (card == 12 || card == 25 || card == 38 || card == 51)
face = "Queen";
if (card == 13 || card == 26 || card == 39 || card == 52)
face = "King";
if (card >= 1 && card <= 13)
cout << "\n" << face << " of Diamonds\n" << endl;
if (card >= 14 && card <= 26)
cout << "\n" << face << " of Hearts\n" << endl;
if (card >= 27 && card <= 39)
cout << "\n" << face << " of Spades\n" << endl;
if (card >= 40 && card <= 52)
cout << "\n" << face << " of Clubs\n" << endl;
else if (card >= 53)
cout << "\nInvalid Entry\n\n" ;
system ("pause");
return 0;
}
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
I've got the second edition of the book by Malik. I thought it was quite a decent book - but what suits one person doesn't suit another. Another popular book is the one by Deitel. I've got the fourth edition of this but I think it's a bit heavy going for someone trying to learn c++ from scratch and it's not my favourite. Is there a library where you are that has books on c++ that you could look at to see which one you prefer if you don't like Malik?
Now that you've handed in the assignment, another way of coding it is
Code:
int main() {
const char *suits[] = {"Diamonds", "Hearts", "Spades", "Clubs"};
const char *value[] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
int card;
cout << "Enter a number between 1 and 52: ";
//Check if a non-numeric entered or other problem with input
if (!(cin >> card)) {
cout << "You did not enter a number!" << endl;
return (1);
}
//Check if number in required range
if ((card < 1) || (card > 52)) {
cout << "You must enter a number between 1 and 52!" << endl;
return (2);
}
//Display suit and value of chosen card
cout << value[(card - 1) % 13] << " of " << suits[(card - 1) / 13] << endl;
return (0);
}
Look particularly at how I output the suit and value. value and suits are both arrays of strings and using the integer remainder for the value and integer division for the suit, the required element of these arrays can be accessed and their values printed. Try and understand how this works.:thumb:
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
That's basically what I was attempting to do,but couldnt figure out the declarations. So how does your code know how to assign Ace, King, etc?
does:
Code:
const char *value[] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
this assign values 1-13 to these?
and can you break this down?
Code:
cout << value[(card - 1) % 13] << " of " << suits[(card - 1) / 13] << endl;
Some of that I do recognize, but I dont understand how the formula is getting the correct card.
Also, I completed the one you guys gave me to do.
Code:
#include <iostream>
using namespace std;
int main()
{
int num1;
int num2;
cout << "Enter any number: ";
cin >> num1; cin.clear(); cin.ignore(10,'\n');
cout << "Enter any number again: ";
cin >> num2; cin.clear(); cin.ignore(10,'\n');
cout << "\n" << num1 << " and " << num2 << endl;
if (num1 >= 1 && num1 <= 100)
cout << "\nYes " << num1 << " is between 1 & 100.\n" << endl;
else if (num1 < 1 || num1 > 100)
cout << "\nNo " << num1 << " is not between 1 & 100.\n" << endl;
if (num2 == 3 || num2 == 16 || num2 == 45)
cout << "\nYes " << num2 << " is either 3, 16 or 45.\n" << endl;
else if (num2 != 3 || num2 != 16 || num2 != 45)
cout << "\nNo " << num2 << " is neither 3, 16 or 45.\n" << endl;
system ("pause");
return 0;
}
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
And is there a chart or something, or a better way of memorizing which headerfiles and declarations to use
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
Quote:
Originally Posted by
psfign
And is there a chart or something, or a better way of memorizing which headerfiles and declarations to use
As per my experience, this has proven to be quite helpful: http://www.cplusplus.com/reference/