# Small Problem with double and integer adding?

• 03-27-2009
Nathan the noob
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); }```
• 03-27-2009
anon
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';     } }```
• 03-27-2009
Nathan the noob
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
• 03-28-2009
anon
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).

• 03-28-2009
CornedBee
Quote:

the order of applying side-effects is undefined
Unspecified. Subtle, but to standard linguists important, distinction.
• 03-28-2009
grumpy
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.