#Trying to debug my code

167 messages · Page 1 of 1 (latest)

misty urchin
woeful slateBOT
#

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.

tight perch
#

What's the error you get?

misty urchin
#

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

misty urchin
#

@blissful shard 🙏🏻

vast phoenix
#

I guess product_count is zero then

misty urchin
vast phoenix
#

Idk. Time to debug

misty urchin
#

i just realized does that for loop run 512 x 8 times...?

vast phoenix
#

Also why are you using raw arrays, new, instead of just a vector<Product>?

misty urchin
#

cant use vectors

#

was told to use array/dynamic arrays

vast phoenix
#

Dynamic arrays aren't even in standard cpp

misty urchin
#

i think i have to use that cuz we learned that recently

vast phoenix
#

And yeah, you read one product, then add it DEFAULT ARRAY SIZE times

misty urchin
vast phoenix
#

Oh, I missed your if nullptr/break.

#

Should be ok

tight perch
misty urchin
#

if anyone figures out any of the issues please @ me

vast phoenix
#

That's the thing to do

misty urchin
#

i tried but idk what im looking for

misty urchin
vast phoenix
#

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

misty urchin
vast phoenix
#

Well put break points where it should and see what's happening

misty urchin
#

kk

misty urchin
vast phoenix
#

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

misty urchin
#

this is the only time i use fout

#

so idk where id be setting it to blank

#

unless my fout just does nothing maybe?

vast phoenix
#

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

vast phoenix
#

No, you want it

vast phoenix
misty urchin
vast phoenix
#

Change it to something like /home/me/testing/output.txt

#

(and create the dirs necessary)

misty urchin
#

like copy that

#

and replace it?

vast phoenix
#

If you delete the file, does it come back when you run your app

misty urchin
#

lemme try

#

no

#

it doenst

vast phoenix
#

Then it's not being written. Or it's not being written there

misty urchin
#

how can i check where it goes

vast phoenix
#

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

misty urchin
#

how would i write a path to put into my documents

vast phoenix
#

(so it should be next to your app from what I can see)

#

Open my docs then copy the address

misty urchin
#

that?

#

const std::string InventorySystem::DEFAULT_OUTPUT_FILE_NAME = "Documents/default_output.txt";

#

?

vast phoenix
#

Damn, is that what happens?!

#

Is that the group

#

Or the directory

misty urchin
#

when i clikc

#

there

#

it says Documents

vast phoenix
#

My docs should be under like c: users/name/...

misty urchin
#

C:\Users\kingp\OneDrive\Documents\Foothill_CS2B

#

like that?

vast phoenix
#

Abs part needs to start with a drive letter on windows

misty urchin
#

C:\Users\kingp\OneDrive\Documents\Foothill_CS2B\default_output.txt

vast phoenix
#

Yeah

misty urchin
#

so that would be the whole thing

#

wont let me

vast phoenix
#

But when you put it in " you need to double every \

misty urchin
#

oh

#

ok

#

yea its not in there

#

when i run

vast phoenix
#

I generally wouldn't dev inside OneDrive or write stuff there from code tho

misty urchin
vast phoenix
#

Anywhere normal. Ie not program files etc

misty urchin
#

what is normal xD

vast phoenix
#

Just c: is fine for testing

misty urchin
#

"C:\Users\kingp\default_output.txt";

#

so that

vast phoenix
#

Sure

misty urchin
#

kk

vast phoenix
#

Are you sure you're running the same thing when you're debugging Vs when you're running these tests?

misty urchin
#

same exact code

#

besides whatever we chnaged

#

again its nowhere to be seen

vast phoenix
#

So how are you running it now?

misty urchin
#

by just clicking run?

#

is that not what im meant to do 😭

vast phoenix
#

Should be fine if you've set up ide right. So what happens if you run the app from terminal?

misty urchin
#

how can i run from terminal

vast phoenix
#

Open cmd where your app is

misty urchin
#

i have this open

#

now what

vast phoenix
#

Go to where your app is then run it

misty urchin
vast phoenix
#

Sorry I don't have any more time

misty urchin
#

no worries

vast phoenix
#

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

misty urchin
#

going to where i coded it, i clikc the same thing i always click "Compile with default flags and run with default arguments"

misty urchin
#

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

misty urchin
#

do u have any idea how to grant permission @desert ivy ?

desert ivy
#

where are you opening the file?

misty urchin
#

im just running it on vs code if thats what u mean

desert ivy
#

so where is that happening?

misty urchin
#

yea

#

this is exactly

#

what it says

desert ivy
#

maybe you're still running the program somewhere?

misty urchin
#

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

desert ivy
#

try moving it out of onedrive

misty urchin
#

where should it be?

#

it was automatically there

desert ivy
misty urchin
desert ivy
misty urchin
#

this "Users", i got this when i tried

#

do i just lcick continue

desert ivy
misty urchin
#

i thought thats what u meant when u said under your user 😭

desert ivy
#

just go into the users folder not move it

misty urchin
#

oh

#

xd

desert ivy
#

then create your files in the folder for your user

misty urchin
#

so put it in there

#

the highlighted one

desert ivy
misty urchin
#

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 😭

misty urchin
#

!solved