d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Return To The Beginning Of A Switch Statement > If User Input Is Invalid.
Add Reply New Topic New Poll
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Nov 17 2014 06:17pm
C++

I am not sure why I am getting errors cuz noob. Can anyone shed any light on this? I am trying to make a switch statement repeat if the user inputs an invalid choice/data.

Code
// Michael C. Anderson
// 17-NOV-2014

#include <iostream>
#include <iomanip>



int main()
{
//int totalMinutes, overageMinutes, totalCost, overageCost;
//const float PLAN_A = 39.99, PLAN_B = 59.99, PLAN_C = 69.99;
//char customerPlan, A = PLAN_A, B = PLAN_B, C = PLAN_C;


std::cout << "You can use this program to determine if there is a more cost effective cell phone plan available.\n";
std::cout << "The current plans available are as follow." << std::endl << std::endl;
std::cout << "\tPlan A - $39.99 per month; 450 minutes provided.\n\tAdditional minutes are $0.45 per minute.\n\n";
std::cout << "\tPlan B - $59.99 per month; 900 minutes provided.\n\tAdditional minutes are $0.40 per minute.\n\n";
std::cout << "\tPlan C - $69.00 per month; unlimited minutes provided.\n\n";
std::cout << "Now we will collect data from you regarding your usage.\n";
std::cout << "Please select the current plan you have by typing: A for plan A, B for plan B, and C for plan C,\n";
std::cout << "followed by the 'enter/return' key.\n\n";
//std::cin >> customerPlan;
//std::cout << "\nYou entered plan: " << customerPlan << std::endl << std::endl;

bool valid;
do{
char customerPlan;
std::cin >> customerPlan;
//std::cout << "\nYou entered plan: " << customerPlan << std::endl << std::endl;

switch(customerPlan)
{
case 'a':
case 'A':
std::cout << "\tPlan A - $39.99 per month; 450 minutes provided.\n\tAdditional minutes are $0.45 per minute.\n";
valid = true;
break;

case 'b':
case 'B':
std::cout << "\tPlan B - $59.99 per month; 900 minutes provided.\n\tAdditional minutes are $0.40 per minute.\n\n";
valid = true;
break;

case 'c':
case 'C':
std::cout << "\tPlan C - $69.00 per month; unlimited minutes provided.\n\n";
valid = true;
break;

default:
std::cout << "Invalid input!! Please select a correct plan: A, B, or C.\n";
valid = false;
break;
}
while(!valid);
}
return 0;
}


#############################
This is the code I was using as a reference.
#############################

Code
bool valid;
do
{
char answer;
cin >> answer;
switch (answer)
{
case 'y':
case 'Y':
inventory[0] = "Short Sword";
cout << "\nYou place the Rusty Battle Axe in the chest.";
valid = true;
break;

case 'n':
case 'N':
inventory[0] = "Rusty Battle Axe";
cout << "\nYou leave the Short Sword in the chest.";
valid = true;
break;

default :
cout << "\nThat was an invalid response.";
valid = false;
break;
}
}
while (!valid);
}






This is my switch statement with all the other crap removed.

Code
bool valid;
do{
char customerPlan;
std::cin >> customerPlan;
//std::cout << "\nYou entered plan: " << customerPlan << std::endl << std::endl;

switch(customerPlan)
{
case 'a':
case 'A':
std::cout << "\tPlan A - $39.99 per month; 450 minutes provided.\n\tAdditional minutes are $0.45 per minute.\n";
valid = true;
break;

case 'b':
case 'B':
std::cout << "\tPlan B - $59.99 per month; 900 minutes provided.\n\tAdditional minutes are $0.40 per minute.\n\n";
valid = true;
break;

case 'c':
case 'C':
std::cout << "\tPlan C - $69.00 per month; unlimited minutes provided.\n\n";
valid = true;
break;

default:
std::cout << "Invalid input!! Please select a correct plan: A, B, or C.\n";
valid = false;
break;
}
while(!valid);
}


This post was edited by NinjaSushi2 on Nov 17 2014 06:21pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 17 2014 06:25pm
Quote
I am not sure why I am getting errors cuz noob. Can anyone shed any light on this? I am trying to make a switch statement repeat if the user inputs an invalid choice/data.


whatever you do, dont even think about telling us what the error is!

few trouble shooting tips.
1) keep the main method small. it shouldnt have all that crap. put that into another function like run(), which makes things much easier to test
2) put that switch into another function. easier to test/troubleshoot
3) now to test just your switch statement, you can call it from main.

this will help fix your indentations. offhand, it looks like your "while" is inside the do block instead of out of it.
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Nov 17 2014 06:32pm
Quote (carteblanche @ 17 Nov 2014 19:25)
whatever you do, dont even think about telling us what the error is!

few trouble shooting tips.
1) keep the main method small. it shouldnt have all that crap. put that into another function like run(), which makes things much easier to test
2) put that switch into another function. easier to test/troubleshoot
3) now to test just your switch statement, you can call it from main.

this will help fix your indentations. offhand, it looks like your "while" is inside the do block instead of out of it.


I didn't feel like using functions for this homework. hahaha I probably should so it doesn't look like shit. Thanks for noticing the while loop out of place mate.

You can have my body to keep you warm tonight. :evil:

Edit: That fixed it. I had the while in the do block.

This post was edited by NinjaSushi2 on Nov 17 2014 06:34pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 17 2014 06:40pm
Quote (NinjaSushi2 @ Nov 17 2014 07:32pm)
I didn't feel like using functions for this homework. hahaha I probably should so it doesn't look like shit. Thanks for noticing the while loop out of place mate.

You can have my body to keep you warm tonight.  :evil:

Edit: That fixed it. I had the while in the do block.


what IDE are you using that didn't put a squiggly line under it? notepad.exe?
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Nov 17 2014 06:54pm
DEV C++
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Nov 17 2014 07:01pm
Some crazy bitch just got mad at me for "squeaking my chair and tapping my feet". She said I should take off my headphones so I can hear how loud the squeaking is how inconsiderate it is. She then told me I should try to sit still more.

I can't control if the chair squeaks and the tapping is me typing on the keyboard. My headphones are irreverent in this situation. What a crazy old hag. I told her to make a formal complaint about the chairs and how they should WD40 them. She said I wasn't worth a formal complaint. What fucking logic is she using?

/end rant.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 17 2014 07:39pm
Quote (NinjaSushi2 @ Nov 17 2014 08:01pm)
Some crazy bitch just got mad at me for "squeaking my chair and tapping my feet". She said I should take off my headphones so I can hear how loud the squeaking is how inconsiderate it is. She then told me I should try to sit still more.

I can't control if the chair squeaks and the tapping is me typing on the keyboard. My headphones are irreverent in this situation. What a crazy old hag. I told her to make a formal complaint about the chairs and how they should WD40 them. She said I wasn't worth a formal complaint. What fucking logic is she using?

/end rant.


she's clearly hitting on you, trying to get your headphones off so she can talk to you. can't you tell by the way she was watching you wiggle your feet?

This post was edited by carteblanche on Nov 17 2014 07:42pm
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Nov 17 2014 09:56pm
Quote (carteblanche @ 17 Nov 2014 20:39)
she's clearly hitting on you, trying to get your headphones off so she can talk to you. can't you tell by the way she was watching you wiggle your feet?


That's not all I wiggled at her. ;)
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll