Need help.

While this is for an assignment, I currently do not need help with it.
Right now I am asking the user to enter a filename for a txt file.

Here is the current code:
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
//Game of Life
//Dylan Metz
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>

using namespace std;

//functions
char GetName();
int main()
{
const int SIZE=50;
char name[SIZE];

//Get file name
strcpy(name,GetName());




return 0;
}

char GetName ()
{
	const int SIZE=50;
	char file[SIZE];
	ifstream myfile;
	string filename;
	cout<<"Enter the filename: \n";
	cin>>filename;
	myfile.open (filename.c_str());
	myfile>>file;
	
	return file;
}


dev-c++ is giving me errors.
18 22 L:\SCHOOL\ Spring 2012\COMSC-048\Game Of Life.cpp [Error] invalid conversion from 'char' to 'const char*' [-fpermissive]
strcpy(name,GetName());

strcpy takes two parameters; both must be of type char*.

GetName claims to returns a char. A char is not char*.

As it is, GetName is actually a mess, as it returns file which is an array of char, and will be passed back as a char*, but you've written the prototype claiming it will return a char.

I see you're using Dev-c++. If you're using version 4.9.9.2 or earlier, that's a terrible choice.
Last edited on
strcpy(name,GetName());

strcpy takes two parameters; both must be of type char*.

GetName claims to returns a char. A char is not char*.

As it is, GetName is actually a mess, as it returns file which is an array of char, and will be passed back as a char*, but you've written the prototype claiming it will return a char.

I see you're using Dev-c++. If you're using version 4.9.9.2 or earlier, that's a terrible choice.


Is this better?
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
//Game of Life
//Dylan Metz
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>

using namespace std;

//functions
char* GetName();
int main()
{
char g; //stops terminal window from quiting
const int SIZE=50;
char name[SIZE];

//Get file name
strcpy(name,GetName());
cout<<name;


cin>>g;

return 0;
}

char* GetName ()
{
	const int SIZE=50;
	char file[SIZE];
	ifstream myfile;
	string filename;
	cout<<"Enter the filename: \n";
	cin>>filename;
	myfile.open (filename.c_str());
	myfile>>file;
	
	return file;
}



Also I am using a portable version of dev-c++ which has been upload in December 2011 to 5.1

Also how would I do this with 2d arrays?
Last edited on
Is this better?

It's better, although you're still using strcpy, which is not great, and you're using proper C++ strings but converting them into arrays of char. Why not do this instead?

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
//Game of Life
//Dylan Metz
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//functions
string GetName();

int main()
{
string g; //stops terminal window from quiting
string name = GetName();
cout<<name;
cin>>g;
return 0;
}

string GetName ()
{
	string filename;
	cout<<"Enter the filename: \n";
	cin>>filename;
	return filename;
}


Also how would I do this with 2d arrays?


http://www.cplusplus.com/articles/NAUq5Di1/
Last edited on
Need some help:
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
//Game of Life
//Dylan Metz
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>

using namespace std;

//functions
char* GetName();
int main()
{
char g; //stops terminal window from quiting
const int ROWS=12;
const int COLS=30;
char name[ROWS][COLS];

//Get file name
strcpy(name,GetName());
cout<<name;


cin>>g;

return 0;
}

char* GetName ()
{
	const int ROWS=12;
    const int COLS=30;
	char file[ROWS][COLS];
	ifstream myfile;
	string filename;
	cout<<"Enter the filename: \n";
	cin>>filename;
	myfile.open (filename.c_str());
	myfile>>file;
	
	return file;
}


(2) The initial configuration will be read in from a file, which will be a 12 by 30 two-dimensional array of characters. The game board will be surrounded by all O's.
Last edited on
Seriously, why the insistence in your getname fnuction with taking C++ strings, which are easy and safe, and going out of your way to to them into unsafe, difficult arrays of char?

Need some help:

With what? Reading in?

1
2
3
4
5
while (notFinishedReadingEverythingIn)
{
  myfile >> file;
  file++;
}


That will fill the 2D array, if you read in 12*30 chars.

Unfortunately, because you're creating the 2D array as a local variable in the function GetName, when that function ends all that array will be reused for other things and you'll be left with a pointer that points at whatever has been written over the array.
For this assignment we need to have at least three functions.

Also c-strings are covered in the chapter of the book we are using for my c++ class.

Here is the complete assignment:


Conway's Game of Life

For this assignment your are to write a program, that plays Conway's game of Life. See the Wikipedia definition, if
you have never played the game: http://en.wikipedia.org/wiki/Conway's_Game_of_Life.

Here is how our implementation will work:
(1) The program will ask the user to enter a filename from the console. (Just like the last assignment).
(2) The initial configuration will be read in from a file, which will be a 12 by 30 two-dimensional array of characters. The game board will be surrounded by all O's.
(3) The game will be played on 10 by 28 two-dimensional array. (Can you guess why?). A period ('.') will represent a dead cell and an 'X' will represent a live cell.

You will be severely penalized if your program does not have at least three functions.


Also the class uses HyperGrade.
Last edited on
Also c-strings are covered in the chapter of the book we are using for my c++ class.

Perhaps you could rise above it and deliver superior code.

If you're going to implement Conway's Game, you're going to end up with more than three functions. Don't worry about that.

Anyway, you've got enough now to open and read the data file.

Thanks for the help. I'll see what I can come up with and post if I run into any difficult problems.
What's wrong with Dev C++ v4.9.9.2?
I believe its outdated
What other good editors would you recommend? I like Dev C++ because it's so "light" and how easy it is to compile and run my code. I've tried Visual Studio but it's a bit of a pain.
I have been using this for about 2 weeks and so far its been working okay.

http://www.portablefreeware.com/?id=417
Here's the code:
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
//Game Of Life Assignment
//Dylan Metz
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>

using namespace std;

int main()
{
	
const int ROWS=12;
const int COLS=30;
char file[ROWS][COLS];

ifstream myfile;
string filename;
cout<<"Enter the filename: \n";
cin>>filename;
myfile.open (filename.c_str());
myfile>>file;
cout<<file;

return 0;
}


I am getting errors.
1
2
3
4
5
6
7
8
9
10
char* GetName ()
{
	const int ROWS=12;
	const int COLS=30;
	char file[ROWS][COLS];

        // ...       

	return file;
}


Ignoring for a second that your code doesn't even compile, the scope of file in the snippet above is limited to the GetName() function. When you return a pointer to it, you're returning a pointer to memory you have no business accessing any more. Derefrencing that pointer outside of GetName results in undefined behavior. As far as C++ is concerned, your computer could blow up. This is a very bad thing to do.

What's more, your function attempts to do a lot more than the name implies, and returns a pointer to something that doesn't remotely resemble a name. You should name your functions appropriately. Your variable name in function main is obviously not meant to hold a name as it is a two dimensional array. It looks more like it's meant to be the grid the game plays out on. If it is, it should have a name that indicates what it represents.

I hate to present the criticism without also presenting an alternative so maybe the following will get you on the right path:

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
51
52
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>

using namespace std;

const int config_rows = 12 ;
const int config_cols = 30 ;

typedef char config_type[config_rows][config_cols] ; 


//functions
string GetString(char const* prompt) ;
bool ReadConfig(const string& fname, config_type& c);

int main()
{
	config_type initial_config = {0} ;

	string file_name = GetString( "Enter file name:") ;

	if ( !ReadConfigFrom(file_name, initial_config) )
	{
		cerr << "Unable to read initial configuration.\n" ;
		return 0;
	}

	// do more stuff...
	return 0;
}

// prompts a user for line oriented input and returns the result.
string GetString( char const* prompt )
{
	cout << prompt << endl ;

	string input ;
	getline(cin, input) ;
	return input ;
}


// returns true if the read was successful
// fname is the name of the file to read the configuration
// from, cfg is where we're storing the configuration.
bool ReadConfig( const string& fname, config_type& cfg)
{
       // do work here!

}










Last edited on
I've installed the version you mentioned but it has so far been AWFUL. It took about 7 tries to complete the "first time setup" without getting an error. Now, it won't directly open any .cpp file, I have to have Dev C++ open to be able to open any files. It's all been a pain, I think I may just switch back.

Edit: Nope, can't even open Dev C++ anymore.
Last edited on
there's also code:blocks.
Had a look at that too. I prefer the "light" feel of Dev C++ though.
Topic archived. No new replies allowed.