CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 18

Threaded View

  1. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: I need help with this simple calculation!!!

    Don't use gotos. Never, ever, *ever* use gotos. (Unless you can demonstrate decisively that they simplify program logic over the alternatives. Which you almost never can really do.)

    The correct way to write this would be:

    Code:
    do {
         {
              cout<<" SC J 1.0 (BETA) " <<endl;
              cout<<endl;
              cout<<"( simple calc J )"<<endl;
              cout<< endl;
         }
             
         // Enter the first value
              
         cout<<"First Number: ";
         cin>> a1;
         cout<<endl;
         cout<<"Press: + to add, - to subtract, * to multiply and / to divide and press enter ";
         cin >> a;
         cout<<endl;
               //if user inputs + then add
               
         if ( a == '+' )
         {
              stuff here
         }
         else if ( a == '-' )
         { 
              //stuff here
         }
         else if ( a == '*' )
         {
               // stuff here
         }
         else if ( a == '/' )
         {
               // stuff here      
         }
    } while (1); // or whatever condition you want to terminate the loop, maybe a particular character.
    Code:
    if (a != '+', '-', '*', '/' )
    This is incorrect use of the comma operator. It does not do what you think it does. In general the correct way to write this (unnecessary in the above example, BTW) would be:
    Code:
    if (a != '+' && a != '-' && a != '*' && a != '/')
    Although, in the particular case where you're trying to match one of several chars you could instead just write
    Code:
    if (strchr("+-*/",a) == NULL)
    Last edited by Lindley; September 21st, 2008 at 12:39 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured