1. Dev-C++ isn't a compiler. It is an IDE. Also, Dev-C++ is old and unmaintained. You should use something more modern or it's successor, wxDev-C++.
2. iostream.h is not a C++ header. As a matter of fact, that shouldn't compile at all. All C++ headers do not have an extension, and C STD headers, are prefixed with "c" and extension removed. See below for more info.
3. conio is generally bad and very, very, decrepit. You also have no use for it here...
4. Onto the real problem as to why your program doesn't work:
while (choice2 == y);
. This doesn't make sense. y isn't initialized so it's undefined on whether it's anything at all.
If you change it to compare to a character, you'll still find that it doesn't work. Why? Because the cin istream still has data in it and it's dumping that data onto choice. So regardless of what you type in, it's going to be the same. Generally the solution is to clear the stream before each use. We can use strings, getline(), and stringstreams to solve all of our problems: http://www.cplusplus.com/forum/articles/6046/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#include <iostream>
#include <sstream>
using namespace std;
template <typename T>
void getInput(T& output)
{
for (;;)
{
std::string input;
getline(cin, input);
// This code converts from string to number safely.
stringstream myStream(input);
if (myStream >> output) //This will break if T is something bogus.
break;
cout << "Invalid value, please try again" << endl;
}
}
int main()
{
int Length1, Length2 ;
int choice1;
char choice2;
int Area, Perimeter;
do
{ //Start of Do/While Loop;
cout << "Choose one of the following\nThe choices are: " << endl;
cout << "1) Area of a Rectangle" << endl;
cout << "2) Perimeter of a Rectangle" << endl;
getInput(choice1);
cout<<"Enter the length of the two sides"<<endl;
cout<<"Length of side 1>>";
getInput(Length1);
cout<<"Length of side 2>>";
getInput(Length2);
switch(choice1)
{
case 1:
Area = (Length1 * Length2);
cout<<"The Area is "<<Area;
break;
case 2:
Perimeter = (Length1 + Length1 + Length2 + Length2);
cout<<"The Perimeter is "<<Perimeter;
break;
default :
cout<<"You have entered an invalid choice";
break;
}
cout<<endl<<"Do you wish to calculate another value(y/n)?"<<endl;
getInput(choice2);
}
while (choice2 == 'y' || choice2 == 'Y');
cout<<endl<<endl;
return 0;
}
|
I'll also note this still isn't a very clean solution. Many things can go wrong.