simple loop

inside loop everything works fine, calculation and output. but when i enter while loop nothing shows up. i have no errors on debugging.

Sample Output

* Enter rectangle length: 3
* Enter rectangle width: 4
* The area of the rectangle is 12
* The perimeter of the rectangle is 14
* Continue? (y/n): y

* ...........................
* Enter rectangle length: 5
* Enter rectangle width: 3
* The area of the rectangle is 15
* The perimeter of the rectangle is 16
* Continue? (y/n): n
* Press any key to continue . . .

#include <iostream>
#include <string>
using namespace std;

int main()
{

int length;
int width;
int area;
int perimeter;
string choice;

length = 0;
width = 0;
area = 0;
perimeter = 0;

while (choice == "y" || choice == "Y")
{

cout << "Please enter length of the rectangle: ";
cin >> length;

cout << "Please enter widht of the rectangle: ";
cin >> width;

area = length * width;

perimeter = 2 * length + 2 * width;

cout << "The area of the rectangle is " << area << endl;
cout << "The perimeter of the rectangle is " << perimeter << endl;

cout << "Continue? (y/n): ";
cin >> choice;
cin.ignore();

cout << endl << "..........................." << endl;
}

cin.get();

return 0;

}
Last edited on
initialize choice to "Y"

edit:
do while is also an option
Last edited on
yes i figured it out, thanks anyways

choice = 'y';

nice one
what can i say, begginers mistake
another quick question

on my output and on choice "n" i get this


Enter rectangle length: 3
Enter rectangle width: 4
The area of the rectangle is 12
The perimeter of the rectangle is 14
Continue? (y/n): n
.............................
Press any key to continue...


how do i get rid of dots when i press "n" for no?

make cout << endl << "..........................." << endl; as the first statement on the while loop..

or before the cout put a conditional
32
33
if (choice == 'y')
    cout << "...................................." << endl;

Last edited on
Topic archived. No new replies allowed.