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!!!!!
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <assert.h>

    Code:
    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
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    All replies here - http://cboard.cprogramming.com/showthread.php?t=68749

    Duplicate thread

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help needed with simple code
    By Branka in forum C++ Programming
    Replies: 6
    Last Post: 08-18-2005, 03:00 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