Thread: characters into array??

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

    Question characters into array??

    how can I includ the words k and warning int my index?

    look...

    if(pressure[index]>=699)
    {
    status[index]=ok;
    }//end if
    else
    {
    status[index]=warning;
    }//end else

    it doesnt recognise characters, but if I say

    if(pressure[index]>=699)
    {
    status[index]=1;
    }//end if
    else
    {
    status[index]=2;
    }//end else



    it works great
    C:\
    C:\dos
    C:\dos run
    RUN dos RUN!

  2. #2
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    This is obvious, because you haven't defined warning or ok.

    #define WARNING 1
    #define OK 0

    Now this will work

    if(pressure[index]>=699)
    {
    status[index]=OK;
    }//end if
    else
    {
    status[index]=WARNING;
    }//end else
    Making error is human, but for messing things thoroughly it takes a computer

  3. #3
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    Oh btw, this means that OK and WARNING correspond to integer variables 0 and 1. So if you write:

    cout << pressure[index];

    ...you'll still get an integer value, not a string. You can however define strings corresponding to the numerical values:

    const char* const PressureCodes[] = {"ok", "warning"};

    Now PressureCodes[0] points to "ok" and PressureCodes[1] points to "warning". So you can print the string:

    cout << PressureCodes[pressure[index]];

    Hope this cleared it out
    Making error is human, but for messing things thoroughly it takes a computer

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

    Question

    still doesn't work

    please try it


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

    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
    char ok;
    char warning;

    void main()
    {
    float voltage[12];
    float pressure[12];
    //float status[2];


    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 MICHAEL MCGUIRE 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 loop1

    cout<<setw(5)<<"readings"<<setw(12)<<"pressure"<<s etw(18)<<"status"<<endl;//setting column width
    for(index=0;index<12;index++)//12 times
    {
    cout<<setw(5)<<index+1<<setw(12)<<pressure[index]<<setw(20)<<endl;//<<status[index]<<endl;//print the readings and pressures
    }//end for










    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
    C:\
    C:\dos
    C:\dos run
    RUN dos RUN!

  5. #5
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    > #define WARNING 1;
    > #define OK 0;

    Remove the semicolons ( from end of lines. Precompiler lines don't need them (lines starting with #)
    Making error is human, but for messing things thoroughly it takes a computer

  6. #6
    Registered User mik's Avatar
    Join Date
    Oct 2001
    Posts
    15
    heavy sobbing

    2 errors
    cannot convert const char const to float on these lines

    status[index]=pressurecodes[1];
    status[index]=pressurecodes[0];

    /************************************************** ******************
    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
    const char* const pressurecodes[2]={"ok", "warning"};

    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
    char ok;
    char warning;

    void main()
    {
    float voltage[12];
    float pressure[12];
    float status[2];


    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 MICHAEL MCGUIRE 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]=pressurecodes[1];
    }//end if
    else
    {
    status[index]=pressurecodes[0];
    }//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 loop1

    cout<<setw(5)<<"readings"<<setw(12)<<"pressure"<<s etw(18)<<"status"<<endl;//setting column width
    for(index=0;index<12;index++)//12 times
    {
    cout<<setw(5)<<index+1<<setw(12)<<pressure[index]<<setw(20)<<status[index]<<endl;//print the readings and pressures
    }//end for










    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
    C:\
    C:\dos
    C:\dos run
    RUN dos RUN!

  7. #7
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Don't forget to change
    Code:
    //float status[2];
    to
    Code:
    int status[12] = {0};
    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.

  8. #8
    Registered User mik's Avatar
    Join Date
    Oct 2001
    Posts
    15
    thanks but it doesn't fix the errors

    still sobbing!

    looking for the razor blades.....
    C:\
    C:\dos
    C:\dos run
    RUN dos RUN!

  9. #9
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    OK, now your getting confusing. You shouldn't need
    Code:
    const char* const pressurecodes[2]={"ok", "warning"}; 
    // or
    char ok; 
    char warning;
    Just stick with the #define macros.

    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.

  10. #10
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    If you want to output "ok" or "warning", just use a conditional check based on your status to cout the appropriate string.
    Code:
    // somewhere in the loop
    if (status[index] == OK)
        cout << "OK";
    else
        cout << "WARNING";
    Or something similar.

    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.

  11. #11
    Registered User mik's Avatar
    Join Date
    Oct 2001
    Posts
    15
    you missunderstand the function of the program...
    here, copy this then run to see the required layout....

    i.e. the printout at the end of the program is designed to show quickly any errors...thanks.


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

    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
    //const char* const pressurecodes[2]={"ok", "warning"};

    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
    char ok;
    char warning;

    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 MICHAEL MCGUIRE 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]=pressurecodes[1];
    }//end if
    else
    {
    status[index]=pressurecodes[0];
    }//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 loop1

    cout<<setw(5)<<"readings"<<setw(12)<<"pressure"<<s etw(18)<<"status"<<endl;//setting column width
    for(index=0;index<12;index++)//12 times
    {
    cout<<setw(5)<<index+1<<setw(12)<<pressure[index]<<setw(20)<<endl;//<<status[index]<<endl;//print the readings and pressures
    }//end for










    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
    C:\
    C:\dos
    C:\dos run
    RUN dos RUN!

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

    msc++ being used, don't know if it makes a difference?
    C:\
    C:\dos
    C:\dos run
    RUN dos RUN!

  13. #13
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Using MS Visual C++ 6.0 after making the changes suggested it seems to work fine for me.
    Code:
    /**************************************************
    ****************** 
    
    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 MICHAEL MCGUIRE 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 loop1 
    
    cout<<setw(5)<<"readings"<<setw(12)<<"pressure"<<setw(18)<<"status"<<endl;//setting column width 
    for(index=0;index<12;index++)//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 
    
    
    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
    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.

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

    yadancer!

    it works this end tae.

    somebuddy once said....

    no, try not.
    Do or do not
    there is no try.

    you did.....cheers.
    C:\
    C:\dos
    C:\dos run
    RUN dos RUN!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. erase all characters in a character array?
    By diddy02 in forum C Programming
    Replies: 4
    Last Post: 11-20-2008, 05:30 PM
  2. storing a pattern of characters into a 2D array
    By haroonie in forum C Programming
    Replies: 2
    Last Post: 04-20-2005, 05:19 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Can a string be converted to an array of characters?
    By supaben34 in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2002, 12:18 PM
  5. Characters in an Array
    By Xenmordoc in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2002, 11:19 PM