Reputation: 1
I am new at c++ and for an assignment I have a program that requires a switch within a while but i keep getting stuck in an infinite loop
I have tried looking up ways to solve it but since i am not skilled at c++, it is really hard for me to get my error
#include <iostream>
#include <stdio.h>
using namespace std;
int main(void)
{
float length, width, perimeter, area;
char ans;
cout<<"Please enter the length of the rectangle: \n";
cin>>length;
cout<<"Please enter the width of the rectangle: \n";
cin>>width;
cout<<"What do you wish to do with these values?\n";
cout<<"Choose an option from the menu: \n";
cout<<"1 - Calculate Perimeter\n";
cout<<"2 - Calculate Area\n";
cout<<"3 - Quit\n";
cout<<"Please enter your choice: \n";
cin>>ans;
while (ans != '3')
{
printf("give option: "); //found this online
ans = getchar(); //this too
switch (ans)
{
case '1' :
perimeter=2*(length+width);
cout<<"The perimeter of the rectangle with length "<<length<<" and width "<<width<<" is "<<perimeter<<endl;
break;
case '2' :
area=length*width;
cout<<"The area of the rectangle with length "<<length<<" and width "<<width<<" is "<<area<<endl;
break;
default :
cout<<"Invalid Entry, please only select options from menu"<<endl;
}
}
printf("Program finished...\n"); //this was online too
return 0;
}
when i enter the option 2 or 1, there is an infinite loop and i cant seem to fix that. I am not use to formatting on this site, please excuse the way i formatted my code
Upvotes: 0
Views: 73
Reputation: 206697
getchar()
is not the right function to use there. It returns all characters, spaces, newlines, etc.
If you add a line to output the value of ans
right after that, you will notice all the values that are assigned to ans
.
ans = getchar();
cout << "ans: " << (int)ans << endl;
To skip whitespaces from the stream, use
cin >> ans;
In addition, the logic to get ans
inside the while
loop is flawed. It should be after the switch
statement. Otherwise, your program tries to read ans
twice before the first execution of the switch
statement.
Here's an updated version of the relevant code that works for me.
cout << "Please enter your choice: \n";
cin >> ans;
while (ans != '3')
{
switch (ans)
{
case '1' :
perimeter=2*(length+width);
cout<<"The perimeter of the rectangle with length "<<length<<" and width "<<width<<" is "<<perimeter<<endl;
break;
case '2' :
area=length*width;
cout<<"The area of the rectangle with length "<<length<<" and width "<<width<<" is "<<area<<endl;
break;
default :
cout<<"Invalid Entry, please only select options from menu"<<endl;
}
cout << "Please enter your choice: \n";
cin >> ans;
}
Upvotes: 1
Reputation: 341
Here is some help regarding formatting : https://stackoverflow.com/editing-help. Indent the entire code 4 spaces to the right for a code block. You must also be able to see the preview below as you keep writing the question.
getchar() isn't the appropriate function to use here. Read about the nuance here : http://www.cplusplus.com/forum/general/193480/
cin will be more appropriate to use.
Also, analyse your code step by step. You have an input taking line outside the while loop, and one inside too. Realise why this is wrong and try fixing it.
Since this is an assignment question, I won't be telling you the answer, but hopefully lead you to understand where you are going wrong.
After you solve the problem, please go back and analyse why your original code did not work. It helps immensely.
Upvotes: 0