ENGTECH 1CP3 Lecture 3: Switch Case with Input Validation

25 views1 pages
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
Unlock document

This preview shows half of the first page of the document.
Unlock all 1 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers