# Thread: help needed with simple code

1. ## help needed with simple code

Hi my code is working except the final part where I test whether the sum of probabilities is equal 1 or not. I am new in this field and I guess that is the reasson I just cannot find any mistake. Th euser needs to enter 3 numbers which sum is one. And if they enetr it wrongly, teh program should let them know.

I would really appreciate any help!!!!!
Code:
```#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

main ()
{

ofstream os;
ifstream is;

os.open("d:\\Branka\\lit review\\programming\\Prep\\intro.txt");
if(os.fail())
{
cout << " Output file opening failed. \n";
exit(1);
}

os.setf(ios::fixed);
os.setf(ios::showpoint);
os.precision(3);

int a;
float num;
float sum=0;
float small=1e-2;

cout <<"\nPlease enter the probability values for RIF1 or Training for recovery within the team/ATC Centre.";
cout <<"\nThere are three possible Levels of influence of this paricular RIF.";
cout <<"\nYou need to enter corresponding probabilities for each Level so that the sum equals one (e.g. 0.33, 0.33, 0.33).";
cout <<"\nIf one Level corresponds precisely to the event under description, simply enter zero to the other boxes (e.g. 0, 1, 0 or 0.5, 0, 0.5).";
cout <<"\nPlease enter three values separarted with the space: ";

for (a=0; a<3; a++)

{
cin >> num;
os <<"The RIF Level "; os  << (a+1); os << " is: "; os << num; os << endl;
sum +=num;
}

if ((sum>=1.00-small)&&(sum<=1.00+small))
{cout << "The input is verified, move on to the next RIF!\n";}
else
{cout << " The sum of your inputs is not 1, but"; cout << sum;
cout << "! Input the probabilities so that their sum equals 1.\n";}

os << "The sum of your inputs is "; os << sum; os << endl;

return 0;
}```

2. I believe your problem is that you cannot use 'small' as a variable name. Try small1 or smalll or something else. The other problem that I see is that after verifying that the sum is bad, it just prints out the sum to the file anyway and ends the program instead of asking to try again...maybe a loop?

3. For starters... This:
Code:
```#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

main ()
{```
Should be this:
Code:
```#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <cassert>

using namespace std;

int main ()
{```
What problems are you having, exactly?

4. Originally Posted by quizkiwi
I believe your problem is that you cannot use 'small' as a variable name. Try small1 or smalll or something else. The other problem that I see is that after verifying that the sum is bad, it just prints out the sum to the file anyway and ends the program instead of asking to try again...maybe a loop?
Hi thanks a lot. i did use small before and it worked without a problem. The loop that you mentioned: sume is always calculated correctly, it is just that my if statement is not capturing wrong sums. And I don't understand why....

5. Originally Posted by Zach L.
For starters... This:
Code:
```#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

main ()
{```
Should be this:
Code:
```#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <cassert>

using namespace std;

int main ()
{```
What problems are you having, exactly?
if teh sum is less than 0.99 teh if statement does not capture it....it prints out the statement as if sum is correct....

6. Well, I cleaned up your code a little, but didn't change any functionality.
Code:
```#include <iostream>
#include <fstream>
#include <cstdlib>

#include <cstdio>
#include <cassert>
*/

using namespace std;

int main ()
{
ofstream os;
ifstream is;

os.open("intro.txt");
if(os.fail())
{
cout << " Output file opening failed. \n";
exit(1);
}

os.setf(ios::fixed);
os.setf(ios::showpoint);
os.precision(3);

float num;
float sum = 0;
float small = 1e-2;

cout << "\nPlease enter the probability values for RIF1 or Training for "
"recovery within the team/ATC Centre.";
cout << "\nThere are three possible Levels of influence of this "
"paricular RIF.";
cout << "\nYou need to enter corresponding probabilities for each Level "
"so that the sum equals one (e.g. 0.33, 0.33, 0.33).";
cout << "\nIf one Level corresponds precisely to the event under "
"description, simply enter zero to the other boxes "
"(e.g. 0, 1, 0 or 0.5, 0, 0.5).";
cout << "\nPlease enter three values separarted with the space: ";

for (int a = 0; a < 3; a++)
{
cin >> num;
os <<"The RIF Level ";
os  << (a+1); os << " is: ";
os << num;
os << endl;
sum +=num;
}

// Organized & nicely formatted code is much easier to sift through.
if((sum >= 1.00 - small) && (sum <= 1.00 + small))
{
cout << "The input is verified, move on to the next RIF!\n";
}
else
{
cout << " The sum of your inputs is not 1, but ";
cout << sum;
cout << "! Input the probabilities so that their sum equals 1.\n";
}

os << "The sum of your inputs is ";
os << sum;
os << endl;

return 0;
}```
It works fine for me. It told me I was okay with (0.4, 0.3, 0.29) = 0.99, but not with (0.4, 0.3, 0.28) = 0.98. Similarly for larger numbers.

What cases are causing you probems?

7. Well I am not sute what you didi buy my code is now working thanks a million!!! However, I will try to figure out what exactly made teh difference since I was getting always teh same mmessage (the input is verified although my sum was <0).