Thread: functions??

  1. #1
    Registered User mik's Avatar
    Join Date
    Oct 2001
    Posts
    15

    Question functions??

    this program works fine untill I tried to include functions to "simplify" it......DOH!

    why wont it work......please try the first program below to see what I would like to happen, then the second, the function version (which wont work) and give me any advice you have.

    FIRST PROGRAM
    /**************************************************

    THIS IS MY PRESSURE CONTROL PROGRAM
    DESIGNED TO ALERT THE USER TO ANY
    PROBLEMS WITH THE PRESSURE READINGS.

    ERASES the existing information in a file,
    then APPENDS the pressure to that file!!!
    ios::trunc == delete information in file // say IF entries>12
    ios::app == open file for appending

    **************************************************/

    #include<iostream.h>
    #include<fstream.h>
    #include<iomanip.h>
    #include<time.h>
    #include<conio.h>
    #define WARNING 1
    #define OK 0

    int index;//acts as counter for pressure array

    float riser1;//used to count the number of consecutively rising readings
    float riser2;//

    int rcount;//riser counter also used
    float amount;//amount above recomended level

    void main()
    {

    float voltage[12];
    float pressure[12];
    int status[12] = {0};

    ofstream outfile;//setting up stream (called outfile)
    outfile.open("C:\\pressure.txt",ios::trunc);//clear existing file BEFORE the loop starts
    outfile<<"TEXT FILE WRITTEN BY name TO DISPLAY PRESSURE READINGS\n\n";
    outfile.close();//close it!!!

    riser1=0;//set riser1 to 0
    riser2=0;//set riser2 to 0
    rcount=0;//set rcount to

    for(index=0;index<12;index++)//setting counter for loop (12 months)
    {//begin for loop1

    cout<<"\nEnter the voltage for reading number: "<<index+1<<" >> ";//prompt for input
    cin>>voltage[index];//input voltage

    pressure[index]=((voltage[index]*20)+500);//convert to pressure

    amount=(pressure[index]-700);//set amount to 700 minus pressure

    if(pressure[index]>=700)//if equal to or greater than print this to screen
    {
    cout<<endl<<endl<<endl<<endl<<endl<<endl<<"\n\..........* ******** WARNING THE PRESSURE IS "<<amount<<" ABOVE RECOMENDED LEVELS!***********"<<endl;
    }

    //target stream (outfile) to a text file (pressure.txt)
    //then open it ready to APPEND data

    outfile.open("C:\\pressure.txt",ios::app);//open to append

    //send data
    if(pressure[index]>=700)//if pressure is >= 700 print asterix's as warning in file
    {
    outfile<<"*** "<<pressure[index]<<" *** THIS IS "<<amount<<" ABOVE MAXIMUM ALLOWED TEMPERATURE"<<endl;
    }//end if

    else//otherwise just print the pressure
    {
    outfile<<pressure[index]<<endl;//remembering to change line
    }//end if

    outfile.close();//close file


    if(pressure[index]>=700)
    {
    status[index]=WARNING;
    }//end if
    else
    {
    status[index]=OK;
    }//end else



    riser2=pressure[index];//set riser2 to pressure,

    if(riser2>riser1)//now if riser2 is greater than riser1
    {
    rcount++;//add one to the counter (keeping count of consecutively rising readings)
    }//end if
    else
    {
    rcount=1;//else give counter value of 1
    }

    if(rcount>=4)//if counter is greater than or equal to 4 (consecutively risen readings)
    {
    cout<<"\n\..........*********** WARNING! THE LAST "<<rcount<<" ENTRIES HAVE RISEN CONSECUTIVELY ************\n";//alert
    cout<<"\n**** TRY RELEASING SOME OF THE PRESSURE FROM THE MAIN TANK USING THE VALVE ****\n";
    //the user to how many consecutive rises there has been
    }//end if


    cout<<endl<<"file written\n\n\n";//inform the user the file has been written


    riser1=riser2;//set riser1 to riser2
    }//end for loop1

    cout<<setw(5)<<"readings"<<setw(12)<<"pressure"<<s etw(18)<<"status"<<endl;//setting column width
    for(index=0;index<12;index++)//begin for loop2, 12 times
    {
    cout<<setw(5)<<index+1<<setw(12)<<pressure[index];
    if(status[index]==OK)
    cout<<setw(20)<<"OK"<<endl;
    else
    cout<<setw(20)<<"WARNING"<<endl;

    }//end for loop2


    time_t hold_time;
    hold_time=time(NULL);
    outfile.open("C:\\pressure.txt",ios::app);//open to append
    outfile<<"\nThis file was modified on: "<<ctime(&hold_time);
    outfile.close();

    }//end program
























    SECOND PROGRAM
    FUNCTION PROGRAM
    /**************************************************

    THIS IS MY PRESSURE CONTROL PROGRAM
    DESIGNED TO ALERT THE USER TO ANY
    PROBLEMS WITH THE PRESSURE READINGS.

    ERASES the existing information in a file,
    then APPENDS the pressure to that file!!!
    ios::trunc == delete information in file // say IF entries>12
    ios::app == open file for appending

    **************************************************/

    #include<iostream.h>
    #include<fstream.h>
    #include<iomanip.h>
    #include<time.h>
    #include<conio.h>
    #define WARNING 1
    #define OK 0


    void print_header();
    void reset_counters();
    void begin_loop();
    void open_file();
    void close_file();
    void send_data();
    void send_data2();
    void check_riser();
    void set_status();
    void print_consecutive();
    void print_warning();
    void print_headings();
    void print_readings();
    void insert_time();
    void written_inform();





    int index;//acts as counter for pressure array

    float riser1;//used to count the number of consecutively rising readings
    float riser2;//

    int rcount;//riser counter also used
    float amount;//amount above recomended level

    float voltage[12];
    float pressure[12];
    int status[12]={0};





    /********************************void main******************************/
    void main()
    {

    print_header();//in file

    reset_counters();

    begin_loop();//for 12

    print_headings();//to screen

    print_readings();//to screen

    insert_time();//to file



    }//end main
    /********************************void main******************************/














    /*************************functions**************** *******************/











    /****** print_header **********/
    void print_header()
    {
    ofstream outfile;//setting up stream (called outfile)
    outfile.open("C:\\pressure.txt",ios::trunc);//clear existing file BEFORE the loop starts
    outfile<<"TEXT FILE WRITTEN BY name TO DISPLAY PRESSURE READINGS\n\n";
    outfile.close();//close it!!!
    }//end header
    /****** print_header **********/


    /***********************reset_counters************* ****/
    void reset_counters()
    {
    riser1=0;//set riser1 to 0
    riser2=0;//set riser2 to 0
    rcount=0;//set rcount to
    }//end reset
    /***********************reset_counters************* ****/


    /**********************************begin_loop****** *****************************************/
    void begin_loop()
    {

    float voltage[12];
    float pressure[12];
    int status[12]={0};

    for(index=0;index<12;index++)//setting counter for loop (12 months)
    {//begin for loop1

    cout<<"\nEnter the voltage for reading number: "<<index+1<<" >> ";//prompt for input
    cin>>voltage[index];//input voltage

    pressure[index]=((voltage[index]*20)+500);//convert to pressure
    cout<<pressure[index];//************************************************te mp display of pressure

    amount=(pressure[index]-700);//set amount to 700 minus pressure

    if(pressure[index]>=700)//if equal to or greater than print this to screen
    {
    /***************print_warning********************** ****/
    print_warning();
    }

    //target stream (outfile) to a text file (pressure.txt)
    //then open it ready to APPEND data

    /*************************open_file**************** *********/
    open_file();


    /***********************send_dat..........**************** ****************/
    if(pressure[index]>=700)
    {//if pressure is >= 700 print asterix's as warning in file
    send_data();
    }//end
    else
    {
    send_data2();
    }//end

    /****************close_file************************ ***********/
    close_file();

    /**********************set_status****************** *************/
    set_status();

    riser2=pressure[index];//set riser2 to pressure,


    /***********************check_riser**************** ***********/
    check_riser();

    if(rcount>=4)//if counter is greater than or equal to 4 (consecutively risen readings)
    {
    /****************************print_consecutive***** *****************************/
    print_consecutive();
    }//end if

    /***************************written_inform********* ***************/
    written_inform();


    riser1=riser2;//set riser1 to riser2

    }//end for loop1

    }//end begin_loop
    /************************************************** ***begin_loop*******************************/
















    /*************************print_headings*********** ****************/
    void print_headings()
    {
    cout<<setw(5)<<"readings"<<setw(12)<<"pressure"<<s etw(18)<<"status"<<endl;//setting column width
    }//end headings
    /*************************print_headings*********** ****************/



    /****************************print_readings******** *****************/
    void print_readings()
    {
    for(index=0;index<12;index++)//begin for loop2, 12 times
    {
    cout<<setw(5)<<index+1<<setw(12)<<pressure[index];
    if (status[index]==OK)
    cout<<setw(20)<<"OK"<<endl;
    else
    cout<<setw(20)<<"WARNING"<<endl;
    }//end readings
    }//end for loop2
    /****************************print_readings******** *****************/



    /*******************insert_time******************** ********/
    void insert_time()
    {
    ofstream outfile;//setting up stream (called outfile)
    time_t hold_time;
    hold_time=time(NULL);
    outfile.open("C:\\pressure.txt",ios::app);//open to append
    outfile<<"\nThis file was modified on: "<<ctime(&hold_time);
    outfile.close();
    }//end time
    /*******************insert_time******************** ********/





    /******main functions********/























    /********loop functions**************/





    /*****************print_warning******************** ***/
    void print_warning()
    {
    cout<<endl<<endl<<endl<<endl<<endl<<endl<<"\n\..........* ******** WARNING THE PRESSURE IS "<<amount<<" ABOVE RECOMENDED LEVELS!***********"<<endl;
    }
    /*****************print_warning******************** ***/


    /*************************open_file**************** *********/
    void open_file()
    {
    ofstream outfile;//setting up stream (called outfile)
    outfile.open("C:\\pressure.txt",ios::app);//open to append
    }//end open_file
    /*************************open_file**************** *********/


    /***********************send_data2***************** *****************/
    void send_data2()
    {
    ofstream outfile;//setting up stream (called outfile)
    outfile.open("C:\\pressure.txt",ios::app);//open to append
    outfile<<"*** "<<pressure[index]<<" *** THIS IS "<<amount<<" ABOVE MAXIMUM ALLOWED TEMPERATURE"<<endl;
    }//end data2

    /***********************send_data2***************** *****************/


    /***********************send_dat..........**************** ****************/
    void send_data()
    {
    ofstream outfile;//setting up stream (called outfile)
    outfile.open("C:\\pressure.txt",ios::app);//open to append
    outfile<<pressure[index]<<endl;//remembering to change line

    }//end data2

    /***********************send_dat..........**************** ****************/


    /****************close_file************************ ***********/
    void close_file()
    {
    ofstream outfile;//setting up stream (called outfile)
    outfile.close();//close file
    }//end close
    /****************close_file************************ ***********/


    /**********************set_status****************** *************/
    void set_status()
    {
    if(pressure[index]>=700)
    {
    status[index]=WARNING;
    }//end if
    else
    {
    status[index]=OK;
    }//end else
    }//end set_status
    /**********************set_status****************** *************/


    /***********************check_riser**************** ***********/
    void check_riser()
    {
    if(riser2>riser1)//now if riser2 is greater than riser1
    {
    rcount++;//add one to the counter (keeping count of consecutively rising readings)
    }//end if
    else
    {
    rcount=1;//else give counter value of 1
    }
    }//end check_riser
    /***********************check_riser**************** ***********/


    /****************************print_consecutive***** *****************************/
    void print_consecutive()
    {
    cout<<"\n\..........*********** WARNING! THE LAST "<<rcount<<" ENTRIES HAVE RISEN CONSECUTIVELY ************\n";//alert
    cout<<"\n**** TRY RELEASING SOME OF THE PRESSURE FROM THE MAIN TANK USING THE VALVE ****\n";
    //the user to how many consecutive rises there has been
    }//end consecutive
    /****************************print_consecutive***** *****************************/


    /***************************written_inform********* ***************/
    void written_inform()
    {
    cout<<endl<<"file written\n\n\n";//inform the user the file has been written
    }//end inform
    /***************************written_inform********* ***************/

  2. #2
    Registered User mik's Avatar
    Join Date
    Oct 2001
    Posts
    15
    I think it has something to do with...

    status[index]..................somewhere it seems to be going wrong!

  3. #3
    Registered User bljonk's Avatar
    Join Date
    Oct 2001
    Posts
    70

    Unhappy

    why u put all u source code in there anyway? just make it simple for us to understand it and give u a help!
    Ünicode¬>world = 10.0£

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM