Help Neede in While Loop...


#include

#include

void operations();

void main()

{

operations();

getch();

}

void operations()

{

int a,b,c,d,menu;

clrscr();

while(d!=27)

{

cout<<"Enter 1st number = ";

cin>>a;

cout<<"Enter 2nd number = ";

cin>>b;

cout<<"
1=Squre";

cout<<"
2=Cube";

cout<<"
3=Subtraction" ;

cout<<"
4=Division";

cout<<"
5=Multiplication";

cout<<"
6=Remainder";

cout<<"
Your Choice: ";

cin>>menu;

switch(menu)

{

case 1:

c=a*a;

break;

case 2:

c=a*a*a;

break;

case 3:

c=a+b;

break;

case 4:

c=a/b;

break;

case 5:

c=a*b;

break;

case 6:

c=a%b;

break;

default:

cout<<"
Your Operation is not Found";

}

if(menu==1)

{

c=a*a;

cout<<"
Square = "<<c;

}

else

{

if(menu==2)

{

c=a*a*a;

cout<<"
Cube = "<<c;

}

else

{

if(menu==3)

{

c=a-b;

cout<<"
Subtraction = "<<c;

}

else

{

if(menu==4)

{

c=a/b;

cout<<"
Division = "<<c;

}

else

{

if(menu==5)

{

c=a*b;

cout<<"
Multiplication = "<<c;

}

else

{

if(menu==6)

c=a%b;

cout<<"
Remainder = "<<c;

cout<<"
";

d++;

}

}

}

}

}

}

}

i want to terminate the while loop during run time after the program is executed few times by pressing ESC button because ASCII code of ESC is 27 but its not closing and program starts executing again and again.. Compiler not giving any error or warning. Help plz...

Comments

  • [code]
    #include
    #include

    void operations(
    void
    );



    int main(
    void
    )
    {
    operations();
    getch();
    }



    void operations(
    void
    )
    {
    int a,b,c,d,menu;
    clrscr();

    while ( d != 27 )
    {
    cout << "Enter 1st number = ";
    cin >> a;
    cout << "Enter 2nd number = ";
    cin >> b;
    cout << "
    1=Squre";
    cout << "
    2=Cube";
    cout << "
    3=Subtraction" ;
    cout << "
    4=Division";
    cout << "
    5=Multiplication";
    cout << "
    6=Remainder";
    cout << "
    Your Choice: ";
    cin >> menu;

    switch ( menu )
    {
    case 1:
    c = a * a;
    break;

    case 2:
    c = a * a * a;
    break;

    case 3:
    c = a + b;
    break;

    case 4:
    c = a / b;
    break;

    case 5:
    c = a * b;
    break;

    case 6:
    c = a % b;
    break;

    default:
    cout << "
    Your Operation is not Found";
    break;
    }

    if ( menu == 1 )
    {
    c = a * a;
    cout << "
    Square = " << c;

    }
    else
    {
    if ( menu == 2 )
    {
    c = a * a * a;
    cout << "
    Cube = " << c;

    }
    else
    {
    if ( menu == 3 )
    {
    c = a - b;
    cout << "
    Subtraction = " << c;
    }
    else
    {
    if ( menu == 4 )
    {
    c = a / b;
    cout << "
    Division = " << c;
    }
    else
    {
    if ( menu == 5 )
    {
    c = a * b;
    cout << "
    Multiplication = " << c;
    }
    else
    {
    if( menu == 6 )
    {
    c = a % b;

    cout << "
    Remainder = " << c;
    }

    cout << "
    ";
    d++;
    }
    }
    }
    }
    }
    }
    }
    [/code]

    Alright, first off d is never initialized. So that first and for most is an issue.

    Second d only gets incremented if the menu option chosen is 6 or invalid, so you would have to choose 6 some undisclosed amount of times to get d from whatever it is to start with, which is whatever is in it's memory when it is created to 27. Which could potentially take a very long time. Especially if worse case it starts at 28...

    If you want to have the esc key be the ending key then you need to set up a handler so that the console gets informed when the key is pressed. Asynckeystate might work as well...

    Other than that you can do...

    [code]
    if ()
    {}
    else if ()
    {}
    else
    {}
    [/code]

    instead the horrid amount of nesting you have now.
  • I'd add that you're playing with fire by reading in numbers directly; if, for example, a character is entered, the input stream gets loopy and all sorts of bad things can occur. To see its effect, try the program below.

    [code]
    #include

    int main(void) {

    int x;

    do {
    std::cout << "Enter a character ('q' to quit): ";
    std::cin >> x;
    } while(x != 'q');

    return 0;
    }[/code]

    To recover from possible stream errors, you can try something like

    [code]
    #include
    ...

    if(std::cin.fail()) {
    std::cin.clear();
    std::cin.ignore(std::numeric_limits::max(), '
    ');
    std::cout << "Stop entering invalid data!" << std::endl;
    }[/code]

    Using a character string to accept numeric input and then converting it may save you from a lot of grief. :P

    I can't recall what cin does with the newline char, but if you're not getting expected values, then you may be picking up '
    ' in subsequent reads of cin... I seldom use C++.

    In your division, make sure you check for div by zero before the actual operation or you'll get an runtime error.

    Good luck. :)


  • I will give you a code that will satisfy your desire about the Esc key,in my next post. But, before that could you please explain why did use the Switch-Case and then again ifs.....i mean doesn't both them doing the sane work.....???!!! Isn't it redundant????

    It seems you are new to C++ so its advisable not to deal with this Esc keys and stuffz now, its not that easy also....

    In the mean time I think what other members have said so far would help you quite a lot.....

  • yes you are right i am new to c++, but i have used switch case because it makes the program looks good... It gives the user a choice what ever he wants to do. e.g. he can select 1 for addition and so on. Now plz tell me how can i terminate the program using Esc key...
  • If your compiler supports getch, just use it and save yourself a bit of grief. :)

    [code]
    int ch;

    do {
    cout << "Enter a key: ";
    ch = getch();

    switch(ch) {
    default : cout << (char)ch << endl;
    }
    } while(ch != 27);
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories