Thread: almost got it (but not quite)

  1. #1
    mik
    Guest

    Exclamation almost got it (but not quite)

    this program prompts the user for a voltage reading then converts it to pressure. it also accepts default value (user pressing return).

    what im "trying" to do now is print to the screen this:

    reading pressure
    1 x//x is value of pressure
    2 x//changing for every input
    3 x
    4 x
    etc......

    what i get is this
    reading pressure
    1 0//all the pressure values are identical
    2 0//they are all the last entered
    3 0
    etc.......

    can i assign the user input to an array and how?
    or can i do it by writing to a file (its already doing this) but read the required lines int the display array at the end of program

    thank for trying to help. here's the source...


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

    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

    contains a default (press return) process!!!

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

    #include<iostream.h>
    #include<fstream.h>
    #include<iomanip.h>
    #include<stdlib.h>
    #include<string.h>

    float voltage;//assign memory space
    float pressure;//
    float riser1;//used to record whether the temp rises consecutively
    float riser2;//
    int count;//used to count entries before "ios::trunc" (clearing file)
    int rcount;//riser counter
    float parray[12];//pressure array
    float rarray[12];//reading array
    int index;//acts as counter for pressure array
    int index2;//counter for reading array


    void main()
    {
    char rnumber[12];
    char reading[12];
    char buff[100];


    ofstream outfile;//setting up stream (called outfile)
    outfile.open("C:\\pressure.txt",ios::trunc);/*clear existing file BEFORE the loop starts*/
    outfile.close();//close it!!!

    ofstream preading;//set up stream (preading)
    outfile.open("C:\\preading.txt",ios::trunc);/*clear existing file*/
    outfile.close();//close it!!!

    rcount=0;//
    riser1=0;//re-setting
    riser2=0;//


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


    cout<<"Enter the voltage reading: <"<<voltage<<"> ";//prompt for input

    /*this allows the user to accept the default (last voltage reading entered)*/
    //simply by pressing RETURN!!
    cin.getline(buff,100);
    if (strlen(buff)==0)//if nothing is entered (return pressed)
    {
    voltage=voltage;//voltage is not changed
    }//end if
    else
    {
    voltage=atof(buff);//else voltage equals user input
    }//end else

    //convert voltage to pressure

    pressure=((voltage*20)+500);

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

    //write this to a file

    outfile.open("C:\\preading.txt",ios::app);/*open to append*/
    outfile<<pressure<<endl;//remembering to change line
    outfile.close();//close it!!!


    //check pressure readings

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

    riser2=pressure;//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 reset counter to one*/
    }

    if(rcount>=4)/*if counter is egreater than or equal to 4 (consecutively risen readings)*/
    {

    cout<<"\n\..........*********** WARNING! THE LAST "<<rcount<<" ENTRIES HAVE RISEN CONSECUTIVELY ************\n";//alert
    /*the user to how many consecutive rises there has been*/

    }//end if


    /******************** store reading ************************/


    //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>=700)/*if pressure is >= 700 print asterix's as warning in file*/
    {
    outfile<<"***"<<pressure<<"***"<<endl;
    }//end if

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

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

    outfile.close();//close file


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


    riser1=riser2;/*set riser1 equal to riser2 (helps keep track of the consecutive readings)*/


    cout<<"temp print out of pressure "<<pressure<<endl<<endl;/*print the pressure reading to the screen*/

    }//end for loop1

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


    cout<<setiosflags(ios::left)<<setw(12)<<"READING"< <setw(12)<<"PRESSURE"<<endl<<endl;

    for(index=0;index<12;index++)
    {//begin loop2
    cout<<setw(12)<<index+1<<setw(18)<<index2<<endl;
    }//end loop
    cout<<endl;

    }//end main

  2. #2
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Code:
    for(index=0;index<12;index++) 
    {//begin loop2 
    cout<<setw(12)<<index+1<<setw(18)<<index2<<endl; 
    }//end loop
    index2 is never set to any value. You are always going to get a junk value here.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

Popular pages Recent additions subscribe to a feed