Calling functions! Program to read #s from .txt file, calculate discriminate,

This is a discussion on Calling functions! Program to read #s from .txt file, calculate discriminate, within the C++ Programming forums, part of the General Programming Boards category; Hey there! If someone could help me with my assignment, I would greatly appreciate it! This is my problem from ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Location
    WA
    Posts
    2

    Calling functions! Program to read #s from .txt file, calculate discriminate,

    Hey there! If someone could help me with my assignment, I would greatly appreciate it!

    This is my problem from my professor:

    Write a program (C++) that will read 5 sets of 3 values a, b, c from a file (quad.txt).
    Create a function to calculate the discriminate and return either: "positive", "zero" or "negative"
    In other words, the function will calculate the discriminate and determine if the discriminate is positive, zero and negative and return the correct string message.
    Create a function that will calculate and display a, b, c and the roots or no roots based on the string message returned.
    Read another set of a, b, c from a file.
    Repeat this for 5 sets of a, b, c.
    End

    This is what I have so far (basically I have a program that does all of what is stated above, but I need it to call for the functions instead of calculating in main

    Code:
    //
    
    #include "stdafx.h"
    #include<iostream> //required for cout. endl.
    #include<fstream>//Required for ifstream, ofstream
    #include<cmath> 
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    //Declare variables
    	double  a, b, c, discriminant;
    	float r1, r2;
    	
    	ifstream fin;
    	ofstream  report;
    
    
    //Open file and read first data line (a, b, c)
    	fin.open("quad.txt");
    	if(fin.fail() ) //open failed
    	{
    		cerr<<"File quad.txt could not be opened";
    		exit(1); //end execution of the program
        }
    	while(!fin.eof())
    	{
    		fin >> a >> b >> c;
    		
    		//Calculate discriminant
    		discriminant=((b*b)-4*a*c);
    		cout << discriminant << endl;
    		if(discriminant>0)
    		{
    			r1=(-1*b+sqrt(discriminant))/(2*a);
    			r2=(-1*b-sqrt(discriminant))/(2*a);
    			cout<<"r1="<<r1<<"\n";
    			cout<<"r2="<<r2<<"\n";
    			cout<<"there are two distinct roots";
    		}
    		else if(discriminant==0)
    		{
    			r1=(-1*b)/(2*a);
    			cout<<"r1="<<r1<<"\n";
    			cout<<"there is exactly one distinct real root";
    		}
    		else
    		{
    			discriminant=-1*discriminant;
    			cout<<"No real roots";
    		}
    	cout<<"\n";
    		system("pause");
    	}	
    
    
    return 0;
    }
    Thanks guys!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    WA
    Posts
    2
    OK, so I have done a bit more progress (see code)...but I'm having trouble with the call for function line:

    discriminant=dis_calc(?????); //calling function

    do I put a, b, c into the function, or something else?

    Thanks in advance for any help!

    Code:
    //
    #include "stdafx.h"
    #include<iostream> //required for cout. endl.
    #include<fstream>//Required for ifstream, ofstream
    #include<cmath> //required for acos().
    using namespace std;
    double dis_calc(double double double); //Function Prototype
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    //Declare variables
    	double  a, b, c, discriminant;
    	double r1, r2;
    	
    	ifstream fin;
    	ofstream  report;
    
    	//prompt user for name of input file
    	/*cout<<"enter the name of the input file";
    	cin>>filename;*/
    
    
    //Open file and read first data line (a, b, c)
    	fin.open("quad.txt");
    	if(fin.fail() ) //open failed
    	{
    		cerr<<"File quad.txt could not be opened";
    		exit(1); //end execution of the program
        }
    	while(!fin.eof())
    	{
    		fin >> a >> b >> c;
    		
    		//Calculate discriminant
    
    		discriminant=dis_calc(?????); //calling function
    
    		cout << discriminant << endl;
    		if(discriminant>0)
    		{
    			r1=(-1*b+sqrt(discriminant))/(2*a);
    			r2=(-1*b-sqrt(discriminant))/(2*a);
    			cout<<"r1="<<r1<<"\n";
    			cout<<"r2="<<r2<<"\n";
    			cout<<"there are two distinct roots";
    		}
    		else if(discriminant==0)
    		{
    			r1=(-1*b)/(2*a);
    			cout<<"r1="<<r1<<"\n";
    			cout<<"there is exactly one distinct real root";
    		}
    		else
    		{
    			discriminant=-1*discriminant;
    			cout<<"No real roots";
    		}
    	cout<<"\n";
    		system("pause");
    	}	
    
    
    return 0;
    }
    
    	
    double dis_calc(double a, double b, double c)
    {
    	double discriminant=((b*b)-4*a*c);
    	return discriminant;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read a file of commands and execute them
    By surlyTomato in forum C Programming
    Replies: 5
    Last Post: 08-20-2009, 12:32 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Replies: 5
    Last Post: 02-01-2003, 10:58 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21