Heres the problem:
A scientist has been conducting experiments and recording results of those experiments. Two pieces of data per experiment have been recorded: temperature in degrees Celsius and a concentration ratio (in the range – 0.5 to 2.0). Validate user inputs.
heres what I have:
Part1 - Writing to file
*****************************************************************
#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
using namespace std;
struct XPR
{
float temp;
float conc;
};
XPR xprmntRec;
int recWritten;
FILE * XPRMNT;
char GetAns();
int main(void)
{
const int maxTempVals = 20;
char usrAns;
float tempIn;
float concIn;
float tempStore[maxTempVals];
int tempIndex;
float tempVals;
int index;
cout << "This program will gather data about temperatures " << endl;
cout << "as measured in degrees celcius." << endl << endl;
cout << "If you choose to continue, any data saved to this file" << endl;
cout << "currently will be lost." << endl << endl;
cout << "Do you want to continue? Y/N" << endl;
usrAns = GetAns();
if ( usrAns == 'y' || usrAns == 'Y' )
{
XPRMNT = fopen("k:\\xprmnt.dat" , "wb");
if (XPRMNT == NULL)
{
cout << "ERROR" << endl; //Output file failed...
cout << "TERMINATING" << endl;
}
else
{
//File opened ok
}
cout << "Do you have data to enter? Y/N " << endl;
usrAns == GetAns();
while(usrAns == 'y' || usrAns == 'Y')
{
cout << "Enter the temperature." << endl;
cin >> tempIn;
tempStore[index] = tempStore[index] + tempIn;
cout << "Enter temperature concentration ratio." << endl;
cout << "Values must range from -0.5 to 2.0 degrees" << endl;
cin >> concIn;
while ( concIn < -.5 || concIn > 2.0 )
{
cout << "Values must range from -0.5 to 2.0 degrees" << endl;
cout << "Re-enter the concentration ratio." << endl;
cin >> concIn;
}
//Build temperature record
xprmntRec.temp = tempStore[index];
xprmntRec.conc = concIn;
recWritten = fwrite (&xprmntRec,sizeof(xprmntRec),1,XPRMNT);
cout << "Do you have data to enter? Y/N " << endl;
usrAns = GetAns();
}
fclose(XPRMNT);
}
return 0;
}
char GetAns()
{
char usrAns;
cin >> usrAns;
while (usrAns != 'n' && usrAns != 'N' && usrAns != 'y' && usrAns != 'Y' )
{
cout << "You can only enter Y(yes) or N(no)" << endl;
cout << "Do you want to continue?" << endl;
cin >> usrAns;
}
return usrAns;
}
Part 2 - Displaying file
#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
using namespace std;
struct XPR
{
float temp;
float conc;
};
XPR xprmntRec; // Record name
int recRead;
FILE * XPRMNT; // Pointer variable
int main(void)
{
char exitOnInput;
XPRMNT = fopen("k:\\xprmnt.dat" , "rb");
if (XPRMNT == NULL)
{
cout << "ERROR" << endl; //Output file failed...
cout << "TERMINATING" << endl;
}
else
{
recRead = fread (&xprmntRec,sizeof(xprmntRec),1,XPRMNT);
}
cout << "Temperature Concentreation Ratio"<< endl << endl;
while ( recRead != 0) // If reCread = 0 End of file
{
cout << " " << xprmntRec.temp << " " << xprmntRec.conc << endl;
}
//File opened ok
fclose(XPRMNT);
cout << "Enter a letter followed by 'ENTER' to exit" << endl;
cin >> exitOnInput;
return 0;
}
It displays a strange garbage value like 10*11111118956 + 33 over and over til i terminate...