Jump to content

C++ Help


Recommended Posts

Ok, so I have to write a basic program for C++ and I've been working on it for hours and have gotten nowhere, i was wondering if any of you had any computer programming background and could help me figure out where I'm going wrong: here is what I've got so far, as well as my errors://My Name//CSCI1010 //Programming Assignment //January 31, 2006/*This program will request a length and width from the keyboard, calculate the area of a rectange with those dimensions, and display to the monitor length, width, and area in tabular form.*/#include <iostream>using namespace std;int main( ){ int width, height; cout << "Enter Rectangle Width: "; cin >> width; cout << "Enter Rectangle Height: "; cin >> height; area = width * height; perimeter = 2 * width + 2 * height; cout << "area: "; cout << "perimeter: "; return 0;}~~"pass2.cpp" 24L, 580C written [myname@cscilinux2 myname]$ g++ pass2.cpppass2.cpp: In function `int main()':pass2.cpp:18: `area' undeclared (first use this function)pass2.cpp:18: (Each undeclared identifier is reported only once for each function it appears in.)pass2.cpp:19: `perimeter' undeclared (first use this function)

Link to post
Share on other sites

Here are updated errors:"pass2.cpp" 24L, 580C written [myname@cscilinux2 myname$ g++pass2.cpp-bash: g++pass2.cpp: command not found[myname@cscilinux2 myname]$ g++ pass2.cpppass2.cpp: In function `int main()':pass2.cpp:18: `area' undeclared (first use this function)pass2.cpp:18: (Each undeclared identifier is reported only once for each function it appears in.)pass2.cpp:19: `perimeter' undeclared (first use this function)[myname@cscilinux2 myname]$

Link to post
Share on other sites
you need to put area and perimeter as an integarso int width, height, area, perimeter;let me think about it for a while
When I added that and compiled, no errors came up but it's like the program didn't try to run, now I'm relaly confused.
Link to post
Share on other sites
i don't remember a ton of c++try putting #include<iostream.h>
This caused a massive error. I took it out and added Onlyme's suggestion and the program again came up with no errors, but failed to run. I have no idea what that means.
Link to post
Share on other sites

OK, I'm sort of an idiot and I'm getting really close. I managed to compile the program, but forgot to run it. Ya, I'm smart like that. Anyways, when I add onlyme's additions it runs the output asking me for the width/height, but when I hit enter, it doesn't calculate the area and perimeter, what am I missing?

Link to post
Share on other sites
OK, I'm sort of an idiot and I'm getting really close. I managed to compile the program, but forgot to run it. Ya, I'm smart like that. Anyways, when I add onlyme's additions it runs the output asking me for the width/height, but when I hit enter, it doesn't calculate the area and perimeter, what am I missing?
hit on me AIM, onlyme386much easier.
Link to post
Share on other sites

Here's the whole shebang thus far.//Programming Assignment 2//January 31, 2006/*This program will request a length and width from the keyboard, calculate the area of a rectange with those dimensions, and display to the monitor length, width, and area in tabular form.*/#include<iostream>using namespace std;int main( ){ int width = 0; int height = 0; int area = 0; int perimeter = 0; cout << "Enter Rectangle Width: "; cin >> width; cout << "Enter Rectangle Height: "; cin >> height; area = width * height; perimeter = 2 * width + 2 * height; cout << "area: "; cout << "perimeter: "; return 0;}~"pass2.cpp" 27L, 627C written [myname@cscilinux2 myname]$ g++ pass2.cpp[myname@cscilinux2 myname]$ ./a.outEnter Rectangle Width: 12Enter Rectangle Height: 9area: perimeter:

Link to post
Share on other sites

01001001-01110100-00100000-01101001-01110011-00100000-01110011-01101111-00100000-01110011-01100001-01100100-00100000-01110111-01100001-01110100-01100011-01101000-01101001-01101110-01100111-00100000-01111001-01101111-01110101-00100000-01100111-01110101-01111001-01110011-00100000-01100110-01101111-01101100-01100100-00100000-01110100-01101111-00100000-01100011-01101111-01101101-01101101-01111001-00100000-01110000-01110010-01100101-01110011-01110011-01110101-01110010-01100101-00100000-01100001-01101110-01100100-00100000-01110101-01110011-01100101-00100000-01000011-00100000-01110000-01101100-01110101-01110011-00100000-01110000-01101100-01110101-01110011-00100000-01110010-01100101-01100001-01101100-00100000-01110000-01110010-01101111-01100111-01110010-01100001-01101101-01101101-01100101-01110010-01110011-00100000-01110101-01110011-01100101-00100000-01100010-01101001-01101110-01100001-01110010-01111001-

Link to post
Share on other sites
Here are updated errors:"pass2.cpp" 24L, 580C written [myname@cscilinux2 myname$ g++pass2.cpp-bash: g++pass2.cpp: command not found[myname@cscilinux2 myname]$ g++ pass2.cpppass2.cpp: In function `int main()':pass2.cpp:18: `area' undeclared (first use this function)pass2.cpp:18: (Each undeclared identifier is reported only once for each function it appears in.)pass2.cpp:19: `perimeter' undeclared (first use this function)[myname@cscilinux2 myname]$
Always instantiate your variables
Link to post
Share on other sites

I have some assignments and 6 power point lecture notes on a few chapters from the C++ book we used in my university course. I can send them if you're interested.

Link to post
Share on other sites

Let's change it for a little better style (I changed what's in the quote). You should declare variables as late as possible, only C makes you declare variables at the beginning of a function. Here you see area and perimeter are declared the first time they are used, and they are initialized when declared (never declare an unitialized variable if you can help it).It also seems like you forgot some of the parts of the assignment "display to the monitor length, width, and area in tabular form"!

//Programming Assignment 2//January 31, 2006/*This program will request a length and width from the keyboard, calculate the area of a rectange with those dimensions, and display to the monitor length, width, and area in tabular form.*/#include<iostream>using namespace std;int main( ){ int width = 0; int height = 0; cout << "Enter Rectangle Width: "; cin >> width; cout << "Enter Rectangle Height: "; cin >> height; int area = width * height; int perimeter = 2 * width + 2 * height; cout << "area: " << area << "perimeter: " << perimeter << endl; return 0;}
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...