Help with input to floating point values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • massdeletion101
    New Member
    • Mar 2007
    • 23

    Help with input to floating point values

    I'm making a calculator that is Supposed to calculate floating point values, but somethign goes wrong along the way (can't be sure what). It calculates intagers just fine, but when I add a decimal with an extra intager on, something funky happens. It infinitly displays a rounded number. For instance, if I type in "3+1.3", instead of displaying "4.3" once like it should, it will just display "4" line by line infinatly over the time span that I don't hit the X at the top right. Can someone perhaps tell me a solution to this? If it will help, I have the code right here:

    Code:
    #include <iostream>
    using namespace std;
    int main()	\\calculator function
    {
       cout << "Press '*' to multiply two numbers (ex: 2*2 plus enter key)\n\n";	  \\start of instructions
       cout << "Press '/' to divide two numbers (ex: 2/2 plus enter key)\n\n";
       cout << "Press '+' to add to two numbers (ex: 2+2 plus enter key)\n\n";
       cout << "Press '-' to subtract a number from another(ex: 2-2 plus enter key)\n\n";	\\end of instructions
       cout << "Press 'Q' or 'q' to exit\n\n";
       char character;
       while(character != 'Q')
       {  long double value;
          getline(cin, value, '*' || '/' || '+' || '-');
          cin >> character;
          long double value2;
          getline(cin, value2)
          if(character == '*')
          {  cout << value * value2 << endl;}
          if(character == '/')
          {  cout << value / value2 << endl;}
          if(character == '+')
          {  cout << value + value2;}
          if(character == '-')
          {  cout << value - value2;}
       }
       return 0;
    }
    Last edited by Ganon11; Apr 11 '07, 12:32 AM. Reason: code tags added, indented
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by massdeletion101
    I'm making a calculator that is Supposed to calculate floating point values, but somethign goes wrong along the way (can't be sure what). It calculates intagers just fine, but when I add a decimal with an extra intager on, something funky happens. It infinitly displays a rounded number. For instance, if I type in "3+1.3", instead of displaying "4.3" once like it should, it will just display "4" line by line infinatly over the time span that I don't hit the X at the top right. Can someone perhaps tell me a solution to this? If it will help, I have the code right here:

    #include <iostream>
    using namespace std;
    int main() \\calculator function
    {
    cout << "Press '*' to multiply two numbers (ex: 2*2 plus enter key)\n\n"; \\start of instructions
    cout << "Press '/' to divide two numbers (ex: 2/2 plus enter key)\n\n";
    cout << "Press '+' to add to two numbers (ex: 2+2 plus enter key)\n\n";
    cout << "Press '-' to subtract a number from another(ex: 2-2 plus enter key)\n\n"; \\end of instructions
    cout << "Press 'Q' or 'q' to exit\n\n";
    char character;
    while(character != 'Q')
    {long double value;
    getline(cin, value, '*' || '/' || '+' || '-');
    cin >> character;
    long double value2;
    getline(cin, value2)
    if(character == '*')
    {cout << value * value2 << endl;}
    if(character == '/')
    {cout << value / value2 << endl;}
    if(character == '+')
    {cout << value + value2;}
    if(character == '-')
    {cout << value - value2;}
    }
    return 0;
    }
    I'm not sure but I believe it is because you are adding(or anything else) an integer with a float. The program adds 1 and 3 (4) and then doesn't know what to do with the 0.3. You should probably make both numbers floats. Again, I'm not sure but I think that is the problem.

    Comment

    • massdeletion101
      New Member
      • Mar 2007
      • 23

      #3
      Originally posted by ilikepython
      I'm not sure but I believe it is because you are adding(or anything else) an integer with a float. The program adds 1 and 3 (4) and then doesn't know what to do with the 0.3. You should probably make both numbers floats. Again, I'm not sure but I think that is the problem.
      Actually, I'm adding two variables of the same type. Looking at the code, both values are of type long double. I tried what you said though, despite thinking you were wrong. It still doesn't work right sadly.:( However, there is a different outcome, it just displays nothing. (but that's because I put two numbers with deimal values in them instead of just one with a whole number, I typed 1.5+1.5 and I got nothing)

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by massdeletion101
        Actually, I'm adding two variables of the same type. Looking at the code, both values are of type long double. I tried what you said though, despite thinking you were wrong. It still doesn't work right sadly.:( However, there is a different outcome, it just displays nothing. (but that's because I put two numbers with deimal values in them instead of just one with a whole number, I typed 1.5+1.5 and I got nothing)
        Ok, I studied it and I still can't figure something. I'm really sorry I couldn't help. Maybe somebody else can.

        Comment

        • massdeletion101
          New Member
          • Mar 2007
          • 23

          #5
          Originally posted by ilikepython
          Ok, I studied it and I still can't figure something. I'm really sorry I couldn't help. Maybe somebody else can.
          That's okay. Trying is enough. I might contact someone else about the problem.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Are you using some different getline() function than I am? I didn't know you could use getline to get double values, or even integer values. I would get the entire user input as a string and try to get the numbers and operator for that instead of what you are doing.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              I also notice the line comments are \\ instead of // which meams this code has never compiled.

              Comment

              Working...