Help!!Urgent..

i try to wrote a program using if function but it doesn't seem to work out smoothly.
===========================
#include <iostream.h>
main ()
{
 char x;
 cout<<"A-to insert";
 cout<<"B-to delete";
 cout<<"C-to edit";
 cout<<"Q-to quit";
 cout<<"Enter your choice";
 cin>>x;
 if (x="A"||"a")
  cout<<insert;
 else if (x="B"||"b")
  cout<<delete;
 else if (x="C"||"c")
  cout<<edit;
 else (x="Q"||"q")
  cout<<quit;
}
=========================
ask user to make a choice on A-to insert,B-to delete,C-to edit or Q-to quit from a menu provided on the screen. If user quit, it must store the program. Any character that is not related is invalid. The menu must accepted both uppercase and lowercase character.
if A,B,C input is accepted, how can it run back to the menu again?
===============================
i'm quite confusing on this function. i try so many times but i still can't manage to work out the problem. please help me out.
yvonnesooAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_lychee_Commented:
1. use single quotes (cos x is a char)... double quotes are for strings...

2. use == (double equal) to compare (single equals assigns to the variable eg. 1st if statement: if(x="A") not compare x to "A"...

3. u need to do long form like if ((x=='A') || (x=='a'))... it's not like english where u say if x equals A or a... each of the terms on either side of a || is a logical expression on its own...

hope u have more luck after reading this :)
0
hongjunCommented:
/* Hope you will award me points
 * I have tried it and it really works
 * And some more I have refined this program using a do-while loop
 */

#include <iostream.h>

main()
{
    char x;
    char toloop='y';

    /* only once semi-colon needed for last statement to be printed */
    /* reading in of input is in the do-while loop to prevent endless loop */
    do {
      cout << endl <<"A-to insert"
                    << endl    /* endl means next line */
             <<"B-to delete" << endl
                 <<"C-to edit"   << endl
             <<"Q-to quit"   << endl << endl
             <<"Enter your choice ";
             cin >> x;

             if ( x == 'A' || x == 'a' ) { /* use single quote for character */
                 cout << "insert";  /* double strings to display unless it is a variable */
                 toloop = 'n';
             }
             else if ( x == 'B' || x == 'b' ) {
                 cout << "delete";
                 toloop = 'n';
             }
             else if ( x == 'C' || x == 'c' ) {
                 cout << "edit";
                 toloop = 'n';
             }
             else if ( x == 'Q' || x == 'q' ) { /* you left out the word if */
                 cout << "quit";
                 toloop = 'n';
             }
             else {
          cout << "You have entered an invalid choice";
                 toloop = 'y';
             }
    } while ( toloop == 'y' );
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hongjunCommented:
Sorry about the poor indentation. Before I submit the source code, everything was okay. However, after I have submitted it, it looks like that. But it can of course works if you copy and paste the source code onto your C editor and will compile successfully without any complaint.

hongjun
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

nietodCommented:
Why use a char for "toloop"?  
Why test for both upper and lowercase characters?
Why not use a switch() statement?


This might be a little cleaner.

#include <iostream.h>

main()
{

   do
   {
       char x;
       bool toloop = false;  

       cout << endl <<"A-to insert" << endl
              <<"B-to delete" << endl
              <<"C-to edit"   << endl
              <<"Q-to quit"   << endl << endl
              <<"Enter your choice ";

      cin >> x;
      x = toupper(x);  // make sure character is capitalized.
      switch (x)
      {
      case 'A':
         cout << "insert";
         break
      case 'B':
         cout << "delete";
         break
      case 'C':
         cout << "edit";
         break
      case 'Q':
         cout << "quit";
         break
      default:
         cout << "You have entered an invalid choice";
         toloop = true; // Make loop repeat.

      }
   } while (toloop);
}
0
_lychee_Commented:
do u need a semicolon after break? i'm not sure..

shouldn't x be declared outside the loop so the value of the decision is still available later?
0
ozoCommented:
You don't even need a toloop variable.
But _lychee_ explained the basic problem.
0
_lychee_Commented:
forget it that wuz dumb...

but shouldn't u quit only when he enters q?

ie.
yadayada...
} while (x!='Q');
0
_lychee_Commented:
ooops.. to avoid misunderstanding, my previous comment wuz addressed to my other comment... not to ozo's...
0
nietodCommented:
>> do u need a semicolon after break? i'm not sure..
I don't, but every one else does   :-).    Yes you do.

>> shouldn't x be declared outside the loop so the
>> value of the decision is still
>> available later?
If you need it later.  You could "handle" the choice made in the swtich.  

>> You don't even need a toloop variable.
Wel there are lots and lots of options, but you need some mechanism to allow unputing to repeat until a valid valuye is obtained.

>> shouldn't u quit only when he enters q
Perhaps.  its hard to say.  Neither yvonnessoo's nor honguin's do.  they both wait for a valid input of any type and then end.   That is useful if you will then usse that vaild input for some action.  i.e. if the code so far is simply a means of obtaining an choice that is later used elsewhere in the program, rather thant this loop actually being the actual program.
0
yvonnesooAuthor Commented:
may i know what the use of toloop?
0
_lychee_Commented:
toloop is a variable that determines if the program is gonna ask the user to enter again...
0
nietodCommented:
it controls whethor or not the "do { ... } while" loop should repeat.  If the user enters an invalid value toloop is set so that the loop repeats and the user is asked to enter a value a 2nd (or 3rd...) time.  If the user enters a valid value, the the toloop variable is set so that the loop ends.
0
hongjunCommented:
If toloop='n', then this means the user has entered a valid choice. In this case, the do loop will then stop looping. If user has entered an invalid choice, toloop is set to 'y' and hence do loop loops again since the loop will loop while toloop=='y'.
Here I made it case-insensitive for input and this means capital letter Q is also a valid choice.
For better user interface, you can try clearing the screen before every prompt for input as shown below.

#include <conio.h>   /* must add this line for clrscr() */
....
....
    do {
        clrscr();    /* clear screen */
       .....
       .....
    } while ...
.....
0
nietodCommented:
clrscr() is not standard C++.  Most compilers will not support it.
0
hongjunCommented:
Then what do you suggest nietod for clearing the screen?
Try system("clear") for UNIX?
Try system("cls") for WINDOWS?
Or using a hex escape character to clear screen?
0
nietodCommented:
>> what do you suggest nietod for clearing the screen?
Whatever is appropriate for the the OS you are running on.  But whhatever approach you use, it will not be portable.  An escape sequence might work on one UNIX machine and not another, it certainly won't work in windows.  The clrscn() function might work on some compilers, and not even compile on others
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.