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





struct format 
  {
      string ID;
      string item_name;
      int current_stock;
      int ordered_stock;
      float retail_price;
      float selling_price;
  } greeting_cards, note_cards, calendars, pens, candles, wind_chimes;



int main () {

    string line;
    string data[6];
    int datacounter = 0;
    string substr = "";
    ifstream lastfile;
    lastfile.open("lastweek.txt"); 

    ifstream myfile;
    myfile.open ("transaction.txt");

  ofstream thisfile;
  thisfile.open ("thisweek.txt");




  //while loop will loop til end of file,gets line  ,first for loop divides line into each word, second loop prints out that line.
while(!lastfile.eof())
{
    //gets line
  getline(lastfile,line);
  //gets each individual substring
  for (int i = 0; i< line.length(); i++)
  {
      if (line.at(i) == ' ')
      {
            data[datacounter] = substr;
            datacounter ++;
            substr = "";
      }
      else
      {
          substr = substr + line.at(i);
      }
  }

  //print outs each substring
  for (int j = 0; j< 6; j++)
  {
      cout << data[j]<<" ";
  }
  cout << endl;

  datacounter = 0;
}
  //thisfile << "progress.\n";

//5 sets of 6 elemts


string format[5][7]={{"1275", "greeting_cards", "42", "0","1.95", "2.75"}
                    ,{"2907", "note_cards", "30", "0", "6.47", "7.95"}
                    ,{"4321", "calendars", "9", "30", "6.45", "8.57"}
                    ,{"5140", "pens", "22", "0", "2.63", "3.19"}
                    ,{"9673", "candles", "31", "0", "3.19", "5.85"}};










  greeting_cards.ID="1275";
  greeting_cards.item_name="greeting_cards";
  greeting_cards.current_stock=42;
  greeting_cards.ordered_stock=0;
  greeting_cards.retail_price=1.95;
  greeting_cards.selling_price=2.75;

  note_cards.ID="2907";
  note_cards.item_name="note_cards";
  note_cards.current_stock=30;
  note_cards.ordered_stock=0;
  note_cards.retail_price=6.47;
  note_cards.selling_price=7.95;

  calendars.ID="4321";
  calendars.item_name="calendars";
  calendars.current_stock=9;
  calendars.ordered_stock=30;
  calendars.retail_price=6.45;
  calendars.ordered_stock=8.57;

  pens.ID="5140";
  pens.item_name="pens";
  pens.current_stock=22;
  pens.ordered_stock=0;
  pens.retail_price=2.63;
  pens.selling_price=3.19;

  candles.ID="9673";
  candles.item_name="candles";
  candles.current_stock=31;
  candles.ordered_stock=0;
  candles.retail_price=3.19;
  candles.ordered_stock=5.85;

I am to use two input files to create three other output files of specific formatting.

I need to use structs and arrays..I am lost can someone help point me in the right direction?

If the contents of lastweek.txt is (input)

1275 greeting_cards 42 0 1.95 2.75
2907 note_cards 30 0 6.47 7.95
4321 calendars 9 30 6.45 8.57
5140 pens 22 0 2.63 3.19
9673 candles 31 0 3.19 5.85 

and the contents of transaction.txt is (input)

11/29/2010 S 2907 1
11/29/2010 S 9673 4
11/30/2010 A 4321 30
11/31/2010 S 9673 12 
12/01/2010 N 5789 wind_chimes 13.54 17.83
12/02/2010 S 5140 5

Then the contents of thisweek.txt is (output)

1275 greeting_cards 42 0 1.95 2.75
2907 note_cards 29 0 6.47 7.95
4321 calendars 39 0 6.45 8.57
5140 pens 17 30 2.63 3.19
9673 candles 15 30 3.19 5.85 
5789 wind_chimes 0 30 13.54 17.83

the contents of sales.txt is (output)

   DATE      ITEM NO.    ITEM        QTY    PRICE     TOTAL
11/29/2010    2907     note_cards     1      7.95      7.95
11/29/2010    9673     candles        4      5.85     23.40 
11/31/2010    9673     candles       12      5.85     70.20
12/02/2010    5140     pens           5      3.19     15.95
                                            TOTAL    117.50

and the contents of orders.txt is (output)

   DATE      ITEM NO.    ITEM        QTY    PRICE     TOTAL
11/31/2010     9673    candles        30     3.19     95.70
12/01/2010     5789    wind_chimes    30    13.54    406.20
12/02/2010     5140    pens           30     2.63     78.90
                                            TOTAL    580.80

Edited by mike_2000_17: Fixed formatting

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by chode1
0

Please press the code button before you paste your code. Please explain your problem better. What does your program do to the data before it writes it into other files?

0

How can I read in a text file so that I can get any piece of that string of various data types?

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


int main () {

    string line;
    string data[6];
    int datacounter = 0;
    string substr = "";
    ifstream lastfile;
    lastfile.open("lastweek.txt"); 



    ifstream myfile;
    myfile.open ("transaction.txt");

  ofstream thisfile;
  thisfile.open ("thisweek.txt");


      getline(lastfile,line);


  for (int i = 0; i< line.length(); i++)
  {
      if (line.at(i) == ' ')
      {
            data[datacounter] = substr;
            datacounter ++;
            substr = "";
      }
      else
      {
          substr = substr + line.at(i);
      }
  }

  //print outs each substring
  for (int j = 0; j< 6; j++)
  {
      cout << data[j]<<" ";
  }
  cout << endl;

  datacounter = 0;
}

I am to use two input files to create three other output files of specific formatting.

I need to use structs and arrays..I am lost can someone help point me in the right direction?

If the contents of lastweek.txt is (input)

1275 greeting_cards 42 0 1.95 2.75
2907 note_cards 30 0 6.47 7.95
4321 calendars 9 30 6.45 8.57
5140 pens 22 0 2.63 3.19
9673 candles 31 0 3.19 5.85 

and the contents of transaction.txt is (input)

11/29/2010 S 2907 1
11/29/2010 S 9673 4
11/30/2010 A 4321 30
11/31/2010 S 9673 12 
12/01/2010 N 5789 wind_chimes 13.54 17.83
12/02/2010 S 5140 5

Then the contents of thisweek.txt is (output)

1275 greeting_cards 42 0 1.95 2.75
2907 note_cards 29 0 6.47 7.95
4321 calendars 39 0 6.45 8.57
5140 pens 17 30 2.63 3.19
9673 candles 15 30 3.19 5.85 
5789 wind_chimes 0 30 13.54 17.83

the contents of sales.txt is (output)

   DATE      ITEM NO.    ITEM        QTY    PRICE     TOTAL
11/29/2010    2907     note_cards     1      7.95      7.95
11/29/2010    9673     candles        4      5.85     23.40 
11/31/2010    9673     candles       12      5.85     70.20
12/02/2010    5140     pens           5      3.19     15.95
                                            TOTAL    117.50

and the contents of orders.txt is (output)

   DATE      ITEM NO.    ITEM        QTY    PRICE     TOTAL
11/31/2010     9673    candles        30     3.19     95.70
12/01/2010     5789    wind_chimes    30    13.54    406.20
12/02/2010     5140    pens           30     2.63     78.90
                                            TOTAL    580.80

Edited by mike_2000_17: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.