i cant get this to loop and i cant get it to error trap

cout << " Please choose an odd number 3-9 to calculate width and height of your choosing\n";
cin >> number;

do
{
    if((number!=3) || (number!=5) || (number!=7) || (number!=9))
    {
        cout << "invalid choice " <<endl << endl;
        }
        else 
        {
            cout << "please choose any symbol on keyboard to create picture " << endl << endl;
            }
while((number==3) || (number==5) || (number==7) || (number==9))

can anyone help me

Edited 6 Years Ago by timbomo: fix mistake

There are no conditions on an else statement. You could have an else if with conditions but I don't think that's what you need here.

The way you have written your if condition, it will never become false. You have written it with OR statements. Only one of the statements must be true for the entire statement to be true. As a result, even if you enter a valid value, the other 3 check values return true making the trap true.

Change the ORs ( "||" ) to ANDs ( "&&" ). For an AND to return true, all sections of it must be true. If you enter a valid value, you only get 3/4 trues so the net result of the trap will be false.

Using this information, how can you modify your do-while?

This is how I generally write my validation loops (taken from a current project):

do {	//current rules not accepted, begin validation loop
	cin.clear();
	cout << "Enter rule No. to change (0-4, -1 to cancel): ";
	cin >> ruleToChange;
        //ruleToChange is an int declared much earlier in the code.
} while (!cin.good() || ruleToChange < -1 || ruleToChange > 4);	//end validation loop

This is one small part of a much larger loop that is used to configure a game's active rules-set.

Edited 6 Years Ago by Fbody: n/a

>> if((number!=3) || (number!=5) || (number!=7) || (number!=9))

Change the OR(||) to AND(&&).

>>
while((number==3) || (number==5) || (number==7) || (number==9))

Change the "==" to != and change the || to &&.

And most of all, inside your do while loop, GET THE INPUT from the console!

EDIT: I just got ninja'd

Edited 6 Years Ago by firstPerson: n/a

wat am i doing wrong to make this loop

while(1)
        {
            cout << '\n' << "Do you wish to continue: (y/n) " << endl;
            cin >> answer;
            if(answer=='y' && answer=='Y')
            break;/* can you do this , and wats a better way to do it*/
                else if(answer=='n'&& answer=='N')
                    {
                        cout << "Thank You For Your Time " << endl;
                        return 0;/* can you do this , and wats a better way to do it*/
                    }
                        else
                        cout << "Please choose a valid option.Either y or n.";
                    }
                }
if(answer=='y' && answer=='Y')
else if(answer=='n'&& answer=='N')

This is the same issue I mentioned in my previous post, but in reverse. Answer can't be both 'y' AND 'Y' at the same time. You are always going to have 1/2 false results which makes the total statement false because of the AND. Use an OR.

I don't think you understand how AND(&&) and OR(||) work. I would suggest you review "Truth Tables" for AND and OR. They may help you with your issue(s).

Edited 6 Years Ago by Fbody: n/a

this will not loop wat am i doing wrong?

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
int  number =0, answer,h=0,width=0, setw=0;
char symbol = '\0',spaces[80] = {' ',' ',' ',' ',' ',' ',' ',' ',' ','\0'};

