10 Replies - 807 Views - Last Post: 02 April 2010 - 08:09 PM Rate Topic: -----

#1 dinisrainman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 10-February 08

Reading from file.Can someone help please!

Posted 02 April 2010 - 06:49 PM

Hi everyone.
I'm pretending to read a file and output. I have a [a11...a33] matrix and the output i'm expecting is not the one the compiler is giving me. what im i doing wrong?
This is the expected output:

a = 3, b = -2, c = -1
The value of x1 is: 1
The value of x2 is: -0.333333
1 of the x values is/are positive

This what I have worked on so far:

include <iostream>
#include <fstream>
#include <cmath>


double num_positive ( double, double);
void QuadraticEquation ( double , double , double, double&, double& );

using namespace std;


int main()
{
	ifstream infile;

	double y2;
	double temp1=0;
	double temp2=0;
	double res1, res2;
	double a=0 , b=0 , c=0 ;
	int t1=0 , t2=0 , t3=0 ;

	infile.open("data1.txt");
	if (!infile)
	 
			cout << "Error: file could not be opened" << endl; 
      	else
			{
			while (!infile.eof()) 
			{
				infile >> a, b, c;
				
			  infile >> a, b, c;
			  cout << "a = " << a << " b = " << b << " c = " << c;
			}
				QuadraticEquation( t1, t2,t3, res1, res2);
				y2 = num_positive( temp1,temp2);
			 
			}
				cout << "(The value of x1 is:)"  << y2 << endl;
				
      	infile.close();
	
	return 0;
}

void QuadraticEquation( double a , double b , double c, double &x1, double &x2 )
{
	
	double r1, r2;

		if ( ((b*B)/> - 4 * a * c )> 0 )
		{
			r1= (-b+sqrt((b*B)/> - 4 * a * c ))/(2*a);
			r2= (-b-sqrt((b*B)/> - 4 * a * c ))/(2*a);
		}
		else if (((b*B)/> - 4 * a * c ) == 0)
		{
			r1=(-B)/>/(2*a);
			r2=(-B)/>/(2*a);
		}
		else
			cout << "The number is generated are not real." << endl;
	
}

double num_positive ( double q, double p )
{
	    double a1=0;
		double b1=0;
		if (  (a1 || b1) > 0 )
		{
	      	 cout << "There one positive value";
		}
		exit (1);
}






This post has been edited by JackOfAllTrades: 03 April 2010 - 06:00 AM
Reason for edit:: Added code tags. PLEASE!!!! [code]...PUT YOUR CODE IN HERE...[/code]


Is This A Good Question/Topic? 0
  • +

Replies To: Reading from file.Can someone help please!

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1091
  • View blog
  • Posts: 4,571
  • Joined: 09-June 09

Re: Reading from file.Can someone help please!

Posted 02 April 2010 - 06:52 PM

its probably because you got smiley s in your code lol :) but put your code in code tags so its easier for us to read
Was This Post Helpful? 0
  • +
  • -

#3 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Reading from file.Can someone help please!

Posted 02 April 2010 - 06:56 PM

double num_positive ( double q, double p )
{
double a1=0;
double b1=0;
if ( (a1 || b1) > 0 )
{
cout << "There one positive value";
}
exit (1);
}


You take two parameters, ignore them both, and then exit with a non zero status?
Also, here: if ( (a1 || b1) > 0 ). I think you mean: if ( q > 0 || p > 0 )

void QuadraticEquation( double a , double b , double c, double &x1, double &x2 )

You never use the variables x1 and x2, you instead use r1 and r2, which are local to the function
Was This Post Helpful? 0
  • +
  • -

#4 dinisrainman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 10-February 08

Re: Reading from file.Can someone help please!

Posted 02 April 2010 - 07:03 PM

Yes I realized thanks im a newbbie. the reason I didnt reutilize x1 x2 because the compiler gives me a syntax error. also can call quadraticEquation function for some reason. How do I print x1 and x2 values?
Was This Post Helpful? 0
  • +
  • -

#5 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Reading from file.Can someone help please!

Posted 02 April 2010 - 07:10 PM

I am not so sure if r1 and r2 represents x1 and x2, but first you should pass the values of r1 and r2 into x1 and x2. (inside the QuadraticEquation function, after getting values into r1 and r2).
if yo want to print their values inside that function, simply add this code:
if ( ((b* b ) - 4 * a * c )> 0 )
{
r1= (-b+sqrt((b*b ) - 4 * a * c ))/(2*a);
r2= (-b-sqrt((b*b ) - 4 * a * c ))/(2*a);
x1 = r1;
x2 = r2;//assigning x1 and x2 with values
cout<<x1<<" "<<x2;//printing
}

as for:

Quote

the compiler gives me a syntax error

what error?

This post has been edited by japanir: 02 April 2010 - 07:10 PM

Was This Post Helpful? 0
  • +
  • -

#6 dinisrainman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 10-February 08

Re: Reading from file.Can someone help please!

Posted 02 April 2010 - 07:20 PM

I mad the changes you suggested but still can't make it work.
r1 and r2 are the locals variables to find the roots.

this is the output I got
a = 1 b = 0 c = 0a = 3 b = 0 c = 0a = 2 b = 0 c = 0a = 1 b = 0 c = 0a = 6 b = 0
c = 0a = 9 b = 0 c = 0a = 3 b = 0 c = 0a = -2 b = 0 c = 0a = -1 b = 0 c = 0a = 1
 b = 0 c = 0a = -1 b = 0 c = 0a = 0 b = 0 c = 0a = 1 b = 0 c = 0a = 0 b = 0 c =
0a = -4 b = 0 c = 0a = 2 b = 0 c = 0a = 2 b = 0 c = 0a = -2 b = 0 c = 0a = -2 b
= 0 c = 0-9.25596e+061-9.25596e+061
There one positive valuePress any key to continue . . .

instead of this :

a = 3, b = -2, c = -1
The value of x1 is: 1
The value of x2 is: -0.333333
1 of the x values is/are positive


This post has been edited by JackOfAllTrades: 03 April 2010 - 06:01 AM
Reason for edit:: Added code tags. PLEASE!!!! [code]...PUT YOUR CODE IN HERE...[/code]

Was This Post Helpful? 0
  • +
  • -

#7 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Reading from file.Can someone help please!

Posted 02 April 2010 - 07:23 PM

What I suggested did not fix every error, just some things I noticed. Please post update code here using the code tags :code:
Was This Post Helpful? 0
  • +
  • -

#8 dinisrainman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 10-February 08

Re: Reading from file.Can someone help please!

Posted 02 April 2010 - 07:28 PM

some improvements but still got this output:
a = 1 b = 0 c = 0-9.25596e+061-9.25596e+061
There one positive valuePress any key to continue . . . Please where do I improve?


#include <iostream>
#include <fstream>
#include <cmath>


double num_positive ( double, double);
void QuadraticEquation ( double , double , double, double&, double& );

using namespace std;


int main()
{
	ifstream infile;

	double y2;
	double temp1=0;
	double temp2=0;
	double res1, res2;
	double a=0 , b=0 , c=0 ;
	int t1=0 , t2=0 , t3=0 ;

	infile.open("project4.txt");
	if (!infile)
	 
			cout << "Error: file could not be opened" << endl; 
      	else
			{
			while (!infile.eof()) 
			{
							
				infile >> a, b, c;
				cout << "a = " << a << " b = " << b << " c = " << c;
			
				QuadraticEquation( t1, t2,t3, res1, res2);
				y2 = num_positive( temp1, temp2);
			}
			
		}
				cout << "(The value of x1 is:)"  << y2 << endl;
				
      	infile.close();
	
	return 0;
}

