does anyone know how to....

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.

#include <iostream>
#include <string>
#include <fstream>

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;
}

while (!file.eof ())
{
getline(file, b[x][6]);
x++;
}
return x;
}

int main()
{
string cal[7][6];
string y;
string m;
cin >> m;
y = FillArray (m, cal);
cout << y << endl;

return 0;
}
Last edited on
@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.
yeah, sorry, i just fixed that a bit ago
y = FillArray (m, cal);


FillArray returns an int.
y is a string.

You cannot assign an int to a string.


Also, tell us what's wrong with it.

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.
even when i use int y it wont open the file(i didnt think that would change it, but thanks for pointing that out)
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.
Last edited on
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.
test #1

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.
Last edited on
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.
Last edited on
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?
I just noticed this:

getline(file, b[x][6]);

b[x][6] is out of bounds. Then 2nd dimension is only [6] wide, so [5] would be the last element. [6] is out of bounds.

Also, if the file is too large (longer than 7 lines), you will corrupt memory because b[x] will also go out of bounds.

I don't really see why you have a 2D array here. What is the second dimension for?
this is a test for another program. what im really trying to do is make a calender, so i need a 2-d array to print out under

cout << "sun mon tue wen thu fri sat" << endl;
cout << " --- ----- --- ----- --- -- ----" << endl;

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.
then, no, your code will not do what you want because you are reading into your array as if it were a 1D array.

But really, you shouldn't use a 2D array for this. Just because the data is displayed in a grid-type form doesn't mean it should be stored as one.

I'm also a little puzzled why you'd need to read these numbers from a file.
Last edited on
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.
test #3:
Go to the program directory. Execute from there.

test #4:
Check that you have permission to open the file
$ ls -l

test #5:
Pass the filename as an argument to your program, so you can use tab completion. (don't add the extension)
1
2
3
4
int main(int argc, char **argv){
  //...
  FillArray(argv[1], calc);
}

$ ./program input.txt

test #6:
Use the standard input to test your program
1
2
3
//ifstream file;
istream &file = cin; //change the input to be the standard input
//if( not file.is_open() )  

Then execute your program $ ./program.bin < input.txt

You shouldn't loop on eof, it will be set after you try to read it. (one step too late)
Use while( getline(file, b[x]) ) instead
Last edited on
thank you, thank you very much. i will try all of this out and see what happens.
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

#include <iostream>
#include <string>
#include <fstream>
using namespace 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.

Last edited on
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.
Topic archived. No new replies allowed.