I started learning about objects and classes this week and it's really confusing. I'm trying to make a class/object to read data from an input file and use the values to calculate the profit loss/gain(in percent and numbers). So far when I try to run the program I get an error saying " 'main' : looks like a function definition, but there is no parameter list; skipping apparent body"

Current input from txt file:
IBM 100 93.2 98.6
MER 200 67.2 43.89
QQQ 300 78.9 70.0
C 200 35.78 50.02
CSCO 400 45.67 57.23

What I have so far(Sorry if it's messy, it's my first time working with class/objects):

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

class Stock
    {
    private:
        string stockName;
        double numShares;
        double buyPrice;
        double currPrice;
	public:
		void setstockName(string);
        void setnumShares(double);
        void setbuyPrice(double);
        void setcurrPrice(double);
        double computePercentProfitLoss(double);
        double computeDollarProfitLoss(double);
};

int main{}
{	
	Stock stockdata;
	string stockname;
	double numshar, buypric, currpric;

    ifstream inFile;
	ofstream outFile;
    
	inFile.open("C:\\text1.txt");
	outFile.open("C:\\text2.txt");

    if(!inFile.good()) 
	{
        cout << "Unable to open file";
    }
	  
      while (inFile.good())
	  {
	  inFile >> stocknam >> numshar >> buypric >> currpric;
	  stockdata.setstockName(stocknam);
	  stockdata.setnumShares(numchar);
	  stockdata.setbuyPrice(buypric);
	  stockdata.setcurrPrice(currpric);
	  }
	  outfile << setw(7) << "Stock" << setw(7) << "# Shares" << setw(7) << "Buy Price" << setw(7) << "Current Price << setw(7) << "$P/L" << setw(7) << "%P/L"<<endl;
	
	outFile << setw(7) << stockName << setw(7) << numShares << setw(7) << buyPrice << setw(7) << currPrice << setw(7) << computePercentProfitLoss(stockdata.setnumShares(), stockdata.setbuyPrice(), stockdata.setcurrPrice())<< setw(7)<< computePercentProfitLoss(stockdata.setnumShares(), stockdata.setbuyPrice(), stockdata.setcurrPrice())<< endl;

    inFile.close();
	outFile.close();
    
	return 0;
    
}
double computePercentProfitLoss(double nshare, double bprice, double cprice);
{
	double percentloss;
	percentloss= ((cprice - bprice) * nshare) / bprice;
	if(percentloss)
	{
		cout<<"(percentloss)"<<endl;
	}
	return percentloss;
}

double computeDollarProfitLoss(double nshare2, double bprice2, double cprice2)
{
	double profitloss;
	profitloss = ((cprice2 - bprice2) * nshare2);

	if(profitloss <0)
	{
		cout<<"(profitloss)"<<endl;
	}
	return profitloss;
}

I started learning about objects and classes this week and it's really confusing. I'm trying to make a class/object to read data from an input file and use the values to calculate the profit loss/gain(in percent and numbers). So far when I try to run the program I get an error saying " 'main' : looks like a function definition, but there is no parameter list; skipping apparent body"

Current input from txt file:
IBM 100 93.2 98.6
MER 200 67.2 43.89
QQQ 300 78.9 70.0
C 200 35.78 50.02
CSCO 400 45.67 57.23

What I have so far(Sorry if it's messy, it's my first time working with class/objects):

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

class Stock
    {
    private:
        string stockName;
        double numShares;
        double buyPrice;
        double currPrice;
	public:
		void setstockName(string);
        void setnumShares(double);
        void setbuyPrice(double);
        void setcurrPrice(double);
        double computePercentProfitLoss(double);
        double computeDollarProfitLoss(double);
};

int main{}
{	
	Stock stockdata;
	string stockname;
	double numshar, buypric, currpric;

    ifstream inFile;
	ofstream outFile;
    
	inFile.open("C:\\text1.txt");
	outFile.open("C:\\text2.txt");

    if(!inFile.good()) 
	{
        cout << "Unable to open file";
    }
	  
      while (inFile.good())
	  {
	  inFile >> stocknam >> numshar >> buypric >> currpric;
	  stockdata.setstockName(stocknam);
	  stockdata.setnumShares(numchar);
	  stockdata.setbuyPrice(buypric);
	  stockdata.setcurrPrice(currpric);
	  }
	  outfile << setw(7) << "Stock" << setw(7) << "# Shares" << setw(7) << "Buy Price" << setw(7) << "Current Price << setw(7) << "$P/L" << setw(7) << "%P/L"<<endl;
	
	outFile << setw(7) << stockName << setw(7) << numShares << setw(7) << buyPrice << setw(7) << currPrice << setw(7) << computePercentProfitLoss(stockdata.setnumShares(), stockdata.setbuyPrice(), stockdata.setcurrPrice())<< setw(7)<< computePercentProfitLoss(stockdata.setnumShares(), stockdata.setbuyPrice(), stockdata.setcurrPrice())<< endl;

    inFile.close();
	outFile.close();
    
	return 0;
    
}
double computePercentProfitLoss(double nshare, double bprice, double cprice);
{
	double percentloss;
	percentloss= ((cprice - bprice) * nshare) / bprice;
	if(percentloss)
	{
		cout<<"(percentloss)"<<endl;
	}
	return percentloss;
}

double computeDollarProfitLoss(double nshare2, double bprice2, double cprice2)
{
	double profitloss;
	profitloss = ((cprice2 - bprice2) * nshare2);

	if(profitloss <0)
	{
		cout<<"(profitloss)"<<endl;
	}
	return profitloss;
}

lolz you used the wrong kind of bracket and put int main{}. I hate it when I do shit like that.

lolz you used the wrong kind of bracket and put int main{}. I hate it when I do shit like that.

Oh.. Ouch, now there's a lot of new errors. Are my classes and objects done correctly? It looks like most of the error are coming from the main function.

Oh.. Ouch, now there's a lot of new errors. Are my classes and objects done correctly? It looks like most of the error are coming from the main function.

Cut and paste the error list. bbl.

Cut and paste the error list. bbl.

my documents\visual studio 2008\projects\88888\88888\7777.cpp(42) : error C2065: 'stocknam' : undeclared identifier

my documents\visual studio 2008\projects\88888\88888\7777.cpp(43) : error C2065: 'stocknam' : undeclared identifier

my documents\visual studio 2008\projects\88888\88888\7777.cpp(44) : error C2065: 'numchar' : undeclared identifier

\my documents\visual studio 2008\projects\88888\88888\7777.cpp(48) : error C2065: 'outfile' : undeclared identifier

my documents\visual studio 2008\projects\88888\88888\7777.cpp(48) : error C2146: syntax error : missing ';' before identifier '$P'

my documents\visual studio 2008\projects\88888\88888\7777.cpp(48) : error C2065: '$P' : undeclared identifier

my documents\visual studio 2008\projects\88888\88888\7777.cpp(48) : error C2065: 'P' : undeclared identifier

my documents\visual studio 2008\projects\88888\88888\7777.cpp(48) : error C2001: newline in constant

my documents\visual studio 2008\projects\88888\88888\7777.cpp(50) : error C2146: syntax error : missing ';' before identifier 'outFile'

my documents\visual studio 2008\projects\88888\88888\7777.cpp(50) : error C2065: 'stockName' : undeclared identifier

my documents\visual studio 2008\projects\88888\88888\7777.cpp(50) : error C2065: 'numShares' : undeclared identifier

my documents\visual studio 2008\projects\88888\88888\7777.cpp(50) : error C2065: 'buyPrice' : undeclared identifier

my documents\visual studio 2008\projects\88888\88888\7777.cpp(50) : error C2065: 'currPrice' : undeclared identifier

my documents\visual studio 2008\projects\88888\88888\7777.cpp(50) : error C2660: 'Stock::setnumShares' : function does not take 0 arguments

my documents\visual studio 2008\projects\88888\88888\7777.cpp(50) : error C2660: 'Stock::setbuyPrice' : function does not take 0 arguments

my documents\visual studio 2008\projects\88888\88888\7777.cpp(50) : error C2660: 'Stock::setcurrPrice' : function does not take 0 arguments

my documents\visual studio 2008\projects\88888\88888\7777.cpp(50) : error C2660: 'Stock::setnumShares' : function does not take 0 arguments

my documents\visual studio 2008\projects\88888\88888\7777.cpp(50) : error C2660: 'Stock::setbuyPrice' : function does not take 0 arguments

my documents\visual studio 2008\projects\88888\88888\7777.cpp(50) : error C2660: 'Stock::setcurrPrice' : function does not take 0 arguments

my documents\visual studio 2008\projects\88888\88888\7777.cpp(50) : error C3861: 'computePercentProfitLoss': identifier not found

my documents\visual studio 2008\projects\88888\88888\7777.cpp(50) : error C3861: 'computePercentProfitLoss': identifier not found

my documents\visual studio 2008\projects\88888\88888\7777.cpp(59) : error C2447: '{' : missing function header (old-style formal list?)

Edited 5 Years Ago by SeePlusPlus2: n/a

Look at your cout statement:

"Current Price << setw(7) << "$P/L" << setw(7) << "%P/L"<<endl;

Your Current Price string is running into your operator, and other numerous problems with this. Fix your "", throughout this line

Edited 5 Years Ago by Akill10: n/a

Chdek your spilling Nad your cAPS.
(In case you can't understand that, it says "Check your spelling and your CAPS".)

Does this:

inFile >> stocknam >> numshar >> buypric >> currpric;
	  stockdata.setstockName(stocknam);
	  stockdata.setnumShares(numchar);
	  stockdata.setbuyPrice(buypric);
	  stockdata.setcurrPrice(currpric);

Match These:

string stockName;
        double numShares;
        double buyPrice;
        double currPrice;

???
Answer: NO. Not only are they spelled incorrectly, the capitalization doesn't match. The compiler is pretty smart, but it's not that smart. If your spelling and caps aren't consistent throughout your program the compiler won't be able to figure out what you're trying to tell it to do.

Edited 5 Years Ago by Fbody: n/a

Look at your cout statement:

"Current Price << setw(7) << "$P/L" << setw(7) << "%P/L"<<endl;

Your Current Price string is running into your operator, and other numerous problems with this. Fix your "", throughout this line

Oh, for that I was trying to make it look something like this in the output:
Stock # Shares Buy Current $ P/L % P/L
Price Price
==================================================================
IBM 100.00 93.20 98.60 540.00 5.79
MER 200.00 67.20 43.89 ( 4662.00)( 34.69)
QQQ 300.00 78.90 70.00 ( 2670.00)( 11.28)
C 200.00 35.78 50.02 2848.00 39.80
CSCO 400.00 45.67 57.23 4624.00 25.31


I'm guessing that it's completely wrong?

Chdek your spilling Nad your cAPS.
(In case you can't understand that, it says "Check your spelling and your CAPS".)

Does this:

inFile >> stocknam >> numshar >> buypric >> currpric;
	  stockdata.setstockName(stocknam);
	  stockdata.setnumShares(numchar);
	  stockdata.setbuyPrice(buypric);
	  stockdata.setcurrPrice(currpric);

Match These:

string stockName;
        double numShares;
        double buyPrice;
        double currPrice;

???
Answer: NO. Not only are they spelled incorrectly, the capitalization doesn't match. The compiler is pretty smart, but it's not that smart. If your spelling and caps aren't consistent throughout your program the compiler won't be able to figure out what you're trying to tell it to do.

As for this, I didn't really get the concept of the private/public class. In a sample program that my professor showed in class, he declared new variables in the main function(similar to the ones in private) after making the class. I assumed that you had to make them so I shorten the names and attempted to use them to read the inputdata while storing it in objects(?) from the private class. Really confused.

Edited 5 Years Ago by SeePlusPlus2: n/a

As for this, I didn't really get the concept of the private/public class. In a sample program that my professor showed in class, he declared new variables in the main function(similar to the ones in private) after making the class. I assumed that you had to make them so I shorten the names and attempted to use them to read the inputdata while storing it in objects(?) from the private class. Really confused.

Wow, I completely missed that those were member variables. :$
These are the ones in question (Lines 25-27):

Stock stockdata;
	string stockname;
	double numshar, buypric, currpric;

Almost all of your errors are of a similar nature. C++ is case-sensitive be sure to pay close attention to both your spelling and capitalization. Changing the capitalization of one (1) character in an identifier can change it. The identifier "myVariable" is not the same as "myvariable".

I'm guessing that it's completely wrong?.

Well, just look ,

"Current Price << setw(7) << "$P/L" << setw(7) << "%P/L"<<endl;

You start a string when you use the ". Now look how this piece of code goes.
"Current Price<<setw(7)<<"...This will all be inside the string, since you start with the " and end with a ". Therefore, the << and the setw(7) are not executed as you wish, they are now simply part of a string.

Then, after you close that string, you have $P/L...which is not part of any string, it is just garbage. its like creating a string such as :

string s = "hello" my name is "seeplus"; //my name is, is just garbage, it is not encased in the inverted commas.

Therfore, at the end of your cout statement. You are starting a whole new string at:
"<<endl;
Since you are not closing this, the rest of your code will be seen as part of this string, and will no doubt receieve alot of errors.

Wow, I completely missed that those were member variables. :$
These are the ones in question (Lines 25-27):

Stock stockdata;
	string stockname;
	double numshar, buypric, currpric;

Almost all of your errors are of a similar nature. C++ is case-sensitive be sure to pay close attention to both your spelling and capitalization. Changing the capitalization of one (1) character in an identifier can change it. The identifier "myVariable" is not the same as "myvariable".

Well, just look ,

"Current Price << setw(7) << "$P/L" << setw(7) << "%P/L"<<endl;

You start a string when you use the ". Now look how this piece of code goes.
"Current Price<<setw(7)<<"...This will all be inside the string, since you start with the " and end with a ". Therefore, the << and the setw(7) are not executed as you wish, they are now simply part of a string.

Then, after you close that string, you have $P/L...which is not part of any string, it is just garbage. its like creating a string such as :

string s = "hello" my name is "seeplus"; //my name is, is just garbage, it is not encased in the inverted commas.

Therfore, at the end of your cout statement. You are starting a whole new string at:
"<<endl;
Since you are not closing this, the rest of your code will be seen as part of this string, and will no doubt receieve alot of errors.

Ah, that was careless of me. Now I have fewer errors . How do functions like stockdata.setstockName() work? Do I have it set up right in the while loop and also the public part?

Current errors:

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C2660: 'Stock::setnumShares' : function does not take 0 arguments

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C2660: 'Stock::setbuyPrice' : function does not take 0 arguments

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C2660: 'Stock::setcurrPrice' : function does not take 0 arguments

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C2660: 'Stock::setnumShares' : function does not take 0 arguments

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C2660: 'Stock::setbuyPrice' : function does not take 0 arguments

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C2660: 'Stock::setcurrPrice' : function does not take 0 arguments

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C3861: 'computePercentProfitLoss': identifier not found

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C3861: 'computeDollarProfitLoss': identifier not found

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(60) : error C2447: '{' : missing function header (old-style formal list?)

Current code:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

class Stock
    {
    private:
        string stockName;
        double numShares;
        double buyPrice;
        double currPrice;
	public:
		void setstockName(string);
        void setnumShares(double);
        void setbuyPrice(double);
        void setcurrPrice(double);
        double computePercentProfitLoss(double);
        double computeDollarProfitLoss(double);
};

int main()
{	
	Stock stockdata;
	string stockname;
	double numshar, buypric, currpric;

    ifstream inFile;
	ofstream outFile;
    
	inFile.open("C:\\text1.txt");
	outFile.open("C:\\text2.txt");

    if(!inFile.good()) 
	{
        cout << "Unable to open file";
    }
	  
      while (inFile.good())
	  {
	  inFile >> stockname >> numshar >> buypric >> currpric;
	  stockdata.setstockName(stockname);
	  stockdata.setnumShares(numshar);
	  stockdata.setbuyPrice(buypric);
	  stockdata.setcurrPrice(currpric);
	  }
	  
	outFile << setw(7) << "Stock" << setw(7) << "# Shares" << setw(7) << "Buy Price" << setw(7) << "Current Price" << setw(7) << "$p/l" << setw(7) << "%p/l";
	
	outFile << setw(7) << stockname << setw(7) << numshar << setw(7) << buypric << setw(7) << currpric << setw(7) << computePercentProfitLoss(stockdata.setnumShares(), stockdata.setbuyPrice(), stockdata.setcurrPrice()) << setw(7)<< computeDollarProfitLoss(stockdata.setnumShares(), stockdata.setbuyPrice(), stockdata.setcurrPrice());

    inFile.close();
	outFile.close();
    
	return 0;
    
}
double computePercentProfitLoss(double nshare, double bprice, double cprice);
{
	double percentloss;
	percentloss= ((cprice - bprice) * nshare) / bprice;
	if(percentloss)
	{
		cout<<"(percentloss)"<<endl;
	}
	return percentloss;
}

double computeDollarProfitLoss(double nshare2, double bprice2, double cprice2)
{
	double profitloss;
	profitloss = ((cprice2 - bprice2) * nshare2);

	if(profitloss <0)
	{
		cout<<"(profitloss)"<<endl;
	}
	return profitloss;
}

Edited 5 Years Ago by SeePlusPlus2: n/a

I'm not really competent in c++ yet, I have never used ifstream. But, I just noticed another simple error you have with one of your methods:

double computePercentProfitLoss(double nshare, double bprice, double cprice);

You are placing a ; at the end of the method declaration, therefore the next block of code is not part of that method!

I'm not really competent in c++ yet, I have never used ifstream. But, I just noticed another simple error you have with one of your methods:

double computePercentProfitLoss(double nshare, double bprice, double cprice);

You are placing a ; at the end of the method declaration, therefore the next block of code is not part of that method!

Damn me and my bad habits. I was hoping it'd fix all the problems, but I guess not, haha. Getting less errors though:

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C2660: 'Stock::setnumShares' : function does not take 0 arguments

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C2660: 'Stock::setbuyPrice' : function does not take 0 arguments

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C2660: 'Stock::setcurrPrice' : function does not take 0 arguments

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C2660: 'Stock::setnumShares' : function does not take 0 arguments

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C2660: 'Stock::setbuyPrice' : function does not take 0 arguments

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C2660: 'Stock::setcurrPrice' : function does not take 0 arguments

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C3861: 'computePercentProfitLoss': identifier not found

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C3861: 'computeDollarProfitLoss': identifier not found
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

class Stock
    {
    private:
        string stockName;
        double numShares;
        double buyPrice;
        double currPrice;
	public:
		void setstockName(string);
        void setnumShares(double);
        void setbuyPrice(double);
        void setcurrPrice(double);
        double computePercentProfitLoss(double);
        double computeDollarProfitLoss(double);
};

int main()
{	
	Stock stockdata;
	string stockname;
	double numshar, buypric, currpric;

    ifstream inFile;
	ofstream outFile;
    
	inFile.open("C:\\text1.txt");
	outFile.open("C:\\text2.txt");

    if(!inFile.good()) 
	{
        cout << "Unable to open file";
    }
	  
      while (inFile.good())
	  {
	  inFile >> stockname >> numshar >> buypric >> currpric;
	  stockdata.setstockName(stockname);
	  stockdata.setnumShares(numshar);
	  stockdata.setbuyPrice(buypric);
	  stockdata.setcurrPrice(currpric);
	  }
	  
	outFile << setw(7) << "Stock" << setw(7) << "# Shares" << setw(7) << "Buy Price" << setw(7) << "Current Price" << setw(7) << "$p/l" << setw(7) << "%p/l";
	
	outFile << setw(7) << stockname << setw(7) << numshar << setw(7) << buypric << setw(7) << currpric << setw(7) << computePercentProfitLoss(stockdata.setnumShares(), stockdata.setbuyPrice(), stockdata.setcurrPrice()) << setw(7)<< computeDollarProfitLoss(stockdata.setnumShares(), stockdata.setbuyPrice(), stockdata.setcurrPrice());

    inFile.close();
	outFile.close();
    
	return 0;
    
}
double computePercentProfitLoss(double nshare, double bprice, double cprice)
{
	double percentloss;
	percentloss= ((cprice - bprice) * nshare) / bprice;
	if(percentloss)
	{
		cout<<"(" << percentloss ")"; //To put negative value in parentheses.
	}
	return percentloss;
}

double computeDollarProfitLoss(double nshare2, double bprice2, double cprice2)
{
	double profitloss;
	profitloss = ((cprice2 - bprice2) * nshare2);

	if(profitloss <0)
	{
		cout<<"(" << profitloss ")"; //To put negative value in parentheses.
	}
	return profitloss;
}

Edited 5 Years Ago by SeePlusPlus2: n/a

Damn me and my bad habits. I was hoping it'd fix all the problems, but I guess not, haha. Getting less errors though:

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C2660: 'Stock::setnumShares' : function does not take 0 arguments

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C2660: 'Stock::setbuyPrice' : function does not take 0 arguments

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C2660: 'Stock::setcurrPrice' : function does not take 0 arguments

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C2660: 'Stock::setnumShares' : function does not take 0 arguments

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C2660: 'Stock::setbuyPrice' : function does not take 0 arguments

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C2660: 'Stock::setcurrPrice' : function does not take 0 arguments

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C3861: 'computePercentProfitLoss': identifier not found

my documents\visual studio 2008\projects\hw2\hw2\hw 2.cpp(51) : error C3861: 'computeDollarProfitLoss': identifier not found
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

class Stock
    {
    private:
        string stockName;
        double numShares;
        double buyPrice;
        double currPrice;
	public:
		void setstockName(string);
        void setnumShares(double);
        void setbuyPrice(double);
        void setcurrPrice(double);
        double computePercentProfitLoss(double);
        double computeDollarProfitLoss(double);
};

int main()
{	
	Stock stockdata;
	string stockname;
	double numshar, buypric, currpric;

    ifstream inFile;
	ofstream outFile;
    
	inFile.open("C:\\text1.txt");
	outFile.open("C:\\text2.txt");

    if(!inFile.good()) 
	{
        cout << "Unable to open file";
    }
	  
      while (inFile.good())
	  {
	  inFile >> stockname >> numshar >> buypric >> currpric;
	  stockdata.setstockName(stockname);
	  stockdata.setnumShares(numshar);
	  stockdata.setbuyPrice(buypric);
	  stockdata.setcurrPrice(currpric);
	  }
	  
	outFile << setw(7) << "Stock" << setw(7) << "# Shares" << setw(7) << "Buy Price" << setw(7) << "Current Price" << setw(7) << "$p/l" << setw(7) << "%p/l";
	
	outFile << setw(7) << stockname << setw(7) << numshar << setw(7) << buypric << setw(7) << currpric << setw(7) << computePercentProfitLoss(stockdata.setnumShares(), stockdata.setbuyPrice(), stockdata.setcurrPrice()) << setw(7)<< computeDollarProfitLoss(stockdata.setnumShares(), stockdata.setbuyPrice(), stockdata.setcurrPrice());

    inFile.close();
	outFile.close();
    
	return 0;
    
}
double computePercentProfitLoss(double nshare, double bprice, double cprice)
{
	double percentloss;
	percentloss= ((cprice - bprice) * nshare) / bprice;
	if(percentloss)
	{
		cout<<"(" << percentloss ")"; //To put negative value in parentheses.
	}
	return percentloss;
}

double computeDollarProfitLoss(double nshare2, double bprice2, double cprice2)
{
	double profitloss;
	profitloss = ((cprice2 - bprice2) * nshare2);

	if(profitloss <0)
	{
		cout<<"(" << profitloss ")"; //To put negative value in parentheses.
	}
	return profitloss;
}

I'm back. Okay you have a few problems.

First, remember that "ComputeDollarProfitLoss" and so on, do not exist except inside the object you created. So putting ComputeDollarProfitLoss by itself will cause your compiler to say wtf is that. You have to do stockname.ComputeDollarProfitLoss(num1, num2, num3).

Second, you're not evoking setcurrPrice, or those other functions correctly. You have it in your class definition that setcurrPrice takes a double. i.e. setcurrPrice(double). So this means you HAVE to give it a number.

Obviously, from what you're trying to do, you dont want to give it a new number. All you want it to do is output a number, right, so you can put it into a file.

Remember to keep setters and getters separate. Have one function to SET the price, and another to retrieve it.

So make a new public function in your class:
double getcurrPrice() {return currPrice;}

And do this for all the variables that you need to retrieve data from. Then use these functions in place of the "set" functions.
-Greywolf

I'm back. Okay you have a few problems.

First, remember that "ComputeDollarProfitLoss" and so on, do not exist except inside the object you created. So putting ComputeDollarProfitLoss by itself will cause your compiler to say wtf is that. You have to do stockname.ComputeDollarProfitLoss(num1, num2, num3).

Second, you're not evoking setcurrPrice, or those other functions correctly. You have it in your class definition that setcurrPrice takes a double. i.e. setcurrPrice(double). So this means you HAVE to give it a number.

Obviously, from what you're trying to do, you dont want to give it a new number. All you want it to do is output a number, right, so you can put it into a file.

Remember to keep setters and getters separate. Have one function to SET the price, and another to retrieve it.

So make a new public function in your class:
double getcurrPrice() {return currPrice;}

And do this for all the variables that you need to retrieve data from. Then use these functions in place of the "set" functions.
-Greywolf

Do I really need the stockName get/set function since I'm only outputting it in the output file? Also when you use the get and set functions you have to use the object created in main function, right? I have Stock stockdata declared, so it would be something like stockdata.setstockName(stockname) and stockdata.getstockName(stockname)?

Currently getting these errors, I think I'm using the .get/set function and passing the data wrong:

my documents\visual studio 2008\projects\hw2ed\hw2ed\hw2ed.cpp(49) : error C2660: 'Stock::setstockName' : function does not take 1 arguments

my documents\visual studio 2008\projects\hw2ed\hw2ed\hw2ed.cpp(50) : error C2660: 'Stock::setnumShares' : function does not take 1 arguments

my documents\visual studio 2008\projects\hw2ed\hw2ed\hw2ed.cpp(51) : error C2660: 'Stock::setbuyPrice' : function does not take 1 arguments

my documents\visual studio 2008\projects\hw2ed\hw2ed\hw2ed.cpp(52) : error C2660: 'Stock::setcurrPrice' : function does not take 1 arguments

my documents\visual studio 2008\projects\hw2ed\hw2ed\hw2ed.cpp(59) : error C2660: 'Stock::computePercentProfitLoss' : function does not take 3 arguments

my documents\visual studio 2008\projects\hw2ed\hw2ed\hw2ed.cpp(60) : error C2660: 'Stock::computeDollarProfitLoss' : function does not take 3 arguments

Current program:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

class Stock
    {
    private:
        string stockName;
        double numShares;
        double buyPrice;
        double currPrice;
	public:
		void setstockName();
        void setnumShares();
        void setbuyPrice();
        void setcurrPrice();
        double computePercentProfitLoss();
        double computeDollarProfitLoss();
		string getstockName() {return stockName;}
		double getnumShares() {return numShares;}
		double getbuyPrice() {return buyPrice;}
		double getcurrPrice() {return currPrice;}

		
};

int main()
{	
	Stock stockdata;
	string stockname;
	double numshar, buypric, currpric;

    ifstream inFile;
	ofstream outFile;
    
	inFile.open("C:\\text1.txt");
	outFile.open("C:\\text2.txt");

    if(!inFile.good()) 
	{
        cout << "Unable to open file";
    }
	
      while (inFile.good())
	  {
		inFile >> stockname >> numshar >> buypric >> currpric;
		stockdata.setstockName(stockname);
		stockdata.setnumShares(numshar);
		stockdata.setbuyPrice(buypric);
		stockdata.setcurrPrice(currpric);
	  }

	stockdata.getstockName();
	stockdata.getnumShares();
	stockdata.getbuyPrice();
	stockdata.getcurrPrice();
	stockdata.computePercentProfitLoss(numshar, buypric, currpric);
	stockdata.computeDollarProfitLoss(numshar, buypric, currpric);
	
	  
	outFile << "Stock" << setw(7) << "# Shares" << setw(7) << "Buy Price" << setw(7) << "Current Price" << setw(7) << "$p/l" << setw(7) << "%p/l";
	
	outFile << stockname << setw(7) << numshar << setw(7) << buypric << setw(7) << currpric << setw(7) << stockdata.computePercentProfitLoss() << setw(7)<< stockdata.computeDollarProfitLoss();

    inFile.close();
	outFile.close();
    
	return 0;
    
}
double computePercentProfitLoss(double nshare, double bprice, double cprice)
{
	double percentloss;
	percentloss= ((cprice - bprice) * nshare) / bprice;
	if(percentloss < 0)
	{
 		cout << "(" << percentloss << ")" << endl; //To put negative value in parentheses.
	}
	return percentloss;
}

double computeDollarProfitLoss(double nshare2, double bprice2, double cprice2)
{
	double profitloss;
	profitloss = ((cprice2 - bprice2) * nshare2);

	if(profitloss < 0)
	{
		cout << "(" << profitloss << ")" << endl; //To put negative value in parentheses.
	}
	return profitloss;
}

Do I really need the stockName get/set function since I'm only outputting it in the output file? Also when you use the get and set functions you have to use the object created in main function, right? I have Stock stockdata declared, so it would be something like stockdata.setstockName(stockname) and stockdata.getstockName(stockname)?

Currently getting these errors, I think I'm using the .get/set function and passing the data wrong:

my documents\visual studio 2008\projects\hw2ed\hw2ed\hw2ed.cpp(49) : error C2660: 'Stock::setstockName' : function does not take 1 arguments

my documents\visual studio 2008\projects\hw2ed\hw2ed\hw2ed.cpp(50) : error C2660: 'Stock::setnumShares' : function does not take 1 arguments

my documents\visual studio 2008\projects\hw2ed\hw2ed\hw2ed.cpp(51) : error C2660: 'Stock::setbuyPrice' : function does not take 1 arguments

my documents\visual studio 2008\projects\hw2ed\hw2ed\hw2ed.cpp(52) : error C2660: 'Stock::setcurrPrice' : function does not take 1 arguments

my documents\visual studio 2008\projects\hw2ed\hw2ed\hw2ed.cpp(59) : error C2660: 'Stock::computePercentProfitLoss' : function does not take 3 arguments

my documents\visual studio 2008\projects\hw2ed\hw2ed\hw2ed.cpp(60) : error C2660: 'Stock::computeDollarProfitLoss' : function does not take 3 arguments

Current program:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

class Stock
    {
    private:
        string stockName;
        double numShares;
        double buyPrice;
        double currPrice;
	public:
		void setstockName();
        void setnumShares();
        void setbuyPrice();
        void setcurrPrice();
        double computePercentProfitLoss();
        double computeDollarProfitLoss();
		string getstockName() {return stockName;}
		double getnumShares() {return numShares;}
		double getbuyPrice() {return buyPrice;}
		double getcurrPrice() {return currPrice;}

		
};

int main()
{	
	Stock stockdata;
	string stockname;
	double numshar, buypric, currpric;

    ifstream inFile;
	ofstream outFile;
    
	inFile.open("C:\\text1.txt");
	outFile.open("C:\\text2.txt");

    if(!inFile.good()) 
	{
        cout << "Unable to open file";
    }
	
      while (inFile.good())
	  {
		inFile >> stockname >> numshar >> buypric >> currpric;
		stockdata.setstockName(stockname);
		stockdata.setnumShares(numshar);
		stockdata.setbuyPrice(buypric);
		stockdata.setcurrPrice(currpric);
	  }

	stockdata.getstockName();
	stockdata.getnumShares();
	stockdata.getbuyPrice();
	stockdata.getcurrPrice();
	stockdata.computePercentProfitLoss(numshar, buypric, currpric);
	stockdata.computeDollarProfitLoss(numshar, buypric, currpric);
	
	  
	outFile << "Stock" << setw(7) << "# Shares" << setw(7) << "Buy Price" << setw(7) << "Current Price" << setw(7) << "$p/l" << setw(7) << "%p/l";
	
	outFile << stockname << setw(7) << numshar << setw(7) << buypric << setw(7) << currpric << setw(7) << stockdata.computePercentProfitLoss() << setw(7)<< stockdata.computeDollarProfitLoss();

    inFile.close();
	outFile.close();
    
	return 0;
    
}
double computePercentProfitLoss(double nshare, double bprice, double cprice)
{
	double percentloss;
	percentloss= ((cprice - bprice) * nshare) / bprice;
	if(percentloss < 0)
	{
 		cout << "(" << percentloss << ")" << endl; //To put negative value in parentheses.
	}
	return percentloss;
}

double computeDollarProfitLoss(double nshare2, double bprice2, double cprice2)
{
	double profitloss;
	profitloss = ((cprice2 - bprice2) * nshare2);

	if(profitloss < 0)
	{
		cout << "(" << profitloss << ")" << endl; //To put negative value in parentheses.
	}
	return profitloss;
}

Ok there's no point to having these just standalone in the middle of main().

stockdata.getstockName();
stockdata.getnumShares();
stockdata.getbuyPrice();
stockdata.getcurrPrice();	
stockdata.computePercentProfitLoss(numshar, buypric, currpric);
stockdata.computeDollarProfitLoss(numshar, buypric, currpric);

Since all they do is return a double. Getters are used to retrieve information, but you're not doing anything with that information.

You could do:
stockname = stockdata.getstockName();
Or
cout << stockdata.computePercentProfitLoss(numshar, buypric, currpric);

And that might be useful.

You need to think about the flow of information and what each function actually does. From what I can see, you don't even need to have computePercentProfitLoss take any arguments, since you already have that information in hand stored in your class. You could do something like this, rather than pass it arguments:

double computePercentProfitLoss()
{
	double percentloss;
	percentloss= ((currPrice - buyPrice) * numShares) / buyPrice;
	if(percentloss < 0)
	{
 		cout << "(" << percentloss << ")" << endl; //To put negative value in parentheses.
	}
	return percentloss;
}

Ok there's no point to having these just standalone in the middle of main().

stockdata.getstockName();
stockdata.getnumShares();
stockdata.getbuyPrice();
stockdata.getcurrPrice();	
stockdata.computePercentProfitLoss(numshar, buypric, currpric);
stockdata.computeDollarProfitLoss(numshar, buypric, currpric);

Since all they do is return a double. Getters are used to retrieve information, but you're not doing anything with that information.

You could do:
stockname = stockdata.getstockName();
Or
cout << stockdata.computePercentProfitLoss(numshar, buypric, currpric);

And that might be useful.

You need to think about the flow of information and what each function actually does. From what I can see, you don't even need to have computePercentProfitLoss take any arguments, since you already have that information in hand stored in your class. You could do something like this, rather than pass it arguments:

double computePercentProfitLoss()
{
	double percentloss;
	percentloss= ((currPrice - buyPrice) * numShares) / buyPrice;
	if(percentloss < 0)
	{
 		cout << "(" << percentloss << ")" << endl; //To put negative value in parentheses.
	}
	return percentloss;
}

Totally lost on this, since it has to have a value entered if I put something like 'double, double, double' in the brackets of the set function, what should I put? The sample code I saw during class was simple because it required the user to enter a code and use cin to give it a value, but now with input data I'm not sure what to do. Sorry for asking so many questions. I think I got the computeProfitloss/computeDollarloss functions fixed, but right now I'm still getting this:

my documents\visual studio 2008\projects\homework2 final\homework2 final\homework2 edit.cpp(50) : error C2660: 'Stock::setstockName' : function does not take 1 arguments

my documents\visual studio 2008\projects\homework2 final\homework2 final\homework2 edit.cpp(51) : error C2660: 'Stock::setnumShares' : function does not take 1 arguments

my documents\visual studio 2008\projects\homework2 final\homework2 final\homework2 edit.cpp(52) : error C2660: 'Stock::setbuyPrice' : function does not take 1 arguments

my documents\visual studio 2008\projects\homework2 final\homework2 final\homework2 edit.cpp(53) : error C2660: 'Stock::setcurrPrice' : function does not take 1 arguments
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

class Stock
    {
    private:
        string stockName;
        double numShares;
        double buyPrice;
        double currPrice;
	public:
	void setstockName();
        void setnumShares();
        void setbuyPrice();
        void setcurrPrice();
        string getstockName() {return stockName;}
		double getnumShares() {return numShares;}
		double getbuyPrice() {return buyPrice;}
		double getcurrPrice() {return currPrice;}
		
};

int main()
{	
	Stock stockdata;
	string stockname;
	double numshar, buypric, currpric;
	double computePercentProfitLoss(double, double, double);
	double computeDollarProfitLoss(double, double, double);
	double percentloss;
	double dollarloss;

    ifstream inFile;
	ofstream outFile;
    
	inFile.open("C:\\text1.txt");
	outFile.open("C:\\text2.txt");

    if(!inFile.good()) 
	{
		cout << "Unable to open file";
    }
	
      while (inFile.good())
	  {
		inFile >> stockname >> numshar >> buypric >> currpric;
		stockdata.setstockName(stockname);
		stockdata.setnumShares(numshar);
		stockdata.setbuyPrice(buypric);
		stockdata.setcurrPrice(currpric);
	  }

	stockname = stockdata.getstockName();
	numshar = stockdata.getnumShares();
	buypric = stockdata.getbuyPrice();
	currpric = stockdata.getcurrPrice();

	percentloss = computePercentProfitLoss(numshar, buypric, currpric);
	dollarloss = computeDollarProfitLoss(numshar, buypric, currpric);
	  
	outFile << "Stock" << setw(7) << "# Shares" << setw(7) << "Buy Price" << setw(7) << "Current Price" << setw(7) << "$p/l" << setw(7) << "%p/l"; //Output to text file.
	
	outFile << stockname << setw(7) << numshar << setw(7) << buypric << setw(7) << currpric << setw(7) << percentloss << setw(7)<< dollarloss; //Output to text file.

    inFile.close();
	outFile.close();
    
	return 0;
    
}
double computePercentProfitLoss(int numshar, double buypric, double currpric)
{
	double percentloss;
	percentloss= ((currpric - buypric) * numshar);
	if(percentloss < 0)
	{
 		cout << "(" << percentloss << ")" << endl; //To put negative value in parentheses.
	}
	return percentloss;
}

double computeDollarProfitLoss(int numshar, double buypric, double currpric)
{
	double profitloss;
	profitloss = ((currpric - buypric) * numshar);

	if(profitloss < 0)
	{
		cout << "(" << profitloss << ")" << endl; //To put negative value in parentheses.
	}
	return profitloss;
}

You misunderstood me. You had your SET functions right. They SHOULD take a double. So put them back so inside the class they are "setwhatever(double);"

You misunderstood me. You had your SET functions right. They SHOULD take a double. So put them back so inside the class they are "setwhatever(double);"

Hmm, just changed it and started to get a weird error.

1>homework2.obj : error LNK2019: unresolved external symbol "public: void __thiscall Stock::setcurrPrice(double)" (?setcurrPrice@Stock@@QAEXN@Z) referenced in function _main

1>homework2.obj : error LNK2019: unresolved external symbol "public: void __thiscall Stock::setbuyPrice(double)" (?setbuyPrice@Stock@@QAEXN@Z) referenced in function _main

1>homework2.obj : error LNK2019: unresolved external symbol "public: void __thiscall Stock::setnumShares(double)" (?setnumShares@Stock@@QAEXN@Z) referenced in function _main

1>homework2.obj : error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Stock::setstockName(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setstockName@Stock@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V23@@Z) referenced in function _main

My Documents\Visual Studio 2008\Projects\Homework2 final\Debug\Homework2 final.exe : fatal error LNK1120: 4 unresolved externals
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

class Stock
    {
    private:
        string stockName;
        double numShares;
        double buyPrice;
        double currPrice;
	public:
		string setstockName(string);
        void setnumShares(double);
        void setbuyPrice(double);
        void setcurrPrice(double);
        string getstockName() {return stockName;}
		double getnumShares() {return numShares;}
		double getbuyPrice() {return buyPrice;}
		double getcurrPrice() {return currPrice;}
		
};

int main()
{	
	Stock stockdata;
	string stockname;
	double numshar, buypric, currpric;
	double computePercentProfitLoss(double, double, double);
	double computeDollarProfitLoss(double, double, double);
	double percentloss;
	double dollarloss;

    ifstream inFile;
	ofstream outFile;
    
	inFile.open("C:\\text1.txt");
	outFile.open("C:\\text2.txt");

    if(!inFile.good()) 
	{
		cout << "Unable to open file";
    }
	
      while (inFile.good())
	  {
		inFile >> stockname >> numshar >> buypric >> currpric;
		stockdata.setstockName(stockname);
		stockdata.setnumShares(numshar);
		stockdata.setbuyPrice(buypric);
		stockdata.setcurrPrice(currpric);
	  }

	stockname = stockdata.getstockName();
	numshar = stockdata.getnumShares();
	buypric = stockdata.getbuyPrice();
	currpric = stockdata.getcurrPrice();

	percentloss = computePercentProfitLoss(numshar, buypric, currpric);
	dollarloss = computeDollarProfitLoss(numshar, buypric, currpric);
	  
	outFile << "Stock" << setw(7) << "# Shares" << setw(7) << "Buy Price" << setw(7) << "Current Price" << setw(7) << "$p/l" << setw(7) << "%p/l"; //Output to text file.
	
	outFile << stockname << setw(7) << numshar << setw(7) << buypric << setw(7) << currpric << setw(7) << percentloss << setw(7)<< dollarloss; //Output to text file.

    inFile.close();
	outFile.close();
    
	return 0;
    
}
double computePercentProfitLoss(double numshar, double buypric, double currpric)
{
	double percentloss;
	percentloss= ((currpric - buypric) * numshar);
	if(percentloss < 0)
	{
 		cout << "(" << percentloss << ")" << endl; //To put negative value in parentheses.
	}
	return percentloss;
}

double computeDollarProfitLoss(double numshar, double buypric, double currpric)
{
	double profitloss;
	profitloss = ((currpric - buypric) * numshar);

	if(profitloss < 0)
	{
		cout << "(" << profitloss << ")" << endl; //To put negative value in parentheses.
	}
	return profitloss;
}

Totally lost on this, since it has to have a value entered if I put something like 'double, double, double' in the brackets of the set function, what should I put? The sample code I saw during class was simple because it required the user to enter a code and use cin to give it a value, but now with input data I'm not sure what to do. Sorry for asking so many questions. I think I got the computeProfitloss/computeDollarloss functions fixed, but right now I'm still getting this:

my documents\visual studio 2008\projects\homework2 final\homework2 final\homework2 edit.cpp(50) : error C2660: 'Stock::setstockName' : function does not take 1 arguments

my documents\visual studio 2008\projects\homework2 final\homework2 final\homework2 edit.cpp(51) : error C2660: 'Stock::setnumShares' : function does not take 1 arguments

my documents\visual studio 2008\projects\homework2 final\homework2 final\homework2 edit.cpp(52) : error C2660: 'Stock::setbuyPrice' : function does not take 1 arguments

my documents\visual studio 2008\projects\homework2 final\homework2 final\homework2 edit.cpp(53) : error C2660: 'Stock::setcurrPrice' : function does not take 1 arguments
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

class Stock
    {
    private:
        string stockName;
        double numShares;
        double buyPrice;
        double currPrice;
	public:
	void setstockName();
        void setnumShares();
        void setbuyPrice();
        void setcurrPrice();
        string getstockName() {return stockName;}
		double getnumShares() {return numShares;}
		double getbuyPrice() {return buyPrice;}
		double getcurrPrice() {return currPrice;}
		
};

int main()
{	
	Stock stockdata;
	string stockname;
	double numshar, buypric, currpric;
	double computePercentProfitLoss(double, double, double);
	double computeDollarProfitLoss(double, double, double);
	double percentloss;
	double dollarloss;

    ifstream inFile;
	ofstream outFile;
    
	inFile.open("C:\\text1.txt");
	outFile.open("C:\\text2.txt");

    if(!inFile.good()) 
	{
		cout << "Unable to open file";
    }
	
      while (inFile.good())
	  {
		inFile >> stockname >> numshar >> buypric >> currpric;
		stockdata.setstockName(stockname);
		stockdata.setnumShares(numshar);
		stockdata.setbuyPrice(buypric);
		stockdata.setcurrPrice(currpric);
	  }

	stockname = stockdata.getstockName();
	numshar = stockdata.getnumShares();
	buypric = stockdata.getbuyPrice();
	currpric = stockdata.getcurrPrice();

	percentloss = computePercentProfitLoss(numshar, buypric, currpric);
	dollarloss = computeDollarProfitLoss(numshar, buypric, currpric);
	  
	outFile << "Stock" << setw(7) << "# Shares" << setw(7) << "Buy Price" << setw(7) << "Current Price" << setw(7) << "$p/l" << setw(7) << "%p/l"; //Output to text file.
	
	outFile << stockname << setw(7) << numshar << setw(7) << buypric << setw(7) << currpric << setw(7) << percentloss << setw(7)<< dollarloss; //Output to text file.

    inFile.close();
	outFile.close();
    
	return 0;
    
}
double computePercentProfitLoss(int numshar, double buypric, double currpric)
{
	double percentloss;
	percentloss= ((currpric - buypric) * numshar);
	if(percentloss < 0)
	{
 		cout << "(" << percentloss << ")" << endl; //To put negative value in parentheses.
	}
	return percentloss;
}

double computeDollarProfitLoss(int numshar, double buypric, double currpric)
{
	double profitloss;
	profitloss = ((currpric - buypric) * numshar);

	if(profitloss < 0)
	{
		cout << "(" << profitloss << ")" << endl; //To put negative value in parentheses.
	}
	return profitloss;
}

You should also use a constructor to intially set your values. Here's a tiny sample program using some of your code with a few modifications. I didn't test it so there might be a typo somewhere.

class Stock
    {
    private:
        string stockName;
        double numShares;
        double buyPrice;
        double currPrice;
    public:
        Stock(string name, double shares, double bprice, double cprice)
          {stockName = name; numShares = shares; buyPrice = price; currPrice = cprice;} 
        void setstockName(string name) {stockName = name;}
        void setnumShares(double shares) {numShares = shares;}
        void setbuyPrice(double bprice) {buyPrice = bprice;}
        void setcurrPrice(double cprice) {currPrice = cprirce;}
        string getstockName() {return stockName;}
        double getnumShares() {return numShares;}
        double getbuyPrice() {return buyPrice;}
        double getcurrPrice() {return currPrice;}
        double computePercentProfitLoss()
           {
	  double percentloss;
	  percentloss= ((currPrice - buyPrice) * numShares);
            //dont put cout statements here... handle that in main.
	  return percentloss;
           }

        double computeDollarProfitLoss()
           {
	  double profitloss;
	  profitloss = ((currPrice - buyPrice) * numShares);
           //dont put cout statements here... handle that in main.
	  return profitloss;
           }
	
};


int main(){
//Sample code of how to use this class
string name;
double shares;
double bprice;
double cprice;
cin >> name >> shares >> bprice >> cprice;

Stock sample_stock(name, shares, bprice, cprice);//now sample_stock has all the data.

cout << sample_stock.getstockName() << endl;  //sample output... replace with your formatting code.
cout << sample_stock.getnumShares() << endl;
cout << sample_stock.getbuyPrice() << endl;
cout << sample_stock.getcurrPrice() << endl;
cout << sample_stock.computePercentProfitLoss() << endl;
cout << sample_stock.computeDollarProfitLoss() << endl;
system("pause");
return 0;
}

Edited 5 Years Ago by Greywolf333: n/a

You should also use a constructor to intially set your values. Here's a tiny sample program using some of your code with a few modifications. I didn't test it so there might be a typo somewhere.

class Stock
    {
    private:
        string stockName;
        double numShares;
        double buyPrice;
        double currPrice;
    public:
        Stock(string name, double shares, double bprice, double cprice)
          {stockName = name; numShares = shares; buyPrice = price; currPrice = cprice;} 
        void setstockName(string name) {stockName = name;}
        void setnumShares(double shares) {numShares = shares;}
        void setbuyPrice(double bprice) {buyPrice = bprice;}
        void setcurrPrice(double cprice) {currPrice = cprirce;}
        string getstockName() {return stockName;}
        double getnumShares() {return numShares;}
        double getbuyPrice() {return buyPrice;}
        double getcurrPrice() {return currPrice;}
        double computePercentProfitLoss()
           {
	  double percentloss;
	  percentloss= ((currPrice - buyPrice) * numShares);
            //dont put cout statements here... handle that in main.
	  return percentloss;
           }

        double computeDollarProfitLoss()
           {
	  double profitloss;
	  profitloss = ((currPrice - buyPrice) * numShares);
           //dont put cout statements here... handle that in main.
	  return profitloss;
           }
	
};


int main(){
//Sample code of how to use this class
string name;
double shares;
double bprice;
double cprice;
cin >> name >> shares >> bprice >> cprice;

Stock sample_stock(name, shares, bprice, cprice);//now sample_stock has all the data.

cout << sample_stock.getstockName() << endl;  //sample output... replace with your formatting code.
cout << sample_stock.getnumShares() << endl;
cout << sample_stock.getbuyPrice() << endl;
cout << sample_stock.getcurrPrice() << endl;
cout << sample_stock.computePercentProfitLoss() << endl;
cout << sample_stock.computeDollarProfitLoss() << endl;
system("pause");
return 0;
}

Awesome, I managed to output it the way I want, but for some reason only the last line form the input data is reading. Is there a problem with the way I set up the while loop?

Input:
IBM 100 93.2 98.6
MER 200 67.2 43.89
QQQ 300 78.9 70.0
C 200 35.78 50.02
CSCO 400 45.67 57.23

Current output:
Stock # Shares BPrice CPrice $p/l %p/l
CSCO 400 45.67 57.23 4624 4624

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

class Stock
    {
    private:
        string stockName;
        double numShares;
        double buyPrice;
        double currPrice;
    public:
        Stock(string name, double shares, double bprice, double cprice)
          {stockName = name; numShares = shares; buyPrice = bprice; currPrice = cprice;} 
        void setstockName(string name) {stockName = name;}
        void setnumShares(double shares) {numShares = shares;}
        void setbuyPrice(double bprice) {buyPrice = bprice;}
        void setcurrPrice(double cprice) {currPrice = cprice;}
        string getstockName() {return stockName;}
        double getnumShares() {return numShares;}
        double getbuyPrice() {return buyPrice;}
        double getcurrPrice() {return currPrice;}
        double computePercentProfitLoss()
           {
			double percentloss;
			percentloss= ((currPrice - buyPrice) * numShares);
			return percentloss;
           }

        double computeDollarProfitLoss()
           {
			double profitloss;
			profitloss = ((currPrice - buyPrice) * numShares);
			return profitloss;
           }
	
};


int main(){
string name;
double shares, bprice, cprice, percentloss, dollarloss;


	ifstream inFile;
	ofstream outFile;
    
	inFile.open("C:\\text1.txt");
	outFile.open("C:\\text2.txt");

    if(!inFile.good()) 
	{
		cout << "Unable to open file";
    }

	while (inFile.good())
	{
		inFile >> name >> shares >> bprice >> cprice;
	}
Stock stock_data(name, shares, bprice, cprice);

stock_data.getstockName();
stock_data.getnumShares();
stock_data.getbuyPrice();
stock_data.getcurrPrice();
percentloss = stock_data.computePercentProfitLoss();
dollarloss = stock_data.computeDollarProfitLoss();

outFile << "Stock" << setw(10) << "# Shares" << setw(10) << "Buy" << setw(10) << "Current" << setw(10) << "$P/L" << setw(10) << "%P/L" << endl;
outFile << setw(26) << "Price" << setw(8) << "Price" <<endl;
outFile << "==============================================================" << endl;
outFile << name << setw(10) << shares << setw(10) << bprice << setw(10) << cprice << setw(11) << percentloss << setw(11) << dollarloss;


inFile.close();
outFile.close();
system("pause");
return 0;
}

Edited 5 Years Ago by SeePlusPlus2: n/a

It looks like you're using a single object for all of your data. Additionally, it looks like you are doing all of your file reading and related data processing before you do any outputting at all. As a result, the last series of data that you read is the only data available to output because you have overwritten your other data.

You should move the statements that build your header to before your first read. Then, move your analysis and output statements for the individual stocks into your while loop. By doing so, you analyze and output each individual stock as you read it so you don't lose the data.

Edited 5 Years Ago by Fbody: n/a

Thank you everyone for all the help. Sorry for all the trouble, finally figured it out.

This question has already been answered. Start a new discussion instead.