When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
28 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
hello guys
iam having a problem with this code
when I run this code the computer don't go through the while
@devout pumice
Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. You can use !solved to close a post and mark it as solved.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double price;
int period;
double Reduction;
char Type;
double VAT=11;
cout << "\nInput your subsricption:\n"
<< "Type A for 50 dollars per month\n"
<< "Type B for 75 dollars per month\n"
<< "Type C for 100 dollars per month\n";
cin >> Type;
while (Type != 'A' || Type != 'B'|| Type != 'C')
{
cout << "\nInput your subsricption:\n"
<< "Type A for 50 dollars per month\n"
<< "Type B for 75 dollars per month\n"
<< "Type C for 100 dollars per month\n";
cin >> Type;
}
cout << "input period in your month subsricption:\n";
cin >> period;
while (period < 1 || period > 12)
{
cout << "input period in your month subsricption:\n";
cin >> period;
}
switch (Type)
{
case 'A':
price = 50;
break;
case 'B':
price=75;
break;
case 'C':
price = 100;
break;
default:
cout << "invalid input!";
break;
}
if (period == 1)
Reduction = 0;
else if (period >2 || period <6)
Reduction=10;
else if (period > 6 || period < 12)
Reduction=15;
price = price + price * VAT/100;
price = price - price * Reduction/100;
cout << "the total is: \n" << price;
return 0 ;
}
when i run this code the computer doesn't go through the while loop
for example it gives me output like this
and it dont stop
Input your subsricption:
Type A for 50 dollars per month
Type B for 75 dollars per month
Type C for 100 dollars per month
A
Input your subsricption:
Type A for 50 dollars per month
Type B for 75 dollars per month
Type C for 100 dollars per month
any solutions ?
The problem is with you while condition
You are using the || operator, you should be using the &&
Let me give you an example, let's say you input B. The || operator basically means that all you need is for one of the parts to be true, for it to be true, so if you, for example, input B, the Type != 'A' would be true, so it would keep looping.
@devout pumice Do you understand the issue?
Doing while(Type != 'A && Type != 'B' && Type != 'C') would sove this.
i see but why it did work with the do while loop let me send it to you 1 sec
#include <iostream>
using namespace std;
int main() {
int period;
char type;
double price, reduction, VAT = 11;
do {
cout << "Input the type of inscription: "
<< "Type A for 50$ / month "
<< "Type B for 75$ / month "
<< "Type C for 100$ / month ";
cin >> type;
} while (type != 'A' || type != 'B' || type != 'C');
do {
cout << "Input the period in month of your subscription: ";
cin >> period;
} while (period < 1 || period > 12);
switch (type) {
case 'A': price = 50; break;
case 'B': price = 75; break;
case 'C': price = 100; break;
}
if (period > 1 || period < 6) reduction = 10;
else if (period > 6 || period < 12) reduction = 15;
else reduction = 0;
price = price + price * VAT/100;
price = price - price * reduction/100;
cout << "the total is: " << price;
return 0;
}
using do while loop
That replicates the same problem.
hmmm
Don't use || when you have !=on both sides, because the condition will always be true.
if( x != 3 || x != 4) this will always be true, if x = 3 then it is != 4, if x = 4 then it is != 3, and if x = some number it will be != 3 and != 4
You too sir. Glad I could help.
@devout pumice Has your question been resolved? If so, run !solved :)
!solved