-
characters into array??
how can I includ the words :ok 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
-
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
-
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
-
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
-
> #define WARNING 1;
> #define OK 0;
Remove the semicolons (;) from end of lines. Precompiler lines don't need them (lines starting with #)
-
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
-
Don't forget to change
to
Code:
int status[12] = {0};
David
-
thanks but it doesn't fix the errors
still sobbing!
looking for the razor blades.....
-
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
-
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
-
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
-
p.s.
msc++ being used, don't know if it makes a difference?
-
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
-
yadancer!
it works this end tae.
somebuddy once said....
no, try not.
Do or do not
there is no try.
you did.....cheers.