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...

writing to file: why are you using C FILE and associated functions instead of c++ fstream? If you are going to write a c++ program then don't resort to C functions. It'll work but not very good practice or coding style.

You need to pay attention to the warnings that you compiler produces. Treat each warning as if it were an error. For example the following warnings are actually errors:

line 58: using == instead of = assignment operator

line 68: variable index is unitialized.

writing to file: why are you using C FILE and associated functions instead of c++ fstream? If you are going to write a c++ program then don't resort to C functions. It'll work but not very good practice or coding style.

Because my prof said so :p This is my first C++ programming course, I have never done anything in C and this is basically how my prof showed us to open / write to files.

>>Because my prof said so :
Ok, so do as he said.

>>I have never done anything in C
Yes you have, but you may not realize it. FILE and associated functions are C. Those C functions just happened to be also supported in C++. But that neither here nor there because your prof said to use it, so end of subject.

Your efforts should be to correct all warnings before complaining that your programs don't work as intended.

ok sorry I missed that one...
Anyway I fixed it and it still does the same thing.

line 27 of the write program: using unitialized arrays: This will fix it. float tempStore[maxTempVals] = {0};

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.