![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
|
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).
__________________
Страшнее человека-паука только человек-тапок. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
#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.
__________________
|
|
|
|
|
|
#3 |
|
Newbie
|
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
__________________
Страшнее человека-паука только человек-тапок. |
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
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.
__________________
|
|
|
|
|
|
#5 |
|
Newbie
|
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
__________________
Страшнее человека-паука только человек-тапок. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|