Results 1 to 5 of 5

Thread: a simple calculator in C++

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default a simple calculator in C++

    Hi!

    I'm a Computer Science student doing my first year and I'm still pretty new to C++, we havent't really started with it yet.

    But anyway, I'm trying to write a simple calculator that doesn't have any operator precedence. So 4 + 3 * 5 = 35 and 4 * 3 + 5 = 17.

    Qt Code:
    1. int main(){
    2.  
    3. int number;
    4. float total;
    5. char sign;
    6.  
    7. while(sign != '='){
    8.  
    9. cin >> number;
    10. cin >> sign;
    11.  
    12. if(sign == '+'){
    13.  
    14. total += number;
    15. }
    16. else if(sign == '-'){
    17.  
    18. total -= number;
    19. }
    20. else if(sign == '*'){
    21.  
    22. total *= number;
    23. }
    24. else if(sign == '/'){
    25.  
    26. total /= number;
    27. }
    28. }
    29.  
    30. cout << "Answer = " << total << endl;
    31. return 0;
    32. }
    To copy to clipboard, switch view to plain text mode 

    I'm just doing it all in Main() and I'm using a while loop but this isn't working.

    Does anyone have a better idea?

    appreciate any advise!
    Cheers!
    Last edited by wysota; 4th May 2011 at 20:26.

Similar Threads

  1. Roman Numeral Calculator
    By hazardpeter in forum Qt-based Software
    Replies: 0
    Last Post: 14th April 2010, 10:48
  2. A little calculator problem
    By bijan311 in forum Newbie
    Replies: 37
    Last Post: 15th March 2010, 01:46
  3. Calculator Builder Example
    By janus in forum Newbie
    Replies: 1
    Last Post: 19th April 2008, 09:23
  4. From calculator: how to draw
    By mattia in forum Newbie
    Replies: 1
    Last Post: 26th October 2007, 11:22
  5. Need help with a basic calculator
    By Morea in forum Qt Programming
    Replies: 7
    Last Post: 25th March 2006, 19:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.