New Posts  All Forums:Forum Nav:

C++ program help

post #1 of 3
Thread Starter 
I'm having some trouble with a program for my c++ class. For the program we are to use input and output file streams.

what I have so far is:

Code:
// Michael Barker
// Seat-ID D-10
// Section 02

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

int main()
{
string stock;
double stock_price;
int shares_owned;

ifstream inputfile;
ofstream outputfile;

inputfile.open("stock.txt");

while(inputfile)
{
inputfile.get(string, 3 , char $)
inputfile.get(double stock_price, 5, char $)
inputfile.get(int shares_owned, 3 ,char $)
}

outputfile.open("stocks2.txt");

outputfile << stock << stock_price << shares_owned << endl;



return 0;


}
Sample line from input file:

AA$27.1525$100


I'm supposed to use get to get stock symbol-"AA" stock price- 27.1525 and number of shares-100.

Now I'm confused about how to approach this. I know my syntax for get() is off and I need some help with that. But, what I want to do is to take the stock symbol and store it into variable string stock, stock price in double stock_price, and number of shares in int shares_owned. So i can manipulate them and send them to an output file with a certain format. So basically I need help with using get and its syntax.

Output:

is supposed to be formatted with the stock in all caps, stock price with 2 decimals, followed by number of shares with white space instead of '$'.

Thanks in advance.
My System
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 955 Asus M4A89GTD PRO/USB3 AM3 MSI TF III 7950 OC G. Skill Sniper 8GB 
Hard DriveHard DriveOSMonitor
Crucial M4 128GB SSD  WD Caviar Blue 500GB Windows 7 Pro / Arch Linux Dell Ultrasharp U2312hm 
PowerCase
Ocz ModXStream-Pro 500W Fractal Design Define R4 
  hide details  
Reply
My System
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 955 Asus M4A89GTD PRO/USB3 AM3 MSI TF III 7950 OC G. Skill Sniper 8GB 
Hard DriveHard DriveOSMonitor
Crucial M4 128GB SSD  WD Caviar Blue 500GB Windows 7 Pro / Arch Linux Dell Ultrasharp U2312hm 
PowerCase
Ocz ModXStream-Pro 500W Fractal Design Define R4 
  hide details  
Reply
post #2 of 3
You should have something like this:

ifstream Infile("stock.txt");
assert(Infile);
while(Infile << stock,3)
{Infile << stock_price << shares_owned;
Infile.ignore(1);

This will help you get started for now. I am at work so I can look at it later if you are still having problems. Also to print the stock price with 2 decimals you want to print cout << fixed << setprecision(2); anywhere before the loop executes that way the numbers show up with two decimal places. Make sure that you add #include <assert> before the "using namespace std;"at the beginning.

Hope this helps
Edited by Rafen - 10/31/11 at 10:52am
Veronica
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x 4 945 Gigabyte GA-MA770T-UD3P ASUS 6870 Gskill DDR3 1333 x 8GB 
Hard DriveOSMonitorKeyboard
500 GB Seagate HHD Win7 Professional 64 bit AOC 24" x 2 Leopold TKL w/ cherry mx browns 
PowerCaseMouse
Antec Earthwatts 650 Cooler Master HAF 922 Razer Deathadder 
  hide details  
Reply
Veronica
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x 4 945 Gigabyte GA-MA770T-UD3P ASUS 6870 Gskill DDR3 1333 x 8GB 
Hard DriveOSMonitorKeyboard
500 GB Seagate HHD Win7 Professional 64 bit AOC 24" x 2 Leopold TKL w/ cherry mx browns 
PowerCaseMouse
Antec Earthwatts 650 Cooler Master HAF 922 Razer Deathadder 
  hide details  
Reply
post #3 of 3
Try this out...works for any amount of input

Code:
#include <iostream>  
#include <string>

using namespace std;  
  

int main(){  

         string stock_symbol, stock_price, shares_owned;

ifstream file;
ofstream out_file;
file.open("Stock.txt");
out_file.open("Stock2.txt");

while(!file.eof()){

getline(file, stock_symbol, '$');
out_file << "Stock: " << stock_symbol << endl;
getline(file, stock_price, '$');
out_file << "Stock price: " << stock_price << endl;
getline(file, shares_owned);
out_file << "Shares owned: " << shares_owned << endl << endl;
}

file.close();
out_file.close(); 

         return 0;

}
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming