Member Avatar for Member #36984

HI! I am new to programming (*although i know some html*) and I was wondering if someone could tell me what is wrong with my C++ area calculator? Thanks!

#include <iostream>
using namespace std;
int main (int argc, char **argv)
{
    int height;
    int width;
    int area = height*width;

    cout << "This program was written by Michael Keefe\n\n---------------------------------------------------------------- \n\nWhat is the height of the quadrilateral? \n" << endl;
    cin >> height;
    
    cout << "\nThankyou.\n\nThe height of the quadrilateral is: " << height << "\n\nPlease enter the width of the quadrilateral.\n" << endl;
    cin >> width;

    cout << "\nThankyou.\n\nThe height of the quadrilateral is: " << height << "\nThe width of the quadrilateral is: " << width << "\n\n-------------------------------------------------------\n\nThe area of the quadrilateral you specified is: " << area << endl;   
    cout << "\n\nThis program was developed by Michael Keefe" << endl;
    
    system("pause");
    return (0);
}

Every time I input something, it gives me the area as 152! Even 3x3!

Can someone help?

Dani AI

Generated

Two separate problems show up in this thread: the wrong numeric result (area printed as 152) and the console window closing immediately. was right that the area was being calculated before the program read the height and width, which leaves area holding an indeterminate value (undefined behavior). ’s “rebuild all” tip explains why changes sometimes did not take effect — a stale binary can make it look like fixes had no effect. The console-close symptom comes from the leftover newline in the input buffer after cin >> — the final get() reads that newline and returns immediately.

A clear, robust pattern fixes both issues: initialize variables, read inputs, validate them, compute derived values only after successful input, and then explicitly wait for an Enter key while first discarding any leftover characters. Example:

#include <iostream>
#include <limits>

int main() {
    int height = 0, width = 0;
    std::cout << "Height: ";
    if (!(std::cin >> height)) return 1;           // input check
    std::cout << "Width: ";
    if (!(std::cin >> width))  return 1;
    int area = height * width;
    std::cout << "Area = " << area << '\n';
    std::cout << "Press Enter to exit...";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get();
    return 0;
}

Practical tips: always rebuild or clean+build in the IDE when results look stale; run the compiled EXE from a system terminal to inspect output without inserting pauses; prefer input validation (check cin state) and initialize variables at declaration; avoid system("pause") if aiming for portability. For measurements that can be fractional use double, and for real applications check for negatives and overflow. Credit to , and for the helpful pointers in the thread — the fixes above combine those ideas into a small, safe workflow.

Recommended Answers

All 16 Replies

Member Avatar for Member #36984

P.S. This isn't homework! :cheesy: :cheesy:

I am simply learning how to program!

It because you are setting the area at the begging to a garbage value if you want to update the area set it to height * width after the values are inputted

I am allso new to this. But here is the code I made for the same type of thing.
I am sure the pros will be able to stream line it.

#include<iostream>
using namespace std;

int main()
{cout<<"Welcome to the Handy-Dandy Area Calc.\n"<<"\n";
    cout<<"\n";
    
    int input;
    while ( input != 5 ){
    float x;
    float y;
    float z;
    float r;
    
    
    cout<<"Please make a selection...\n";
    cout<<"1...Square\n";
    cout<<"2...Rectangle\n";
    cout<<"3...Circle\n";
    cout<<"4...Triangle\n";
    cout<<"5...Exit\n";
    cin>> input;
    cin.ignore();
    
          if ( input == 1 ) {
              while ( input == 1 ){
              cout<<"what is the length of one side?  ";
              cin>> x;
              cin.ignore();
              cout<<"The area of your square is "<< x*x;
              x = 0;
              cin.get();
              input = 0;
              }
              }
              else if ( input == 2 ) {
                   while ( input == 2 ){
                   cout<<"What is the length of the rectangle?  ";
                   cin>> x;
                   cin.ignore();
                   cout<<"How wide is the rectangle?  ";
                   cin>> y;
                   cin.ignore();
                   cout<<"The area of your rectangle is "<< x*y;
                   x = 0;
                   y = 0;
                   cin.get();
                   input = 0;
                   }
                   }
              else if ( input == 3 ) {
                   while ( input == 3 ){
                   cout<<"What is the radius of your circle?  ";
                   cin>> r;
                   cin.ignore();
                   cout<<"The area of your circle is "<< 3.14159265*(r*r);
                   r = 0;
                   cin.get();
                   input = 0;
                   }
                   }
              else if ( input == 4 ) {
                   while ( input == 4 ){
                   cout<<"what is the base of the triangle?  ";
                   cin>> x;
                   cin.ignore();
                   cout<<"How tall is the triangle? ";
                   cin>> y;
                   cin.ignore();
                   cout<<"The area of your triangle is "<< (x*y)/2;
                   x = 0;
                   y = 0;
                   cin.get();
                   input = 0;
                   }
                   }
          }

}

