ENGTECH 1CP3 Lecture 3: Switch Case with Input Validation
Switch Case with Input Validation
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int choice;
cout << "Select the form of Ohm's law needed by choosing the appropriate
number:\n";
cout << "1: Voltage\n";
cout << "2: Current\n";
cout << "3: Resistance\n";
cout << "Your selection (1, 2, or 3) => ";
//cin >> choice;
while (true)
{
cin >> choice;
if (choice = 1 || choice = 4) // parameters
{
break;
}
else
{
cout << "Please try again" << endl;
}
switch (choice)
{
case 1:
cout << "V = I * R\n";
break;
case 2:
cout << "I = V / R\n";
break;
case 3:
cout << "3: Resistance\n";
break;
default:
cout << "That was not one of the proper selections.\n";
break;
}
}
system("pause");
return 0;
}
find more resources at oneclass.com
find more resources at oneclass.com