Thread: Small Problem with double and integer adding?

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    114

    Small Problem with double and integer adding?

    The Problem here is that when i write a double variable then a int variable i get no out put and are stuck waiting

    Code:
    #include<iostream>
    #include<windows.h>
    #include<conio.h>
    using namespace std;
    double input(double, double);
    int input(int, int);
    double input(double, int);
    //OVERLOADING ADDITION//
    int main()
    {
       cout.setf(ios::showpoint);
       cout.setf(ios::showpos);
       cout.setf(ios::fixed);
       cout.precision(2);
       int x = 0;
       int y = 0;
       int n = 0;
       int run = 1;
      
      double dnumber;
      int inumber;
      
      double dnumber2;
      int inumber2;
      char c;
      double danswer =0;
      int ianswer =0;
    while(run == 1)
    {
     while(n < 2)
     {
        cout <<"Do you want the number to be decimal placed or whole number <D> <W>?:";
       for(;;)
       {
          if(n == 2)
          {
            system("cls");
            break;
          }
       c=toupper(getch());
       
       if(c == 'D')
       {
           if(x == 1)
           {
                system("cls");
                cout <<"Input a number:";
                cin >> dnumber2;
                n = 2;
                x = 2;
                c = ' ';        
           }
           if(x == 0)
           {
                system("cls");
                cout <<"Input a number:";
                cin >> dnumber;
                n = n++;
                x = 1;
                c = ' ';
                system("cls");
                if(y == 0)
                {
                      cout <<"Do you want the number to be decimal placed or whole number <D> <W>?:";
                }
           }
       }
       else if(c == 'W')
       {
           if(y == 1)
           {
                system("cls");
                cout <<"Input a number:";
                cin >> inumber2;
                n = 2;
                y = 2;
                c = ' ';
                
           }
           if(y == 0)
           {
                system("cls");
                cout <<"Input a number:";
                 cin >> inumber;
                 n = n++;
                 y = 1;
                 c = ' ';
                 system("cls");
                 if(x == 0)
                {
                      cout <<"Do you want the number to be decimal placed or whole number <D> <W>?:";
                }
                 
           }
       }
       }//for loop
     } // While n < 2
      if(x == 1 && y == 1)
      {
         danswer = input(dnumber,inumber);
         cout << danswer << endl;
         system("PAUSE");
         x = 0; n = 0; y = 0; dnumber = 0; inumber = 0; ianswer = 0; inumber2 = 0; dnumber2 = 0; danswer = 0; c = ' ';  
      }
      else if(x == 0 && y ==2)
      {
         ianswer = input(inumber,inumber2);
         cout << ianswer << endl;
         system("PAUSE");
         x = 0; n = 0; y = 0; dnumber = 0; inumber = 0; ianswer = 0; inumber2 = 0; dnumber2 = 0; danswer = 0; c = ' ';  
      }
      else if( x == 2 && y == 0)
      {
         danswer = input(dnumber,dnumber2);
         cout << danswer << endl;
         system("PAUSE");
         x = 0; n = 0; y = 0; dnumber = 0; inumber = 0; ianswer = 0; inumber2 = 0; dnumber2 = 0; danswer = 0;  c = ' ';  
      }
      system("cls");
      cout << "Do you wish to run this program again? <N> <Y>:";
    for(;;)
    {
      c = toupper(getch());
      if(c == 'N')
      {
           run = 0;
           break;
      }
      else if(c == 'Y')
      { 
           run = 1;
           break;
      }
    }
      system("cls");       
    } // While Run
    return 0;       
    } // End main
    
    double input(double num1, double num2)
    {
        double num3 = num1 + num2;
        return(num3);
    }
    double input(double num1, int num2)
    {
        double num4 = (double) num2; //Even with out type casting same problem
        double num3 = num4 + num1;
        return(num3);
    }
    int input(int num1, int num2)
    {
        return(num1 + num2);
    }
    Last edited by Nathan the noob; 03-27-2009 at 05:34 PM.
    Who needs a signature?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    My guess is that it is because of lines like that:

    Code:
    n = n++;
    This is undefined behaviour (n may or may not be incremented).

    I guess arrays might still help...

    Code:
    #include <iostream>
    #include <cctype>
    using namespace std;
    
    int main()
    {
        bool use_double[2];
        double doubles[2];
        int ints[2];
    
        for (int i = 0; i != 2; ++i) {
            cout <<"Do you want the number to be decimal placed or whole number <D> <W>?:";
            char c;
            do {
                c = toupper(cin.get());
                if (c == 'W') {
                    cout <<"Input a number:";
                    std::cin >> ints[i];
                    use_double[i] = false;
                }
                else if (c == 'D') {
                    cout <<"Input a number:";
                    std::cin >> doubles[i];
                    use_double[i] = true;
                }
            }while (c != 'W' && c != 'D');
        }
    
        cout.setf(ios::showpoint);
        cout.setf(ios::showpos);
        cout.setf(ios::fixed);
        cout.precision(2);
    
        if (!use_double[0] && !use_double[1]) {
            cout << ints[0] + ints[1] << '\n';
        }
        else {
            cout << (use_double[0] ? doubles[0] : ints[0]) + (use_double[1] ? doubles[1] : ints[1]) << '\n';
        }
    }
    Last edited by anon; 03-27-2009 at 06:44 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    You cut the code down alot :P well acauly the n++ thing u pointed out some how made a difference when i changed it to ++n

    Well thank you. The reason i used all the functions cause i was testing out over loading
    Who needs a signature?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    To increment a variable you do
    Code:
    ++n;
    
    //or
    
    n++;
    These operators work by side-effect and you can't modify (assignment) the same variable again in the same statement (the order of applying side-effects is undefined).

    To test out overloading you might use simpler programs.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    the order of applying side-effects is undefined
    Unspecified. Subtle, but to standard linguists important, distinction.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It has nothing to do with side effects in this case. Modifying a variable twice between two consecutive sequence points (as in "n = n++;") yields undefined behaviour. End of story.

    Quote Originally Posted by C++ Standard, Section 5 para 4
    Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified. Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined.
    Apart from that, the flow control in the original code is setting a number of variables, and relying on particular sets of values. Lots of opportunities for the programmer to expect something to be true, but have forgotten to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  3. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM