The program I have is very simple. It is supposed to just show the area of a rectangle after user input numbers. The problem is the code is correct, but when numbers are typed in the screen and enter is pressed after entering length and width, the screen disappears and doesn't give me output. I have tried a couple of different things to check my code. see below:
/*
an interactive program that computes the area of a rectangle
*/
#include <iostream>
usingnamespace std;
int main()
{
int length; //this declares the length variable
int width; // this declares the width variable
cout << "enter the length: ";
cin >> length; //input the length. input the value of length from the keyboard
cout << "enter the width: ";
cin >> width; //input the width. input the value of width from the keyboard
//window disappears after width input entered and enter key is pressed. it will not show final output.
cout << "the area is ";
cout << length * width; // display the area. This does not display on screen
cin.get();
return 0;
}
If I set the program to automatically set the numbers, the program works perfectly.
/*
an interactive program that computes the area of a rectangle
*/
#include <iostream>
usingnamespace std;
int main()
{
int length; //this declares the length variable
int width; // this declares the width variable
length = 3;
width = 5;
cout << "enter the length: ";
cout << length;
cout << "\n";
//cin >> length; //input the length. input the value of length from the keyboard
cout << "enter the width: ";
cout << width;
cout << "\n";
//cin >> width; //input the width. input the value of width from the keyboard
cout << "the area is ";
cout << length * width; // display the area.
cin.get();
return 0;
}
How do I get the final output to show on the screen?
I am using Dev C++ for my programming. Is that a good programming platform to use for beginners or is there something better I should use?
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/ http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
I am not sure what your problem is. The code appears correct and ran fine for me.
I thought the cin.get(); would dothis. This will only pause the program waiting for input to keep the window open. It has nothing to do with the output.
In this simple program it is not necessary, but I would initialize "length" and "width" when they are defined to avoid any problems. This will be useful in future programs not necessarily in this one. It is good practice to initialize your variables before you use them.
If you are familiar with your debugger put a break point on cout << "the area is "; so you can see what the values of your variables are.
As for the cin.get(); I found out that in my programming platform (Dev C++) the only way I could get my window to stay open was to use the cin.get(); before the return 0; in order to keep the window open on any program I write, even if I don't have any cin >> input.
As for a break point, since I am not doing a loop or a switch, (haven't learned that yet) I am unable to use break;. Is there some other way around this?
Also since I put my code tags in my first post, I ran the program on this web site and everything works just as its supposed to. So why won't it run on my platform?
#include <iostream>
usingnamespace std;
int main()
{
int length; //this declares the length variable
int width; // this declares the width variable
int answer;
do
{
cout << "enter the length: ";
cin >> length; //input the length. input the value of length from the keyboard
cout << "enter the width: ";
cin >> width; //input the width. input the value of width from the keyboard
//window disappears after width input entered and enter key is pressed. it will not show final output.
cout << "the area is ";
cout << length * width; // display the area. This does not display on screen
cout << "\nDo you want to run again? (1 = yes 0 = no): ";
cin >> answer;
}while(answer != 0);
return 0;
}
/*
an interactive program that computes the area of a rectangle
*/
#include <iostream>
#include <limits>
int main()
{
std::cout << "enter the length: ";
int length; //this declares the length variable
std::cin >> length; //input the length. input the value of length from the keyboard
std::cout << "enter the width: ";
int width; // this declares the width variable
std::cin >> width; //input the width. input the value of width from the keyboard
// window disappears after width input entered and enter key is pressed.
// it will not show final output.
std::cin.ignore(1);
std::cout << "The area is " << length * width << '\n'; // display the area.
std::cout << "\nPress ENTER to continue\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
I am very pleased to say that I solved my problem without doing a loop. Since I am new I am trying to figure out my solutions without getting too deep into the programming. The only thing I added was a cin.ignore(); tag, but I also found out there are several other ways to create the same program and get the same results. Thank you everyone.
/*
an interactive program that computes the area of a rectangle
*/
#include <iostream>
#include <limits>
usingnamespace std;
int main()
{
int length; //this declares the length variable
int width; // this declares the width variable
//length = 3;
//width = 5;
cout << "enter the length: ";
cin >> length; //input the length. input the value of length from the keyboard
cout << "enter the width: ";
cin >> width; //input the width. input the value of width from the keyboard
cout << "the area is ";
cout << length * width; // display the area
cout << "\n";
cout << "\npress enter to close window\n";
cin.get(); //gets user input
cin.ignore(); //closes window //added this code only
return 0;
}
I am glad I could keep it simple. I appreciate all the help.
I am not familiar with Dev C++, but the break I speak of is in the use of the IDE during running the program in debug mode. Not any code in the program.
SidV and Enoizat both have a good way of keeping the console window open. Be sure to notice that in Enoizat's example he includes the header file "limits" to use the line of code at line 26.