When i was looking at a debugger to see why my code wasnt working it was saying that line 117 resulted in a error because it was accessing "invalid memory", but idk how thats possible, and why line 279 isnt called when an invalid id is written.
#Trying to debug my code
167 messages · Page 1 of 1 (latest)
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.
What's the error you get?
the actual code has no error
its a logic error
of an invalid id terminate the program
instead of doing line 279
also now I've chnaged Terminate to this to fix the decimal problem for the price:
void InventorySystem::Terminate(){
std::ofstream fout(InventorySystem::DEFAULT_OUTPUT_FILE_NAME);
std::string name;
int id = 0;
int quantity = 0;
double price = 0.0;
// Check if the file is opened successfully
if(!fout){
std::cout << "Error: Unable to open file" << std::endl;
return;
}
for(auto i = 0; i < product_count; i++){
id = product_list[i]->get_item_id();
name = static_cast<Product*>(product_list[i])->get_name();
quantity = static_cast<Product*>(product_list[i])->get_quantity();
price = static_cast<Product*>(product_list[i])->get_price();
fout << id << ";" << name << ";" << quantity << ";" << std::fixed << std::setprecision(2) << price << std::endl;
}
fout.close();
}
but now nothing is in my default_out.txt
@blissful shard 🙏🏻
I guess product_count is zero then
but if my input file works correctly how cna that be
Idk. Time to debug
i just realized does that for loop run 512 x 8 times...?
Also why are you using raw arrays, new, instead of just a vector<Product>?
Dynamic arrays aren't even in standard cpp
i think i have to use that cuz we learned that recently
And yeah, you read one product, then add it DEFAULT ARRAY SIZE times
oh no cuz of "break;"
wdym
Sure they are new T[X]
if anyone figures out any of the issues please @ me
I thought you were looking with the debugger
That's the thing to do
i tried but idk what im looking for
this is all i managed to come up with
Firstly if the code even goes to the file writing code
Then what is does when it gets there. And once you see it write something, then look at the content
it was now it isnt xD
Well put break points where it should and see what's happening
kk
wat does this tell me tho
Uhm, you're looking at the wrong file or the data has not been flushed, are the only things I can think of atm
Or when you're looking at the file you have overwritten the data with an empty file
this is the only time i use fout
so idk where id be setting it to blank
unless my fout just does nothing maybe?
Change your default output filename to be an absolute path (and somewhere simple) then check that file
I've just noticed you have an endl in one of your lines, so that forces a flush of the buffer when it's called
idk what this means ngl
so delete endl in fout?
No, you want it
What is the value of your default output file name constant?
Change it to something like /home/me/testing/output.txt
(and create the dirs necessary)
If you delete the file, does it come back when you run your app
Then it's not being written. Or it's not being written there
how can i check where it goes
Change the variable that control the file name like I said
Then you know exactly where it goes. If you don't give an absolute path then it's relative to the app
how would i write a path to put into my documents
(so it should be next to your app from what I can see)
Open my docs then copy the address
that?
const std::string InventorySystem::DEFAULT_OUTPUT_FILE_NAME = "Documents/default_output.txt";
?
My docs should be under like c: users/name/...
Abs part needs to start with a drive letter on windows
C:\Users\kingp\OneDrive\Documents\Foothill_CS2B\default_output.txt
Yeah
But when you put it in " you need to double every \
I generally wouldn't dev inside OneDrive or write stuff there from code tho
idek what OneDrive is xd, where should i set it to
Anywhere normal. Ie not program files etc
what is normal xD
Just c: is fine for testing
Sure
kk
Are you sure you're running the same thing when you're debugging Vs when you're running these tests?
So how are you running it now?
Should be fine if you've set up ide right. So what happens if you run the app from terminal?
how can i run from terminal
Open cmd where your app is
Go to where your app is then run it
little bit more specific please
Sorry I don't have any more time
no worries
When you debug it shows writing to file. When you run your app it doesn't make a file. So something is not the same between them
You might want to check that what you think "run" is is actually run, not just build
going to where i coded it, i clikc the same thing i always click "Compile with default flags and run with default arguments"
resending so u dont have to scroll
the default files are empty
it wont let me send empty txt things
(also i have no idea what:
wchar_t currentDir[MAX_PATH];
// Get the current directory
if (GetCurrentDirectoryW(MAX_PATH, currentDir) != 0) {
// Convert wide-character string to narrow-character string for printing
char narrowDir[MAX_PATH];
wcstombs(narrowDir, currentDir, MAX_PATH);
std::cout << "Current directory: " << narrowDir << std::endl;
} else {
std::cerr << "Error getting current directory" << std::endl;
return 1;
}
is, this is what chatgpt told me to do
xD
do u have any idea how to grant permission @desert ivy ?
where are you opening the file?
im just running it on vs code if thats what u mean
you're having problem opening a file right
so where is that happening?
is something currently using that file?
maybe you're still running the program somewhere?
i dont know where else id run it, here is the folder which the things are loctaed if that makes more sense
and in vscode
this is what the files are
wait a minute why is this in the onedrive folder??
try moving it out of onedrive
sorry for the late reply
where should it be?
it was automatically there
anywhere that would be stored on your actual computer
im so confused everything is one drive cuz even if i click docuemnts from here i get to same place
click the drive then navigate to somewhere to store the files (e.g. under your user)
why are you trying to move the Users folder now ???
i thought thats what u meant when u said under your user 😭
just go into the users folder not move it
then create your files in the folder for your user
yeah
ok done
but now its in multiple places, how do i make my vs code use that one
erm now less code ran than it did before 😭
!solved