#program help

28 messages · Page 1 of 1 (latest)

craggy cedarBOT
#

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.

devout pumice
#

hello guys

#

iam having a problem with this code

#

when I run this code the computer don't go through the while

craggy cedarBOT
#

@devout pumice

Please Do Not Delete Posts!

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.

devout pumice
#
#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 ;
}
devout pumice
#

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 ?

ebon roost
#

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.

devout pumice
#

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

ebon roost
#

That replicates the same problem.

devout pumice
#

hmmm

ebon roost
#

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

devout pumice
#

ahh i see i see

#

okkk thank you for your help

#

have a good day

ebon roost
#

You too sir. Glad I could help.

craggy cedarBOT
#

@devout pumice Has your question been resolved? If so, run !solved :)

devout pumice
#

!solved