d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Infinite Loop Problem.
Add Reply New Topic New Poll
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Nov 21 2014 08:07pm
I've been stuck on this for awhile and I cannot figure out why I have an infinite loop.

Code
int switch2()
{
bool valid = true;
int month;
do
{


std::cout << "Please select the month of this bill cycle by entering the numerical month\n";
std::cout << "and pressing the return key.\n\n";
std::cout << "\t1 for January\n";
std::cout << "\t2 for February\n";
std::cout << "\t3 for March\n";
std::cout << "\t4 for April\n";
std::cout << "\t5 for May\n";
std::cout << "\t6 for June\n";
std::cout << "\t7 for July\n";
std::cout << "\t8 for August\n";
std::cout << "\t9 for September\n";
std::cout << "\t10 for October\n";
std::cout << "\t11 for November\n";
std::cout << "\t12 for December\n";

int mon;
switch(mon)
{
case 1:
std::cout << "January\n";
month = 31;
valid = true;
break;
case 2:
std::cout << "February\n";
month = 28;
valid = true;
break;
case 3:
std::cout << "March\n";
month = 31;
valid = true;
break;
case 4:
std::cout << "April\n";
month = 30;
valid = true;
break;
case 5:
std::cout << "May\n";
month = 31;
valid = true;
break;
case 6:
std::cout << "June\n";
month = 30;
valid = true;
break;
case 7:
std::cout << "July\n";
month = 31;
valid = true;
break;
case 8:
std::cout << "August\n";
month = 31;
valid = true;
break;
case 9:
std::cout << "September\n";
month = 30;
valid = true;
break;
case 10:
std::cout << "October\n";
month = 31;
valid = true;
break;
case 11:
std::cout << "November\n";
month = 30;
valid = true;
break;
case 12:
std::cout << "December\n";
month = 31;
valid = true;
break;
default:
std::cout << "INVALID INPUT!! Please select 1 - 12.\n";
valid = false;
break;
}
}
while(!valid);
return month;
}


You can do switch statements using numbers correct?

Edit: The switch goes to default and then loops forever.

This post was edited by NinjaSushi2 on Nov 21 2014 08:14pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 21 2014 08:14pm
You are never inputting anything and assigning it to mon. WHich means it is initialized to 0, which is why it always hit the default case...

That should have been apparent when it never halted for user input..........

This post was edited by Minkomonster on Nov 21 2014 08:15pm
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Nov 21 2014 08:15pm
Quote (Minkomonster @ 21 Nov 2014 21:14)
You are never inputting anything and assigning it to mon. WHich means it is initialized to 0, which is why it always hit the default case...


Dear god.. how the hell did not I recognize that. :rofl:
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 21 2014 08:16pm
Quote (NinjaSushi2 @ Nov 21 2014 09:15pm)
Dear god.. how the hell did not I recognize that.  :rofl:


I have no clue... I am sitting here staring at off into space in bewilderment. You said you ahve been stuck on that for a while?
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Nov 21 2014 08:17pm
Thanks again mate!

Code
int switch2()
{
bool valid = true;
int month;
do
{
std::cout << "Please select the month of this bill cycle by entering the numerical month\n";
std::cout << "and pressing the return key.\n\n";
std::cout << "\t1 for January\n";
std::cout << "\t2 for February\n";
std::cout << "\t3 for March\n";
std::cout << "\t4 for April\n";
std::cout << "\t5 for May\n";
std::cout << "\t6 for June\n";
std::cout << "\t7 for July\n";
std::cout << "\t8 for August\n";
std::cout << "\t9 for September\n";
std::cout << "\t10 for October\n";
std::cout << "\t11 for November\n";
std::cout << "\t12 for December\n";

int mon;
std::cin >> mon;
switch(mon)
{
case 1:
std::cout << "January\n";
month = 31;
valid = true;
break;
case 2:
std::cout << "February\n";
month = 28;
valid = true;
break;
case 3:
std::cout << "March\n";
month = 31;
valid = true;
break;
case 4:
std::cout << "April\n";
month = 30;
valid = true;
break;
case 5:
std::cout << "May\n";
month = 31;
valid = true;
break;
case 6:
std::cout << "June\n";
month = 30;
valid = true;
break;
case 7:
std::cout << "July\n";
month = 31;
valid = true;
break;
case 8:
std::cout << "August\n";
month = 31;
valid = true;
break;
case 9:
std::cout << "September\n";
month = 30;
valid = true;
break;
case 10:
std::cout << "October\n";
month = 31;
valid = true;
break;
case 11:
std::cout << "November\n";
month = 30;
valid = true;
break;
case 12:
std::cout << "December\n";
month = 31;
valid = true;
break;
default:
std::cout << "INVALID INPUT!! Please select 1 - 12.\n";
valid = false;
break;
}
}
while(!valid);
return month;
}
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Nov 21 2014 08:18pm
Quote (Minkomonster @ 21 Nov 2014 21:16)
I have no clue... I am sitting here staring at off into space in bewilderment. You said you ahve been stuck on that for a while?


Yeah. lmfao!! What's funnier is I was comparing it to my previous switch. LOL!!!!!!!!

Code
int switch1()
{
bool valid = true;
int planMin;
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\n";
planMin = 450;
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";
planMin = 900;
valid = true;
break;
case 'c':
case 'C':
std::cout << "\tPlan C - $69.00 per month; unlimited minutes provided.\n\n";
planMin = 0;
valid = true;
break;
default:
std::cout << "INVALID INPUT!! Please select a correct plan: A, B, or C.\n";
valid = false;
break;
}
}
while (!valid);
return planMin;
}
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll