Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 31st, 2004, 2:33 PM   #1
inko9nito
Newbie
 
Join Date: Oct 2004
Location: Wylie, TX
Posts: 11
Rep Power: 0 inko9nito is on a distinguished road
Send a message via AIM to inko9nito
Ok, we're supposed to read in a bunch of entries from the file (right here) and store each type (item number, description, quantity, and price) in 4 parallel arrays. The problem is that the descrition consists of up to 4 words. So how would you read in the 1 descrition so that it's stored in one array location, another description so that it's in the second array location etc.

This is what I have so far but it reads each word separately and stores in a separate array location.

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

const int SIZE = 26;

int main()
{
	ifstream readFile;
	// open the file
	readFile.open("Invent.dat");

	int count = 0;
	string itemNum[SIZE], itemDsrn[SIZE], itemQuant[SIZE], itemPrice[SIZE];

	while(count < SIZE && !readFile.eof())
	{
 readFile >> itemNum[count];
 
 readFile >> itemDsrn[count]; 
 readFile >> itemQuant[count]; 
 readFile >> itemPrice[count];

 cout << itemNum[count] << " " << itemDsrn[count] << " " << itemQuant[count] 
  << " " << itemPrice[count] << endl;
 count++;
	}
	
	readFile.close();

	return 0;
}

The thing is, our teacher didn't teach us any of this crap.
This is due Tuesday and this is not even 1/3rd of the program (there's more to it than just reading the data from the file).
__________________
Страшнее человека-паука только человек-тапок.
inko9nito is offline   Reply With Quote
Old Oct 31st, 2004, 3:31 PM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 6 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

const int SIZE = 26;

int main()
{
ifstream readFile;
readFile.open("Invent.dat");

char itemNum[SIZE][128], itemDsrn[SIZE][128];
int itemQuant[SIZE], itemPrice[SIZE], count = 0, pos = 0;

for(int i=0;i<SIZE * 4;i++) {
  if(readFile.eof()) break;

  if(count == 4) {
    count = 0;
    pos++;
  }

  switch(count) {
    case 0:
      readFile.getline(itemNum[pos], 128, '\t');
      break;
    case 1:
      readFile.getline(itemDsrn[pos], 128, '\t');
      break;
    case 2:
      readFile >> itemQuant[pos];
      break;
    case 3:
      readFile >> itemPrice[pos];
      break;
  }

  count++;
}

readFile.close();

return 0;
}

Cheers.
__________________

tempest is offline   Reply With Quote
Old Oct 31st, 2004, 3:46 PM   #3
inko9nito
Newbie
 
Join Date: Oct 2004
Location: Wylie, TX
Posts: 11
Rep Power: 0 inko9nito is on a distinguished road
Send a message via AIM to inko9nito
Hmm, I'm getting weird output. Why did you use 128? Why did you make itemNum a character array? Please don't get me wrong, I'm not complaining, I'm just curious and confused.

Output I'm getting:
-858993460
-858993460216
216
-858993460
-85899346086
86
-858993460
-858993460106
106
-858993460
-858993460130
130
-858993460
-85899346095
95
-858993460
-858993460435
435
-858993460
-858993460
__________________
Страшнее человека-паука только человек-тапок.
inko9nito is offline   Reply With Quote
Old Oct 31st, 2004, 3:59 PM   #4
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 6 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
My fault for not testing it, i made itemNum a character array on the assumption you wouldnt need to do any mathmatical operations with it. That however can be changed easily.

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

const int SIZE = 26;

int main()
{
ifstream readFile;
readFile.open("Invent.dat");

item itemNum[SIZE];
char itemDsrn[SIZE][128];
int itemQuant[SIZE], itemPrice[SIZE], count = 0, pos = 0;

for(int i=0;i<(SIZE * 4);i++) {
  if(readFile.eof()) break;

  if(count == 4) {
    count = 0;
    pos++;
  }

  switch(count) {
    case 0:
      readFile >> itemNum[pos];
      break;
    case 1:
      readFile.getline(itemDsrn[pos], 128, '\t');
      break;
    case 2:
      readFile >> itemQuant[pos];
      break;
    case 3:
      readFile >> itemPrice[pos];
      break;
  }

  count++;
}

readFile.close();

return 0;
}

To the best of my knowledge that should work, i dont have a C++ compiler to test it with right now so i'll have to wait till tomorrow to try and run it. Sorry, maybe someone with a C++ compiler can help you debug it :blink: :wacko: :ph34r: B) -- Sorry, i like smilies.
__________________

tempest is offline   Reply With Quote
Old Nov 1st, 2004, 11:31 AM   #5
inko9nito
Newbie
 
Join Date: Oct 2004
Location: Wylie, TX
Posts: 11
Rep Power: 0 inko9nito is on a distinguished road
Send a message via AIM to inko9nito
cout << itemNum[0] << itemNum[1] << itemNum[2];

I tried to output first 3 array positinos of itemNum and this is what I got

-858993460-858993460-858993460
__________________
Страшнее человека-паука только человек-тапок.
inko9nito is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:32 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC