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