Thread: Hi first time here :)

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    89

    Hi first time here :)

    I need some help with my first program. It compiles just fine... but that is just about it. It is supposed to be a simple calculator but has many problems.
    First of all I cant seem to get the program to return to the menu after each calculation. Also it always performs addition first than division...doesnt matter which choice you make.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    int nChc;
    int nValue1;
    int nValue2;
    
    int main(int argc, char *argv[])
    {
      cout << "Welcome to the DOS Calculator!!!\n"
           << "Please choose from a list of choices:\n"
           << "1) Addition\n"
           << "2) Subtraction\n"
           << "3) Multiplication\n"
           << "4) Division\n"
           << "Enter 1-4: ";
      cin  >> nChc;
      if (nChc = 1)
      {
              cout << "Enter two values to be added:\n"
                   << "Value 1: ";
              cin  >> nValue1;
              cout << "Value 2: ";
              cin  >> nValue2;
              int sum = nValue1 + nValue2;
              cout << nValue1 << " + " << nValue2 << " = " << sum << "\n";
              }
       
       else if (nChc = 2)
           {
            cout << "Enter two numbers to be subtracted\n"
                 << "Value 1: ";
            cin  >> nValue1;
            cout << "Value 2: ";
            cin  >> nValue2;
            int minus = nValue1 - nValue2;
            cout << nValue1 << " - " << nValue2 << " = " << minus << "\n";
            }
       else if (nChc = 3)
        {
            cout << "Enter two numbers to be multiplied\n"
                 << "Value 1: ";
            cin  >> nValue1;
            cout << "Value 2: ";
            cin  >> nValue2;
            int product = nValue1 * nValue2;
            cout << nValue1 << " * " << nValue2 <<  " = " << product << "\n";
            }
       else (nChc = 4);
            {
            cout << "Enter two numbers to be divided\n"
                 << "Value 1: ";
            cin  >> nValue1;
            cout << "Value 2: ";
            cin  >> nValue2;
            float quotient = nValue1 / nValue2;
            cout << nValue1 << " / " << nValue2 << " = " << quotient << "\n";
            }
      system("PAUSE");
        return EXIT_SUCCESS;
    }
    should I be using boolean variables instead?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    why do you need cstdio?
    replace system("PAUSE") by cin.get() and you can also remove cstdlib
    why do you need globals?

    x=4 is assignment
    x == 4 is comparison

    PS. Note that for integer values 5/2 == 2
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    should I be using boolean variables instead?
    No need to use Bools in C++, use integers instead, 0 is false, anything other than 0 is true.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    the include files are already "included" in with my dev C++ compiler.

    thanks!! that solves my first problem. I keep forgetting that the = is actually not a =, but assign a value to an variable

    how do I get the program to return to the menu?

  5. #5
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by eaane74
    the include files are already "included" in with my dev C++ compiler.

    thanks!! that solves my first problem. I keep forgetting that the = is actually not a =, but assign a value to an variable

    how do I get the program to return to the menu?
    Use a do-while loop, and prompt the user if he wants to do another calculation or if he wants to quit.

    If you just want it to loop forever just put a while(1) loop around your code.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Neo1
    No need to use Bools in C++, use integers instead, 0 is false, anything other than 0 is true.
    No Bool is needed. Bool was introduced for very good reasons. One of them was exactly the problem of people using int as a replacement. It isn't type safe and led to an host of very different forms of "bool". Some 0 was false, anything else was true, others 0 was false, 1 was true, others -1 was true, ... not to mention the myriad of ways coders then chose to implement their own bool hack...

    Code:
    enum bool { FALSE, TRUE };
    
    enum bool { false, true };
    
    typedef int bool;
    
    #define TRUE 1
    #define FALSE 0
    
    #define true 1
    #define false 0
    etc... etc... etc... and also where these things were introduced in the code. Some on headers, others not. Files with different names... absolutely no standard way of doing it.

    Bool is a datatype now. Type safe and standard. Use it. It's smarter to do it.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Also, I would recommend that you do something like this:

    (pseudo code)

    Code:
    int choice=0, num1=0, num2=0, result=0;
    
    while ( choice != exit_value )
    {
      print: [Your addition/subtraction/division/multiplication question add an exit option]
      get: choice
    
      print: "Enter number 1:"
      get: num1
    
      print: "Enter number 2:"
      get: num2
    
      /* Now use the value of choice to determine the result */
    
      if choice == addition_value { [...] }
      else if choice == subtraction_value { [...] }
      else if choice == multiplication_value { [...] }
      else if choice == division_value { [...] }
    
      /* inside the { [..] }'s you assign a value to result
         depending on what the user wants to do with the numbers */
    
      print: "Result = " result
    
    }
    This just cuts down on the number of lines of code. Will do the same as what you initially wanted, just with less lines. Professor may be happier with it.

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    You are also using global variables, place these inside main and make them local.
    Double Helix STL

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    89

    using twomers code

    this compiles but still doesnt run...what am I doing wrong?
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    int choice=0, num1=0, num2=0, result=0;
    while ( choice != 0 )
    {
      cout << "Enter 1 for addition\n"
           << "Enter 2 for subtraction\n"
           << "Enter 3 for multiplication\n"
           << "Enter 4 for Division\n"
           << "Enter 0 to exit program";
      cin  >> choice;
    
      cout << "Enter number 1:";
      cin  >> num1;
    
      cout << "Enter number 2:";
      cin  >> num2;
    
      /* Now use the value of choice to determine the result */
    
      if (choice == 1) 
      {
                 result = num1 + num2;
                  }
      else if (choice == 2) 
      {
           result =  num1 - num2;
            }
      else if (choice == 3) 
      {
           result =  num1 * num2;
            }
      else if (choice == 4) 
      {
           result =  num1 / num2;
            }
    
      /* inside the { [..] }'s you assign a value to result
         depending on what the user wants to do with the numbers */
    
      cout << "Result = " << result;
    }
        return EXIT_SUCCESS;
    }

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Choice is initialised to 0 start it at -1 or anything really. You should also do some error checking to make sure that the choice is within the right range.That was my fault, sorry. Didn't compile my pseudo-code
    Last edited by twomers; 01-13-2007 at 04:25 PM.

  11. #11
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    oops...my bad lol...works just fine now. THANKS

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 02:03 AM
  2. Replies: 11
    Last Post: 03-29-2009, 12:27 PM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM