Hi. I am trying to write a program to make data entry at work quicker. I have only taken an intro course in C++, and that was a year ago, so I am a novice and having trouble recalling everything I learned.
Anyways, The program asks the user to input concentrations of compounds at certain temperatures and then averages the concentrations and organizes them into a matrix. Then the matrix is sent to an output file.
I am using dev-C++ 4.9.9.2 on Windows Vista. The program compiles correctly, but I am running into a few problems when trying to run it.
First, it runs on one computer, but not another. Both computers use Windows Vista as an operating system.
Second, on the computer that does run it, the program crashes once it gets to the point where it asks the user to input concentrations for C3H6 at whatever temperature is 50 degrees above the minimum temperature (by the way, you can only input temperatures that are multiples of 50 i.e. 50, 100, 150, 200, etc.). This problem arose once I added the TempCheck function to the program. Before that, I was able to run the program to completion (on the computer that ran it). However, the resulting matrix in the output file did not turn out right. The first element in every row after the first was the same as the last element in the preceding row.
Lastly, I realize that if the user types 'd' without typing any concentrations, then the program will be dividing by zero when trying to find an average. I have been trying to find a way to make the matrix a character array so that when the user types 'd' without typing any concentrations, the element is just a hyphon. However, I can't see how this is possible to do, since then I'd have to make the average concentrations strings instead of doubles, and as far as I know strings cannot be considered as a single element in an array. Is there a way around this?
Sorry for the long explanation. I appreciate any help that anyone has to offer. Here is the source code, along with comments:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
/*This program finds averages for concentrations of compounds at certain
temperatures, and then organizes the data in a matrix that is sent to an output
file.*/
#include<iostream> //Allows for the use of cout/cin commands.
#include<fstream>//Enables one to load data from files and output data to files.
#include<cstdlib> /*Needed for checking that the file was loaded correctly
and also to convert strings to integers or doubles.*/
#include<cstring> //Needed to find string lengths.
#include<cctype> /*Allows for the use of isdigit function to check that an
array element is a digit*/
using namespace std; //Allows for basic C++ functions.
int i,check1=0; //Declares global variables as integers.
int main() //Begins main function.
{
int MaxTemp,MinTemp,NROWS,NCOLS=9,j,x; //Declares variables as integers.
char OutputFileName[100]; //Declares variable as a string.
double A[NROWS][8]; //Declares an array with elements as doubles.
int TempCheck(char extreme[]); /*Declares an integer function with a string
as the only input.*/
double data(char compound[]); /*Declares a double function with a string as
the only input.*/
ofstream outfile; //Declares variable.
cout<<"Enter output file name (use a .csv extension): ";
cin>>OutputFileName;
outfile.open(OutputFileName); //Creates output file.
MaxTemp=TempCheck("maximum");//Calls function and assigns output to MaxTemp.
NROWS=MaxTemp/50-1;
MinTemp=TempCheck("minimum");//Calls function and assigns output to MinTemp.
for(i=MaxTemp;i>=MinTemp;i=i-50) /*For every 50 degree increment of the
temperature starting at MaxTemp and ending at MinTemp...*/
{
x=(i-MinTemp)/50; /*Needed so that for each turn of the loop, the row
number decreases by 1, and eventually reaches 0.*/
A[x][0]=data("CH4"); /*Calls function and assigns output to the
appropriate element in an array.*/
A[x][1]=data("CO");
A[x][2]=data("NO");
A[x][3]=data("NO2");
A[x][4]=data("C2H6");
A[x][5]=data("C3H6");
A[x][6]=data("C3H8");
A[x][7]=data("HCHO");
A[x][8]=data("CH3OH");
}
for(i=0;i<NROWS;i++) //For each successive row...
{
for(j=0;j<NCOLS;j++) //For each successive column in a given row...
outfile<<A[i][j]<<","; /*Send the element in that position to
the output file.*/
outfile<<endl;
}
cout<<endl;
outfile.close(); //Close output file.
system("pause"); //Pause console window.
return 0;
} //Ends main function.
int TempCheck(char extreme[]) /*This function checks to see that the
temperature input consists only of digits, and if so, returns the temperature as
an integer.*/
{
char Temp[100]; //Declares variable as a string.
int y; //Declares variable as an integer.
cout<<"\nEnter "<<extreme<<" temperature: ";
while(1==1) //Always true, therefore infinite loop.
{
cin>>Temp; //Take in whatever user may type.
for(i=0;i<strlen(Temp);i++) //For each character in the string...
{
if(isdigit(Temp[i])) //If the character is a digit...
check1=check1+1;
}
if(check1==strlen(Temp)) //If every character is a digit...
{
y=atoi(Temp); //Convert the string to an integer and assign to y.
check1=0; //Reset value to zero.
return y; //Return temperature as an integer.
break; //End the loop.
}
else //Otherwise...
cout<<"\n Invalid Entry.\n\nEnter "<<extreme<<" temperature: ";
check1=0; //Reset value to zero.
}
}
double data(char compound[]) /*This function asks the user to input either
concentrations of compounds or type 'd' to finish, averages the
concentrations once the user is finished, and returns the average.*/
{
char concentration[100]; //Declares variable as a string.
int k,ndatapoints=0,check2=0; //Declares variables as integers.
double sum=0,z,average; //Declares variables as doubles.
cout<<"\nEnter concentrations for "<<compound<<" at "<<i<<" \370C one by one. Press 'd' when done:\n\n ";
while(1==1) //Always true, therefore infinite loop.
{
cin>>concentration; //Take in whatever the user may type.
cout<<" ";
if(strlen(concentration)==1&concentration[0]=='d') /*If the user
types 'd' and only 'd'...*/
break; //End the loop.
for(k=0;k<strlen(concentration);k++) /*For each character in the
string...*/
{
if(isdigit(concentration[k])) //If the character is a digit...
check1=check1+1;
if(concentration[k]=='.') //If the character is a decimal point...
check2=check2+1;
}
if((check1+check2)==strlen(concentration)&check2<=1) /*If the string
consists of digits and either one or no decimal points...*/
{
z=atof(concentration); /*Convert the string to a double and
assign to z.*/
sum=sum+z; //Add the concentration to the total sum.
ndatapoints=ndatapoints+1; /*Increase the number of
concentrations by one.*/
check1=0; //Reset value to zero.
check2=0; //Reset value to zero.
}
else //Otherwise...
{
cout<<"\n Invalid Entry.\n\n ";
check1=0; //Resets value to zero.
check2=0; //Resets value to zero.
}
}
average=sum/ndatapoints; //Find the average concentration.
return average; //Return the average as a double.
}
|