Skip to content

Instantly share code, notes, and snippets.

Created September 28, 2010 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f651ee79147eaa5cf438 to your computer and use it in GitHub Desktop.
Save anonymous/f651ee79147eaa5cf438 to your computer and use it in GitHub Desktop.
# include <iostream>
# include <string>
# include <cstdlib>
void readValue(int& v, const std::string& str)
{
std::cout << "Please enter " << str << " and press ENTER: ";
std::cin >> v;
if( v <= 0)
{
std::cout << str << " must be a non-negative value. Try again" << std::endl;
readValue(v, str);
}
}
int main()
{
std::cout << "This program calculates area" << std::endl;
int w;
while (42)
{
std::cout << "0 to exit; 1 for triangle; 2 for trapeze" << std::endl;
std::cin >> w;
if (w == 0) break;
if (w == 1)
{
int a,h;
readValue(a, "Base length");
readValue(h, "Vertical height");
std::cout << "Area of triangle is: " << a*h/2. << std::endl; // Дабл, не теряй результат
}
else if (w == 2)
{
int a,b,h;
readValue(a, "Base length of A");
readValue(b, "Base length of B");
readValue(h, "Vertical height(h)");
std::cout << "Area of trapeze is:" // У тебя тут Треугольник написано
<< (a+b)/2.*h << std::endl;
}
else std::cout << "Wrong value. Try again." << std::endl;
}
std::cout << "Thank's for using" << std::endl;
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment