#Input file is outputting random numbers for some reason

1 messages · Page 1 of 1 (latest)

little bolt
#

As context, this is for homework. I'm trying to open a file which the user is supposed to enter, I've mostly figured out how to do that. I was trying to test my code to make sure it's working correctly, but for some reason it's outputting a bunch of numbers instead of the expected number. There's no error messages, just the weird number.

`#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream input; //in
ofstream output; //out
int num;
string fileName;
int* point;
/*
cout << "Enter input file name: ";
cin >> fileName;*/

input.open("HW3_input.txt");
input >> num;

cout << num;

input.close();

return 0;

}`

opal windBOT
#

<@&987246683568103514> please have a look, thanks.

haughty quest
#

what does "HW3_input.txt" contain?

little bolt
#

I'm trying to output the first number/25

haughty quest
little bolt
#

Yes, one moment

#

25
103 13 59 1 3
3 9 413 952 236
31 924 45 215 913
315 12 94 165 324
164 23 54 126 100

haughty quest
#

have you tried using your IDE's debugger to see how the variable changes(i think i found the solution btw)

little bolt
#

I'm using Visual Studio, haven't quite figured out how to access the debugger with it yet

haughty quest
#

from what i understand, you need to use the absolute path and not just the file name

#

idk how to use a relative path

#

(I am a beginner in C++ too btw)

little bolt
#

What's an absolute path?

#

Or a relative path?

haughty quest
#

like D:\C++ projects\Hello World!\HW3_input.txt is the absolute path to HW3_input.txt,(on my machine, Hello World! is my testing/learning folder)

#

but if you are already are in D:\C++ projects\Hello World!\, then HW3_input.txt is in \HW3_input.txt relatively to your current path, so it is the relative path

little bolt
#

I'm supposed to make it so my professor can use a different file and it'll still work without issue, if that makes sense

haughty quest
#

I think that is as much as I can help, as I dont know much about file I/O in C++

little bolt
#

It's alright, thanks for trying