Results 1 to 5 of 5

Thread: a simple calculator in C++

  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 21:26.

  2. #2
    Join Date
    Apr 2011
    Posts
    39
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: a simple calculator in C++

    Your logic is wrong,
    so think again,
    and work it out....!!!!

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: a simple calculator in C++

    You could read the first chapters of Stroustrup's book where he uses the well know calculator example. Just one thought, you know, books, these analog pieces of wood and so

  4. #4
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: a simple calculator in C++

    Ah, there are so many things wrong!!

    Work through an example with pencil and paper, following your algorithm.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: a simple calculator in C++

    I can give you one hint that is unrelated to the language -- you will not receive anything on cin until the enter key is hit on the keyboard, so there will be no reaction immediately after the '=' key is pressed (your program will hang until the whole line of text is input). Another thing I can tell you that all your operators are binary operators yet you are reading only one number per operator (which is wrong at least once). Other issues you'll have to solve on your own otherwise you wouldn't learn anything.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

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