#Need help with function looking into file and extracting data

77 messages · Page 1 of 1 (latest)

steep wren
#

Hello everyone thanks for your time and guidance, I am having issues with my code working correctly. The code will be included for reference but my main issue is within the function defition ```void read_file(Cars cars[], Cars validCars[], Cars errorCars[]) {
// Function to read from input file
ifstream inputFile("input.txt");
string line;
int carIndex = 0;
int validIndex = 0;
int errorIndex = 0;
while (getline(inputFile, line)) {
stringstream ss(line);
string itemID, itemName, manufacturer;
int quantity;
if (getline(ss, itemID, ',') && getline(ss, itemName, ',') && getline(ss, manufacturer, ',') && ss >> quantity) {
Cars car;
car.CarID = itemID;
car.CarModel = itemName;
car.Manufacturer = manufacturer;
car.Amount_on_Hand = quantity;

        if (car.isValidRecord()) {
            validCars[validIndex++] = car;
        } else {
            errorCars[errorIndex++] = car;
        }

        cars[carIndex++] = car; // Add the car record to the cars array
    }
}
inputFile.close();

} its supposed to be able to look at records from an input.txt file ive formatted as follows : 129XYZ6 Explorer Ford 8 24000.00
134ABC5 Civic Honda 15 17000.00
117PQR9 Malibu Chevrolet 26 20000.00
128LMN3 ModelS Tesla 18 40000.00
122RST5 Outback Subaru 25 22000.00
139UVW2 F-150 Ford 12 28000.00
118GHI4 Prius Toyota 30 19000.00``` but whenever i run the program it mostly just returns 0's. I add a link to the full code just in case more info is needed : https://sourceb.in/Wrvr04hKCK

analog spindleBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

inland cloak
#

mb

#

but yea you didnt really

#

void read_file(Cars cars[]); and void read_file(Cars cars[], Cars validCars[], Cars errorCars[]) have different signatures

steep wren
#

I thoguht i had declared them, at the top where i have the void functions above main. did i do that wrong ?

inland cloak
#

Yes, but with a wrong signature

steep wren
#

So for it to work would it need to match ? as in both with the long void one ?

#

void read_file(Cars cars[], Cars validCars[], Cars errorCars[]) as in this one

inland cloak
#
void read_file(Cars cars[]);
void print_file(Cars cars[]);

void read_file(Cars cars[]) and void print_file(Cars cars[]) haven't been defined

steep wren
#

i chaged the void read_file to match the signature of the bottom function but it continues to print 0's and oh maybe the lack of print_file function is messing with things ?

inland cloak
#

You don't call read_file I think

#

You also don't take the valid cars to your menu function.

steep wren
#

I did the following for the read_file function. But it cntinues to return 0's. could it be the way i have the text file ?

#
  //Function to print file 
  print_table_header();
  print_record_table(cars);
}```
inland cloak
#

Your if inside read_file causes some issues i think :D

steep wren
#

is it because of the names that i chose ? if (getline(ss, itemID, ',') && getline(ss, itemName, ',') && getline(ss, manufacturer, ',') && ss >> quantity) {

#

should i have named them the same thing as i had them on the class as ?

inland cloak
#

No, you are reading until ',', but there are none in that file u sent

steep wren
#

oh so should it be formatted ``` 129XYZ6 Explorer Ford 8 24000.00,

134ABC5 Civic Honda 15 17000.00,
117PQR9 Malibu Chevrolet 26 20000.00,
128LMN3 ModelS Tesla 18 40000.00,
122RST5 Outback Subaru 25 22000.00,
139UVW2 F-150 Ford 12 28000.00,
118GHI4 Prius Toyota 30 19000.00, ```

#

or a coma per space

inland cloak
#

getline(ss, itemID, ',') means it will read into itemID until it finds a ','

#

and it will read the ',' too

steep wren
#

ohhhhhh

inland cloak
#

well im not sure that it will read the ',' but the next one will read it for sure

steep wren
#

makes fucking sense, wait so in this case should i do a coma until the end of the line of the first record or should i coma every piece of data per record line?

inland cloak
#

i mean, the ss>>quantity will read the previous comma

inland cloak
#

getline will either read until the separator or the end of the line

steep wren
#

should i then write in the text file \t or literally just add a tab space between each

inland cloak
#

add a tab

#

with the tab key on your keyboard

#

:D

#

A tab is a character just like 'a' or 'c', it just shows as a gap in the text

steep wren
#

i tried adding a tab but it still didnt really do much, i did ``` 123CBD5, ModelX, Tesla, 30, 60000.00,

124JKL6, i8, BMW, 5, 18000.00,

125ASD7, Civic, Honda, 20, 17500.00,

126QWE8, Pilot, Honda, 35, 15000.00 ,``` and tab key at the beggining of every line. could you give me an example of what you mean ?
#

thank you so much for the help btw and your time as well !

inland cloak
#

if you separate fields in the input file with commas that is okay too

#
123CBD5, ModelX, Tesla, 30, 60000.00,
124JKL6, i8, BMW, 5, 18000.00,
125ASD7, Civic, Honda, 20, 17500.00,
126QWE8, Pilot, Honda, 35, 15000.00,
#

with this it works for me, but somewhere you overindex the array when printing their content

#

Is this an assignment? If so, are you allowed to use std::vector ?

steep wren
#

no they dont want me to use STL and vectors. its a little restrictive of an assigment

inland cloak
#

ah okay

#

so when you print, you overindex the array

steep wren
#

Thats weird for whatever reason its only still printing 0's on my end

inland cloak
#

in void print_line(Cars cars[]) you always print out 10 records. That means if the size of cars[] is less than 10 you overindex

steep wren
#

ohhh so because i only have less than 10 itll always print 0 ?

inland cloak
#

no.

steep wren
#

will it overwrite too if its more than 10?

inland cloak
#

your read function wasnt correct either

steep wren
#

sorry i meant overindex

inland cloak
#
  • your input file wasnt consistent when separating data so you cannot read the data
#

i dont know what compiler you use but im surprised it prints 0s and doesnt segfault/print random values

steep wren
#

its cause we have to submit through onlineGDB and we are asked to code on replit or any other IDE

inland cloak
#

@inland cloak you can look at this in a diff viewer and see what i changed

#

the code still has errors but

analog spindleBOT
#
nevemlaci ツ
We Don't Do Your Homework

Welcome to Together C & C++ :wave:

We won't do your homework for you (#rules) but we are happy to help you learn and point you in the right direction.

Please send what you have so far and ask a specific question.

inland cloak
#

also print_record and print_line are the same

#

you should pack all 3 arrays you use into a struct and also store the size of each of the arrays in that struct

steep wren
#

Hey thanks so much for everything im going to try and piece the rest together. But its been truly helpful and thanks for taking the time to explain the concepts and what i had written was actually doing !

inland cloak
#

something like this:

struct carArrays{
  Cars cars[100]; // 100 is the max the array can hold
  size_t cars_size = 0;
  //... same with the other 2 arrays
}
#

then you can pass this struct anywhere by reference

steep wren
#

🙏🏻 thank you so so much

inland cloak
#

no problem

#

pro tip: onlinegdb has a debugger where you can place breakpoints and run your code line-by-line

#

also struct and classes are your friends, use them

steep wren
#

Really ? Im still getting used to the UI of online GDB

#

OH dude im blid af i see the buttome that says debug

#

thank you so much again

inland cloak
#

no problem :D

analog spindleBOT
#

@steep wren Has your question been resolved? If so, type !solved :)