-
noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
I apologize for the stupid questions, but I have to have this submitted tonight and this is my first time and I'm over my head with this deadline looming tonight. I appreciate any and all help anyone could provide. I am using Dev-C++
Here is the assignment: Write a program that prompts the user to input the length of a rectangle and the width of a rectangle and then displays the rectangle's area and perimeter.
The program should be broken down in 3 parts:
1. Input the length and the width (two input statements)
2. Calculate the area & perimeter (results should be saved in variables
3. print results.
Format the output so the user can easily read the results. Use the tab escape so its easy to read.
---------------------------------------------------------------
So here's what I have so far. I have been stuck for two days :(
#include iostream
using namespace std;
int main ()
{
int double length;
int width;
int area;
int perimeter;
cout << enter the length of a rectangle: ";
cin >> length;
cout << enter the width of a rectangle: ";
cin >> width;
area = length * width;
cout << "The area of the retangle is << area << endl;
perimeter = length * 2 + width * 2;
cout << "The perimeter of the rectangle is << perimeter << endl;
---------------------------------------------------------------
I dont want to get a zero for not turning this in, I'll continue to try to figure this out, but man...all this is greek to me. Hopefully i can absorb some of this, to be able to pass this class.
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
Stuck where? You're almost done. A few simple compile errors you need to fix and it works.
To give you a kick in the right direction, string literals need to be enclosed in quotes - one at the start, one at the end.
Your #include needs to be wrapped in <>, as in #include <iostream>
A variable can only have one type. You've tried to declare length as an int and a double.
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
Oh? Wow, I guess maybe the compile errors were throwing me off.
but I do see the various errors as far as the quotations. For example:
cout << enter the length of a rectangle: ";
I need to have cout << "enter the length of a rectangle: ";
right? Looks like 4 lines i need to do that properly
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
Quote:
Originally Posted by
psfign
Oh? Wow, I guess maybe the compile errors were throwing me off.
but I do see the various errors as far as the quotations. For example:
cout << enter the length of a rectangle: ";
I need to have cout << "enter the length of a rectangle: ";
right? Looks like 4 lines i need to do that properly
Yeah, whatever your actual string is needs to be wrapped in quotes. I think you're a lot closer than you think you are.
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
Great! Yea, I guess I am. Thank you for the quick response. Unfortunately I have to wait to get home to enter it in the compiler, but I feel a bit more confident now.
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
So thanks to your help, I've finished that first project. And actually have four others that I've done too. Im stuck on how to write part of this new project. Here's how the assignment reads:
"The video game machines at your local arcade output coupons depending on how well you
played the game. You can redeem 10 coupons for a candy bar, 6 coupons for .a medium soft
drink, or 3 coupons for a pack of gum.
Write a program that inputs the number of coupons you want to redeem, and outputs the
number of candy bars, soft drinks, or packs of gum for which the coupons can be redeemed.
After you input the number of coupons, clear the screen before displaying the Coupon
Redemption Schedule.
Account for Input failure. It is possible the user may accidentally enter something other than a
number into a a numeric field. Expect and correct a possible input failure using clear and ignore
statements."
On the example screenshot the professor provided, there is an output of the "Item", "Coupons Required" & "Amount".
So after the input amount is entered, the output looks something like:
Item Coupons Required Amount
Candy Bar 10 3
Soft Drink 6 5
Gum 3 11
Note: Ok, in the program, the above example is all aligned perfectly. Like the numbers are listed directly below "coupons required and "Amount".
I tried the following code but it isn't for this format it seems:
cout << setfill ('.') << left << setw(1) << "Item";
I don't see anywhere in the chapter that explains the format they want.
Again any help would be appreciated! I hope I explained enough.
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
A fairly simple way to do that is to output tabs (\t) between fields.
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
Quote:
cout << setfill ('.') << left << setw(1) << "Item";
What were you trying to accomplish with this? All it does is output the text Item at the current console cursor position.
As GCDEF stated, you can use output tabs to accomplish what you are trying to achieve. But if you want to use the c++ i/o manipulators then you need to consider formatting the output.
Soft Drink is the longest item name so 'Item' needs to be left justified in a field width of this size. Numbers are usually output right justified so 'Coupons required' and 'Amount' will need to be right justified with a space separating the names.
When outputting the data lines the item name again will need to be left justified in a field width. The number of coupons required and amount will need to be right justifed with a field width to correctly align them with the heading.
setw(n) sets the field width to n
left specifies left justification
right specifies right justification
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
Thanks! I ended up with:
cout << "Item" << left << "\t\tCoupon" << "\t\tAmount" << "\n\t\tRequired" << endl;
If there is a simpler way to do this, I'd like to know, but basically Required is aligned below Coupon, so it looks like:
Coupons
Required
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
Quote:
cout << "Item" << left << "\t\tCoupon" << "\t\tAmount" << "\n\t\tRequired" << endl;
If there is a simpler way to do this, I'd like to know,
If you are using tabs, then it can all be output as one string
Code:
cout << "Item\t\tCoupon\t\tAmount\n\t\tRequired\n";
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
Quote:
Originally Posted by
psfign
Thanks! I ended up with:
cout << "Item" << left << "\t\tCoupon" << "\t\tAmount" << "\n\t\tRequired" << endl;
basically Required is aligned below Coupon, so it looks like:
Coupons
Required
That's because you have a \n before required.
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
well yea, that was intentional. but that was the only way i knew how to accomplish 'Coupon Required' to be formatted that way. So, i was meaning if there was an easier way to create the statement without having coupon and required listed separately. But doesnt look like it.
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
Quote:
easier way to create the statement without having coupon and required listed separately.
I'm not sure I understand your meaning - but if you want 'Coupons Required' to be shown together on the same line then just use
Code:
cout << "Item\t\tCoupons Required\t\tAmount\n";
and adjust the number of '\t' to make it display as required
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
yea, i think Im making confusing statments lol. I'm Trying to get (and have accomplished) having the word 'Required' display below the word 'Coupons'. And for a while, I couldnt see how to do that, but i started messing with /t & /n and eventually got the format right. So "cout << "Item" << left << "\t\tCoupon" << "\t\tAmount" << "\n\t\tRequired" << endl;" worked for me.
Tonight, Im going to try your other suggestion using "cout << "Item\t\tCoupon\t\tAmount\n\t\tRequired\n";"
:)
Sorry for being so confusing.
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
The problem with tabs is that you don't necessarily know how many to use.
setw() provides a little better control. The caveat with setw() is that it specifies
the minimum number of characters for the output field width. So, you might
specify set(5), but if 10 characters are needed, 10 characters will be output.
Below shows the output (on my system) for the following code:
Code:
int x[]= {123456,123456789};
int y[]= {123456,123456};
for (size_t i=0; i<2; ++i)
{
cout << x[i] << "\t" << y[i] << endl;
}
for (size_t i=0; i<2; ++i)
{
cout << setw(15) << x[i] << setw(15) << y[i] << endl;
}
// output:
123456 123456
123456789 123456
123456 123456
123456789 123456
-
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;
}
-
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.
-
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 ___.
-
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.
-
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
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
Quote:
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.
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
ouch, im a trial and error learner. :(
-
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...'
-
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;
}
-
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.
-
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;
}
-
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.
-
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.
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
-
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.
-
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/
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
Yes, you're got the hang of if now. Just a comment though. There's no need for the second if in the if statement
Code:
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 the result of the first if fails - is false - then the number must be outside the range ie is not between 1 and 100 so the second if is not needed in this case as it is always true. So this could be written
Code:
if (num1 >= 1 && num1 <= 100)
cout << "\nYes " << num1 << " is between 1 & 100.\n" << endl;
else
cout << "\nNo " << num1 << " is not between 1 & 100.\n" << endl;
The same with the num2 if test. Again the second if test is not needed. Although in this case the inverse logic of not 3 16 or 45 is
Code:
if (num2 != 3 && num2 != 16 && num2 != 45)
If you want to invert an if condition, then reverse each conditon (eg = to !=, > to <= etc) and change || to && and && to ||. For info, this is known as De Morgan's law.
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
Quote:
So how does your code know how to assign Ace, King, etc?
The code doesn't assign Ace, King etc. It creates an array of strings with the individual elements being the strings "Ace", "King" etc. The same with the suits. The individual elements of these arrays can then be accessed by specifying which element is required starting from 0. So "Ace" is element 0, "Two" is element 1 etc.
So looking at
Code:
cout << value[(card - 1) % 13] << " of " << suits[(card - 1) / 13] << endl;
% is the integer modulo operator. It returns the remainder of the the two numbers. So 7 % 3 is 1 as when 7 is divided by 3 the answer is 2 remainder 1. 8 % 5 is 3 as 8 divided by 5 is 1 remainder 3. The result is always a number beween 0 and 1 less than the second number. For 8 % 5 this would be 4, for 7 % 3 this would be 2.
So for the card values, we want a number between 0 and 12 inclusive which is used as the index to the array. The first suite is the input range 1 to 13, the second is 14 to 26 etc. So first subtract 1 to get in range 0 to 51. Then the result of the modulo 13 operator is a number in the range 0 to 12 which indexes the value array to get the card value.
For the suits, again we want an index number in the range 0 to 3 inclusive. In this case we use integer division. So 7 / 3 is 2, 8 / 5 is 1 etc. Using the range 0 to 51 and dividing by 13 this gives a number in the required range as 0 / 13 is 0 and 51 / 13 is 3.
Hope this helps.
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
Thanks for the link Eri523! Looks like that will help a ton!
And thanks for the info 2k! You guys have been awesome! I'll have to re-read some of that, but I get the gist of it. but again, thank you guys for shooting straight with me and leading me to how to learn this better
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
please also tell me how to compile and run a program in Dev c++ latest version.usual methods dont work.but when i hit open and run it opens an open dialog box.it should compile the program shouldnt it.Devc++ 5.4.0
Regards.
-
Re: noob Dev-C++ help. Need to calc area & perimeter of a rectangle (class proj).
kfger, please don't tag a completely different question onto the end of a thread that has nothing to do with what you are asking. You should start a new thread.