void QuadraticEquation( double a , double b , double c, double &x1, double &x2 )
{
	
	double r1, r2;

		if ( ((b*B)/> - 4 * a * c )> 0 )
		{
			r1= (-b+sqrt((b*B)/> - 4 * a * c ))/(2*a);
			r2= (-b-sqrt((b*B)/> - 4 * a * c ))/(2*a);
			x1= r1;
			x2 = r2;
			cout << x1 << x2 << endl;
		}
		else if (((b*B)/> - 4 * a * c ) == 0)
		{
			r1=(-B)/>/(2*a);
			r2=(-B)/>/(2*a);
			cout << x1 << x2 << endl; 
			
		}
		else
			cout << "The number is generated are not real." << endl;
	
}

double num_positive ( double x1, double x2 )
{
	    double a1=0;
		double b1=0;
		if ( x1 >= a1 || x2 >= b1 )
		{
	      	 cout << "There one positive value";
		}
		exit (1);
}




Was This Post Helpful? 0
  • +
  • -

#9 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Reading from file.Can someone help please!

Posted 02 April 2010 - 07:32 PM

you have to insert the call to the functions inside the while loop that reads the file as well.
also, to make sure that all if\else statements prints a statement, add a printing statement for the x1 and x2 here also:
else if (((b* - 4 * a * c ) == 0)
{
r1=(-/(2*a);
r2=(-/(2*a);
x1 = r1;
x2 = r2;
cout<<"x1 = "<<x1<<" "<<"x2 = "<<x2<<endl;
}

as for what i said about inserting the functions inside the while loopL
else
{
while (!infile.eof()) 
{
infile >> a, b, c;

infile >> a, b, c;
cout << "a = " << a << " b = " << b << " c = " << c<<endl;//add new line
QuadraticEquation( t1, t2,t3, res1, res2);
y2 = num_positive(res1,res2);
}

what is the use for temp1 and temp2?
as erik.price said, i believe that inside the num_positive function you meant to use the parameters q and p,
so, i believe that you have to pass that function res1 and res2 as parameters.
keep in mind that res1 and res2 get NO value if there is no solution to the equation.
so. you should call num_positive only inside the QuadraticEquation, in the if statements where r1 and r2 have values:
void QuadraticEquation( double a , double b , double c, double &x1, double &x2 )
{

double r1, r2;

if ( ((b* b ) - 4 * a * c )> 0 )
{
r1= (-b+sqrt((b* b ) - 4 * a * c ))/(2*a);
r2= (-b-sqrt((b* b ) - 4 * a * c ))/(2*a);
x1 = r1;
x2 = r2;
cout<<"x1 = "<<x1<<" "<<"x2 = "<<x2<<endl;
num_positive(x1, x2);
}
else if (((b* b ) - 4 * a * c ) == 0)
{
r1=(-/(2*a);
r2=(-/(2*a);
x1 = r1;
x2 = r2;
cout<<"x1 = "<<x1<<" "<<"x2 = "<<x2<<endl;
num_positive(x1, x2);
}
else
cout << "The number is generated are not real." << endl;

}


}

and then no need to call num_positive inside the while loop to read the file:
else
{
while (!infile.eof()) 
{
infile >> a, b, c;

infile >> a, b, c;
cout << "a = " << a << " b = " << b << " c = " << c<<endl;//add new line
QuadraticEquation( t1, t2,t3, res1, res2);
}


most important, update your num_positive function as erik.price suggested.
Was This Post Helpful? 1
  • +
  • -

#10 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Reading from file.Can someone help please!

Posted 02 April 2010 - 07:37 PM

while (!infile.eof()) 

Read this.

What does your "project4.txt" file look like?

double num_positive ( double x1, double x2 )
{
            double a1=0;
                double b1=0;
                if ( x1 >= a1 || x2 >= b1 )
                {
                 cout << "There one positive value";
                }
                exit (1);
}

I still don't think you want to exit at the end of this function.
Why is it double? it doesn't return anything, so make it void.

Also, the if will be true if only one is positive, or both, so you should either change the if to test if only one is positive, or change the output to "At least one is positive"
Was This Post Helpful? 0
  • +
  • -

#11 dinisrainman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 10-February 08

Re: Reading from file.Can someone help please!

Posted 02 April 2010 - 08:09 PM

Japanir and erik both thanks a bunch for your help. I corrected it, unfortunately I wasn't on time to submitted.

thanks a lot guys
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1