Thread: Calculator Program

  1. #1
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    Calculator Program

    ...Okay, I had to do this calculator program and this is what I've done so far..how can I make it so that when I press "E"..the program stops and "enter a number and operator sign: " doesn't display? I'm not sure if I wrote it the most efficient way possible..any advice?



    #include <iomanip.h>
    #include <stdlib.h>

    main()
    { float Num, Accum; char Operator;

    cout<<"\n Printing Calculator Program ";
    cout<<"\n-----------------------------";


    while(Operator != 'E' || Operator != 'e')
    { cout<<"\n\n Enter a Number and Operator Sign: + - * / S E: ";
    cin>>Num; cin>>Operator;


    cout.setf(ios::floatfield, ios::showpoint);
    cout.setf(ios::fixed);
    cout<<setprecision(4);

    switch(Operator)
    { case 'S': case 's':
    Accum=Num;
    cout<<"\nAccumulator = "<<Accum;
    break;

    case '+':
    Accum=Accum+Num;
    cout<<" = "<<Accum;
    break;

    case '-':
    Accum=Accum-Num;
    cout<<" = "<<Accum;
    break;

    case '/':
    Accum=Accum/Num;
    cout<<" = "<<Accum;
    break;

    case '*':
    Accum=Accum*Num;
    cout<<" = "<<Accum;
    break;
    }
    }

    system("PAUSE");
    return 0;
    }
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

  2. #2
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    any suggestions would be great
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    initialize Operator to something other than E or e BEFORE the loop starts and

    make the while conditional && instead of ||. Since Operator cannot be both E and e at the same time your conditional is always true.

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    don't you need <iostream.h> for cout and cin???
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed