Thread: help needed with simple code

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    5

    Unhappy 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. #2
    Registered User
    Join Date
    Aug 2004
    Posts
    45
    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. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    5
    Quote 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. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    5
    Quote 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. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, I cleaned up your code a little, but didn't change any functionality.
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    
    /* Unused headers...
    #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?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    5
    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 11-23-2005, 08:53 AM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C# Programming
    Replies: 0
    Last Post: 10-14-2002, 01:26 PM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  4. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 11:34 AM