I know I should //Comment.
/* I just think with that code it is easy to follow */

Later

Member Avatar for Member #36984

Thanks guys, I shall update my code now! You will have to forgive me if i come back even more confused! I am only 13! If u want u can check out my website at !

Thanks!

Member Avatar for Member #36984

Here is my updated code, but the window appears and then dissapears instantly! any thoughts on what i can do? I am using Dev-C++ and when i do "COMPILE AND RUN" it dissapears immediately.

#include <iostream>
using namespace std;
int main ()
{
    int height;
    int width;
    cout << "This program was written by Michael Keefe\n\n---------------------------------------------------------------- \n\nWhat is the height of the quadrilateral? \n" << endl;
    cin >> height;
    cout << "\nThankyou.\n\nThe height of the quadrilateral is: " << height << "\n\nPlease enter the width of the quadrilateral.\n" << endl;
    cin >> width;
    int area = height*width;
    cout << "\nThankyou.\n\nThe height of the quadrilateral is: " << height << "\nThe width of the quadrilateral is: " << width << endl;    
    cout << "\n\n-------------------------------------------------------\n\nThe area of the quadrilateral you specified is: " << area << endl;   
    cout << "\n\nThis program was developed by Michael Keefe" << endl;
    return (0);
}

I removed the PAUSE bit because it was making my program say:

PRESS ANY KEY TO CONTINUE

as soon as i ran it, and when i pressed a key it closed.

Member Avatar for Member #36984

Thanks dave, my program doesn't close immediately now, but I still get incorrect calculations! :sad:

Here is copied text from a console window when i tried to find the area of a 5 by 5 square:

This program was written by Michael Keefe

----------------------------------------------------------------

What is the height of the quadrilateral?

5

Thankyou.

The height of the quadrilateral is: 5

Please enter the width of the quadrilateral.

5

Thankyou.

The height of the quadrilateral is: 5
The width of the quadrilateral is: 5

-------------------------------------------------------

The area of the quadrilateral you specified is: 152


This program was developed by Michael Keefe
Press any key to continue . . .

Any ideas as to why this happens??

Dunno. It looks like you didn't recompile. Maybe trim the clutter 'til later.

#include <iostream>
using namespace std;

int main()
{
    int height;
    int width;
    cout << "height? ";
    cin  >>  height;
    cout << "width? ";
    cin  >>  width;
    int area = height * width;
    cout << "height = " << height << endl;
    cout << "width  = " << width  << endl;
    cout << "area   = " << area   << endl;
    return 0;
}

/* my output
height? 4
width? 5
height = 4
width  = 5
area   = 20
*/
Member Avatar for Member #36984

That may well be it, thanks!

It would seem that my Dev-C++ is playing up... i will reboot and reinstall it to see what happens. **but i will back up my programs first!** lol :mrgreen:

thx alot

Member Avatar for Member #36984

Ok, sorted! Got the program re-compiled. it was a matter of using the "rebuild all" function.


However, i now cannot get my program to stop to allow me to see the output! very annoying, any ideas?

Follow the link I previously posted and reread it. In it there is another link if you choose to add code to solve this problem.

Member Avatar for Member #36984

I entered this but it still closes immediately:

#include <iostream>
using namespace std;

int main(void)
{
    int height;
    int width;
    cout << "Please enter the height of the quadrilateral: ";
    cin  >>  height;
    cout << "\nPlease enter the width of the quadrilateral: ";
    cin  >>  width;
    int area = height * width;
    cout << "\nHeight = " << height << "CM" << endl;
    cout << "Width  = " << width  << "CM" << endl;
    cout << "\n\nThe area of the quadrilateral is " << area << "CM" << endl;
    std::cout <<"Press [Enter] to continue" <<std::endl;
    std::cin.get();
    return(0);

}

Ah. It grabs the newline left after you entered the width. Add a second one.

Member Avatar for Member #36984

THANKYOU!

My 2nd program is finished! Thanks a load!
By the way, you can download my 1st prog at

Member Avatar for Member #36984

I have uploaded my new prog to my website!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.