Thread: simple calculator

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    15

    simple calculator

    Ok so I'm making a simple single operator calculator, but I've ran across a problem. At the end of the compiling process, it turns up an error with the return void; line. I'm sure it has to do with the fact that I voided out the opening and ending, but I wanted to check. And if that's the case, what's a good title type to use for main, and a good return value.



    Code:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    using namespace std;
    
    int main(void)
    {
    char i;
    int integer;
    int sum;
      {
      cout << "Choose the operator: 1(+) 2(-) 3(*) 4(/) \n";
      cin >> i;
        switch(i)
        {
          case '+':
            cout << "Enter a series of integer's to add\n"
                 << "Cancel by entering a negative number\n";
            for(integer > 0;;)
              {
              cin >> integer;
              sum = sum + integer;
              }
              cout << sum;
              
            system("PAUSE");
            break;
          case '-':
          
          case '*':
          
          case '/':
          
    system("PAUSE");
    return void;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    You are also missing a closing bracket for the switch block and your indentation is poor.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int integer;
    ...
    for(integer > 0;;)
    First off, integer isn't initialized yet prior to the comparison and secondly, you're doing the comparison in the wrong place within the for loop. Thirdly, avoid using system calls, prefer a call to cin.get(); or something similar.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    15
    Quote Originally Posted by hk_mp5kpdw View Post
    Code:
    int integer;
    ...
    for(integer > 0;;)
    First off, integer isn't initialized yet prior to the comparison and secondly, you're doing the comparison in the wrong place within the for loop. Thirdly, avoid using system calls, prefer a call to cin.get(); or something similar.
    I don't know cin.get() yet, most of this I don't know about. I'm still a super beginner, I'm just trying to do something more complex then I have been doing to help learn more.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    Just use cin.get() anyway, it doesn't take any arguments or anything and AFAIK it's more portable than the system() calls so it's preferable.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by HunterCS View Post
    I don't know cin.get() yet, most of this I don't know about. I'm still a super beginner, I'm just trying to do something more complex then I have been doing to help learn more.
    Then stop. Don't go over what seems to be complex stuff if it just prompts you to write random code. Try something you're confident you can do, and make sure you can, to some extent, explain every line of code you write. You don't need to understand how something is implemented, but you need to understand what it generally does.

    When I think I know something, sometimes I'll write a trivial program that attempts to utilize that knowledge, like I just did last night. The experience last night was good. It helped solidify what I thought were right answers, but it also left more questions opened that I need to deal with. Overall, it was good stuff.

    If you jump to complex stuff without knowing the basics, you could very well just end up writing random stuff, getting frustrated, and possibly have people criticize you. Overall, it's an experience you should avoid if you can.

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    15
    Ok so I'm still trying at this one, I know it's a little above my skill level but i need to finish it so that I can learn it. i mean I know all the commands i'm just not getting something.

    I've updatedthe coding since the last time:

    Code:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
    char i;
    int accumulator;
      {
      cout << "Choose the operator: (+) (-) (*) (/) \n";
      cin >> i;
        switch(i)
        {
          case '+':
            cout << "Enter a series of integer's to add\n"
                 << "Finish by entering a negative number\n";
            for(;;)
            
             {
             int value = 0; 
            
             cin >> value;
             if (value < 0)
              {
              cout << accumulator <<"\n";
            
              system("PAUSE");
              break;
              }
            
             accumulator = accumulator + value;
             
             } 
              
          case '-':
          break;
          case '*':
          break;
          case '/':
          break;
        }  
      }
       system("PAUSE");
       return 0;
    }
    And then it produces this, now I'm not a mathmatician but I know that 5 + 5 does not equal 4,370,442. Now why is this happening, I can not figure it out. Even in the book I have that I'm learning offof, there's an example program that's runs just like this, except it is not in a switch format. I've pretty much copied what he has yet it still comes up horribly bad.



    Choose the operator: (+) (-) (*) (/)
    +
    Enter a series of integer's to add
    Finish by entering a negative number
    5
    5
    -5
    4370442
    Press any key to continue . . .

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Think you're leaving the '\n' in the buffer. After you read in the value of i, call something like cin.ignore().

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No problems with '\n' in the buffer in that code. That will only show up if you use getline or get.

    The problem is that accumulator is uninitialized.

  11. #11
    Registered User
    Join Date
    May 2007
    Posts
    15
    Awesome, now I'm proud of myself, I figured that out about 30 minutes before I got on here to check. I was also irritated at myself because that was just so obvious. Thank you guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Socialising Chat Bots
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 11-28-2007, 08:42 AM
  2. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  3. A Simple Calculator Program Not Working
    By oobootsy1 in forum C++ Programming
    Replies: 9
    Last Post: 01-09-2004, 09:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Simple simple graphics
    By triplem in forum C Programming
    Replies: 2
    Last Post: 05-19-2003, 02:52 AM