well, i just got into programming, and my professor's a moron.
i'm writing a code that is supposed to return a calendar by filling a 2-d array. this is where the problem comes in. i'm trying to open a file and fill the array with it. can anyone tell me what is wrong with my code, i would greatly appreciate it.
thank you.
p.s. here is a copy of part of the code i am trying to use.
using namespace std;
int FillArray (string a, string b[][6])
{
int x = 0;
ifstream file;
file.open((a+".txt").c_str());
//file.open(a.c_str());
if (!file.is_open())
{
cout << "could not open" << endl;
return -1;
}
@xsemel
If you attach the code, within code brackets,[code.] your program code[/code.],(without the periods) someone here could probably help. Without seeing your code and what makes it not work, we can't help.
Does it not compile? If yes, what error do you get? And on what line(s)?
Does it run and not do what you want? If that's the case, what do you expect it to do, and what does it actually do?
Does it run and crash? If yes, then tell us how you reproduce the crash and if possible tell us what line it crashes on and any error message it spits out when it crashes.
Don't just say "it's broken". That can mean anything. It's much easier for us to solve a problem when we know what the problem is.
it doesnt open the file. it returns -1. there is something with file.open((a+".txt").c_str()); i guess.
this is really just a trial run for a longer code. the real code is supposed to get a month from the user and give them a calender by assigning each line of a .txt file to an array, but it has the same problem. i narrowed the problem down to what you see up there, and i dont really know where to go from here.
there is something with file.open((a+".txt").c_str()); i guess.
Nope. That's fine.
Most likely, either the file name is wrong, or the file is not in the directory it needs to be.
Which directory is the right directory depends on your IDE. With VS it usually is the project directory if you run the program from the IDE. If you're running the executable on its own, then it would be the executable directory.
im pretty sure its supposed to be in the source file folder. i've had several people try to help me, but with no success. i have a file called test.txt. it goes like this
1
2
3
4
5
6
7
8
9
and i want an array that prints basically a 2-d array with these numbers by having the user type in test.(obviously it will be different for real life applications)
i'm sorry if i dont seem all that together, i've been staring at this computer screen for several hours trying to figure out whats wrong.
hardcode the full path to make sure it's finding the file.
file.open("C:/whatever/whatever/test.txt");
test #2
make sure you know which directory it's supposed to look in for relative paths. On windows, you can use GetCurrentDirectory for this:
1 2 3 4 5 6 7 8 9
#include <windows.h>
int main()
{
// for testing...
char curdir[MAX_PATH];
GetCurrentDirectoryA( MAX_PATH, curdir );
cout << "The current directory is: " << curdir;
Whatever that prints is the directory your file needs to be in.
yeah, i have the directory right. hardcoding it didnt work though. i dont really know why. thank you very much though. if any one else has any suggestions, i would love to hear them.
ok, let me just ask. in theory, would this program work? does anyone see anything wrong with the way it is written in terms of what i am trying to do other than the string y instead of the int y thing?
if the 2-d array prints out under this i would basically get a calender. its a little neater in c++ but you get the idea. the first dimension is the days per week. the second is the most lines you can have in one month.
because that's what the professor wanted.lol would you do it as a 1-d array instead? at this point it doesnt matter, i've already submitted it, but i am actually interested in learning c++ and i wish i had a better professor.
i figured, the best way to learn would be to try things and then get help as i go along from people who know what theyre doing.
I too am pretty much a beginner so take any of the following with a grain of salt. I have a basic grasp on the syntax , but the grammar is another story. So to all others who read this keep that in mind. I just registered to offer my $0.02
Although Im not sure why a 2d array was needed for this , the following was the only reason I could come up with , each dimension represents an individual week , rather than an array of ~31 days representing a month.
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
/////////////// Main //////////////////////
string calendar [5][7]= {"sun","mon","tues","wed","thurs","fri","sat"};
string filename = "numbers"; // hardcoded file name
ifstream mfile;
/* cout << "enter name of file to open \n"; // Uncomment this section to be prompted
cin >> filename; // for a file to open.
cin.get();
*/
mfile.open((filename + ".txt").c_str() );
if (!mfile.is_open())
cout << "crash and burn" << "\n";
for (int y = 0; y< 7; y++)
cout << calendar[0][y] << "\t"; // prints the days of the week stored in calendar
cout << "\n\n";
int i = 1;
int j = 0;
while (getline(mfile,calendar[i][j]))
{
cout << calendar[i][j] << "\t";
i++;
j++;
if (i >=4)
i=1;
if (j >=7)
{
cout << "\n"; //if we've printed 7 dates, start a new week.
j= 0; // starts reading dates in at [i][0]
}
}
cout << "\n\n Press enter to exit";
cin.get();
I'm using microsofts visual c++ IDE and this compiles and runs. I'm not sure if its what your trying to achieve or not. but basically prints the headings of the days , then reads in the numbers 1 - 31 from a file , stores them in a 2d array (representing weeks) and prints them to the screen. The file is simply named numbers.txt and is in the project directory with the rest of the projects source files (not the debug).
If its not what you meant, with a little more info on your situation I would be happy to try again from a different angle.
*edit*
Just for clarity I dumped all this in main() , it would be easy enough to create the function. I just chose this way for the sake of simplicity.
well, it turns out i was making a comparison instead of setting something equal. stupid mistake.
but now, there is a whole new problem. for some reason, the .exe file stops working. it is a very interesting problem. i will try your way out. thank you.