do
{
cout << "Please choose a letter that corresponds to the shape of your choosing \n";
cout << " A. triangle " << endl;
cout << " B. upside down triangle " << endl;
cout << " C. rectangle " << endl;
cout << " D. plus sign " << endl;
cout << " E. quit " << endl;
cin >> symbol;
cout << " Please choose an odd number 3-9 to calculate width and height of your choosing\n";
cin >> number;

           switch (symbol)
                {
                    case 'a':
                    case 'A':
                                while(number!=3 && number!=5 && number!=7 && number!=9)
                                {
                                        cout << "You have chosen Invalid Number\n";
                                        cout << "Please choose an odd number 3-9 to calculate width and height of your choosing\n";
                                        cin >> number;
                                }
                    cout << " Thank you, you have choosen triangle and " << number << " for height and width" << endl;
                    cout << " Please enter a symbol to design your shape. " << endl;
                    cin >> symbol;
                    cout << '\n';
                    for(int i=0; i<number; i++)
                            {
                                for(int y=-1; y<i; y++)
                                    cout << symbol;
                                    cout << "\n";
                            }
                        cout << '\n' << " Do you wish to continue: (y/n) " << endl;
                        cin >> answer;
                    break;
                    case 'b':
                    case 'B':
                    while(number!=3 && number!=5 && number!=7 && number!=9)
                                {
                                        cout << "You have chosen Invalid Number\n";
                                        cout << "Please choose an odd number 3-9 to calculate width and height of your choosing\n";
                                        cin >> number;
                                }
                    cout << " Thank you, you have choosen upside_down triangle and " << number << " for height and width" << endl;
                     cout << " Please enter a symbol to design your shape. " << endl;
                    cin >> symbol;
                    cout << '\n';
                        for(int k=number;k>=0;k--)
                            {
                                for(int m=1;m<=k;m++)
                                cout << symbol;
                                cout << "\n";
                            }
                        cout << '\n' << " Do you wish to continue: (y/n) " << endl;
                        cin >> answer;
                    break;
                    case 'c':
                    case 'C':
                    while(number!=3 && number!=5 && number!=7 && number!=9)
                                {
                                        cout << "You have chosen Invalid Number\n";
                                        cout << "Please choose an odd number 3-9 to calculate width and height of your choosing\n";
                                        cin >> number;
                                }
                    cout << " Thank you, you have choosen rectangle and " << number << " for height and width" << endl;
                     cout << " Please enter a symbol to design your shape. " << endl;
                    cin >> symbol;
                    cout << '\n';
                        for(int p=0; p<number; p++)
                            {
                                for(int h=0; h<number; h++)
                                cout << symbol;
                                cout << "\n";
                            }
                        cout << '\n' << " Do you wish to continue: (y/n) " << endl;
                        cin >> answer;
                    break;
                    case 'd':
                    case 'D':
                    while(number!=3 && number!=5 && number!=7 && number!=9)
                                {
                                        cout << "You have chosen Invalid Number\n";
                                        cout << "Please choose an odd number 3-9 to calculate width and height of your choosing\n";
                                        cin >> number;
                                }
                    cout << " Thank you, you have choosen plus sign and " << number << " for height and width" << endl;
                    cout << " Please enter a symbol to design your shape. " << endl;
                    cin >> symbol;
                    cout << '\n';
                     for(int i=0; i<number/2; i++)
                        {
                            cout << symbol << endl;
                        }
                          for(int i=0; i<number; i++)
                             {
                              cout << symbol;
                             }
                               for(int i=0; i<number/2; i++)
                                    {
                                      cout << symbol << endl;
                                    }
                        cout << '\n' << " Do you wish to continue: (y/n) " << endl;
                        cin >> answer;
                    break;
                    case 'e':
                    case 'E':
                        cout << " you choose e, so you must want to quit " << endl;
                    break;
                    default:
                            cout << " please enter a valid response. press y for yes and n for no  " << endl;
                }
    cout << '\n' << "Do you wish to continue: (y/n) " << endl;
         if((answer!='y') && (answer!='Y'))
            {
           cout << " thank you " << endl;
            }
           else if((answer=='n')&& (answer=='N'));
           {
               cout << "Thank You For Your Time " << endl;
           }

 }while((number ==3) || (number==5) || (number==7) || (number==9));

return 0;
}

This program loops fine. Based on your previous posts however, your do-while loop is supposed to be your error trap. If you want the input sequence to loop while the input is invalid, you need to close the loop much sooner than line 131. It should be closed somewhere more like line 21.

Plus, I should also mention to you that line 121 is redundant. Get rid of it. Also note, your variable "answer" is declared as an int, but you are expecting a char input and trying to use it as such. This will cause input stream corruption. Change "answer" to a char.

This question has already been answered. Start a new discussion instead.