Need a little help
Mar 2, 2012 at 7:17pm UTC
For a past assignment I was asked to allow the user to input the name of a file.
Here was part of the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
void MakeArray(string filename);
int main()
{
string filename; //Name of the file
cout<<"Enter the filename: \n" ;
cin>>filename;
MakeArray(filename);
return 0;
}
void MakeArray(string filename)
{
ifstream myfile;
myfile.open (filename.c_str());
//took out the code that did not relate to opening the file
myfile.close();
}
I am wondering how you would go about telling the user that the file does not exit in the directory of the .exe file?
Last edited on Mar 2, 2012 at 7:17pm UTC
Mar 2, 2012 at 7:23pm UTC
1 2 3 4
ifstream myfile(filename.c_str());
if (!myfile)
cout << filename.c_str() << " could not be opened." << endl;
Mar 2, 2012 at 7:34pm UTC
Thanks!
Mar 3, 2012 at 1:59am UTC
EDIT:
Here is the full 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//Global Variables
const int ROWS = 12;
const int COLS = 30;
const int BOARD_ROWS(10);
const int BOARD_COLS(28);
const char LIVE = 'X' ; //life cells
const char DEAD = '.' ; //dead cells
char NewBoard[ROWS][COLS];
//functions
void MakeArray(string filename, char board[][COLS]);
void GameBoard(char board[][COLS]);
void SwitchBoard (char board[][COLS]);
void NextState(char board[][COLS]);
int main()
{
char board [ROWS][COLS];
string filename; //Name of the file
cout<<"Enter the filename: \n" ;
cin>>filename;
cout<<endl;
//call functions
MakeArray(filename, board);
//start loop
for (int l = 0; l<10; l++)
{
NextState(board);
GameBoard(board);
SwitchBoard(board);
}
//end loop
//stop terminal window from quitting after programs ends
char q;
cin >> q;
return 0;
}
void MakeArray(string filename, char board[][COLS])
{
ifstream myfile;
myfile.open (filename.c_str());
if (!myfile)
{
cout << filename.c_str() << " File not found" << endl;
}
else
{
for (int r=0; r<ROWS; r++)
{
for (int c=0; c<COLS; c++)
{
myfile>>board[r][c];
}
}
myfile.close();
}
}
void GameBoard (char board[][COLS])
{
for (int r=1; r<=ROWS-2; r++)
{
for (int c=1; c<=COLS-2; c++)
{
cout<<board[r][c];
}
cout<<endl;
}
cout<<endl;
}
void NextState (char board[][COLS])
{
for (int r=0; r<ROWS; r++)
{
for (int c=0; c<COLS; c++)
{
int LiveCnt=0;
if (board[r-1][c-1]==LIVE)
{
LiveCnt++;
}
if (board[r-1][c]==LIVE)
{
LiveCnt++;
}
if (board[r-1][c+1]==LIVE)
{
LiveCnt++;
}
if (board[r][c-1]==LIVE)
{
LiveCnt++;
}
if (board[r][c+1]==LIVE)
{
LiveCnt++;
}
if (board[r+1][c-1]==LIVE)
{
LiveCnt++;
}
if (board[r+1][c+1]==LIVE)
{
LiveCnt++;
}
/*/
Rules:
1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
2. Any live cell with two or three live neighbours lives on to the next generation.
3. Any live cell with more than three live neighbours dies, as if by overcrowding.
4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
/*/
if (board[r][c] == LIVE && LiveCnt < 2) //rule 1
{
NewBoard[r][c]=DEAD;
}
else if (board[r][c]==LIVE && (LiveCnt==2 || LiveCnt==3))//rule 2
{
NewBoard[r][c]=LIVE;
}
else if (board[r][c]==LIVE && LiveCnt>3 ) //rule 3
{
NewBoard[r][c]=DEAD;
}
else if (board[r][c]==DEAD && LiveCnt==3) //rule 4
{
NewBoard[r][c]=LIVE;
}
}
}
}
void SwitchBoard(char board[][COLS])
{
for (int r=0; r<ROWS; r++)
{
for (int c=0; c<COLS; c++)
{
board[r][c]==NewBoard[r][c];
}
}
}
When I enter a wrong filename I get a bunch of weird symbols and messed of code in output?
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
Enter the filename:
fibvjhsfib.txt
fibvjhsfib.txt File not found
(↨G ¶ƒG h↕G ¶ƒG ▄²( ∞²( h↕
(↨G ¶ƒG ♠ ♠ ► ☺
@ l╠F pFG ♦FG (■( H'
H' = h■( Ç⌠âv = ë⌠âv╛Z┘D
X'= H' = z·EwR►Hw $
H'= H' = ¿■( Ç⌠âv = ë⌠âv
┘D¶ \'= H' = z·EwR►Hw $
ÿ■( ♦ >╖äù¿■( ÷âv
╚╕F ╝■( Bñâv¿☻ìvⁿ■( ╥⌠â
Ä◄ävb◄äv*Z┘D ╚╕F ►:A
(↨G ¶ƒG h↕G ¶ƒG ▄²( ∞²( h↕
(↨G ¶ƒG ♠ ♠ ► ☺
@ l╠F pFG ♦FG (■( H'
H' = h■( Ç⌠âv = ë⌠âv╛Z┘D
X'= H' = z·EwR►Hw $
H'= H' = ¿■( Ç⌠âv = ë⌠âv
┘D¶ \'= H' = z·EwR►Hw $
ÿ■( ♦ >╖äù¿■( ÷âv
╚╕F ╝■( Bñâv¿☻ìvⁿ■( ╥⌠â
Ä◄ävb◄äv*Z┘D ╚╕F ►:A
(↨G ¶ƒG h↕G ¶ƒG ▄²( ∞²( h↕
(↨G ¶ƒG ♠ ♠ ► ☺
@ l╠F pFG ♦FG (■( H'
H' = h■( Ç⌠âv = ë⌠âv╛Z┘D
X'= H' = z·EwR►Hw $
H'= H' = ¿■( Ç⌠âv = ë⌠âv
┘D¶ \'= H' = z·EwR►Hw $
ÿ■( ♦ >╖äù¿■( ÷âv
╚╕F ╝■( Bñâv¿☻ìvⁿ■( ╥⌠â
Ä◄ävb◄äv*Z┘D ╚╕F ►:A
(↨G ¶ƒG h↕G ¶ƒG ▄²( ∞²( h↕
(↨G ¶ƒG ♠ ♠ ► ☺
@ l╠F pFG ♦FG (■( H'
H' = h■( Ç⌠âv = ë⌠âv╛Z┘D
X'= H' = z·EwR►Hw $
H'= H' = ¿■( Ç⌠âv = ë⌠âv
┘D¶ \'= H' = z·EwR►Hw $
ÿ■( ♦ >╖äù¿■( ÷âv
╚╕F ╝■( Bñâv¿☻ìvⁿ■( ╥⌠â
Ä◄ävb◄äv*Z┘D ╚╕F ►:A
(↨G ¶ƒG h↕G ¶ƒG ▄²( ∞²( h↕
(↨G ¶ƒG ♠ ♠ ► ☺
@ l╠F pFG ♦FG (■( H'
H' = h■( Ç⌠âv = ë⌠âv╛Z┘D
X'= H' = z·EwR►Hw $
H'= H' = ¿■( Ç⌠âv = ë⌠âv
┘D¶ \'= H' = z·EwR►Hw $
ÿ■( ♦ >╖äù¿■( ÷âv
╚╕F ╝■( Bñâv¿☻ìvⁿ■( ╥⌠â
Ä◄ävb◄äv*Z┘D ╚╕F ►:A
(↨G ¶ƒG h↕G ¶ƒG ▄²( ∞²( h↕
(↨G ¶ƒG ♠ ♠ ► ☺
@ l╠F pFG ♦FG (■( H'
H' = h■( Ç⌠âv = ë⌠âv╛Z┘D
X'= H' = z·EwR►Hw $
H'= H' = ¿■( Ç⌠âv = ë⌠âv
┘D¶ \'= H' = z·EwR►Hw $
ÿ■( ♦ >╖äù¿■( ÷âv
╚╕F ╝■( Bñâv¿☻ìvⁿ■( ╥⌠â
Ä◄ävb◄äv*Z┘D ╚╕F ►:A
(↨G ¶ƒG h↕G ¶ƒG ▄²( ∞²( h↕
(↨G ¶ƒG ♠ ♠ ► ☺
@ l╠F pFG ♦FG (■( H'
H' = h■( Ç⌠âv = ë⌠âv╛Z┘D
X'= H' = z·EwR►Hw $
H'= H' = ¿■( Ç⌠âv = ë⌠âv
┘D¶ \'= H' = z·EwR►Hw $
ÿ■( ♦ >╖äù¿■( ÷âv
╚╕F ╝■( Bñâv¿☻ìvⁿ■( ╥⌠â
Ä◄ävb◄äv*Z┘D ╚╕F ►:A
(↨G ¶ƒG h↕G ¶ƒG ▄²( ∞²( h↕
(↨G ¶ƒG ♠ ♠ ► ☺
@ l╠F pFG ♦FG (■( H'
H' = h■( Ç⌠âv = ë⌠âv╛Z┘D
X'= H' = z·EwR►Hw $
H'= H' = ¿■( Ç⌠âv = ë⌠âv
┘D¶ \'= H' = z·EwR►Hw $
ÿ■( ♦ >╖äù¿■( ÷âv
╚╕F ╝■( Bñâv¿☻ìvⁿ■( ╥⌠â
Ä◄ävb◄äv*Z┘D ╚╕F ►:A
(↨G ¶ƒG h↕G ¶ƒG ▄²( ∞²( h↕
(↨G ¶ƒG ♠ ♠ ► ☺
@ l╠F pFG ♦FG (■( H'
H' = h■( Ç⌠âv = ë⌠âv╛Z┘D
X'= H' = z·EwR►Hw $
H'= H' = ¿■( Ç⌠âv = ë⌠âv
┘D¶ \'= H' = z·EwR►Hw $
ÿ■( ♦ >╖äù¿■( ÷âv
╚╕F ╝■( Bñâv¿☻ìvⁿ■( ╥⌠â
Ä◄ävb◄äv*Z┘D ╚╕F ►:A
(↨G ¶ƒG h↕G ¶ƒG ▄²( ∞²( h↕
(↨G ¶ƒG ♠ ♠ ► ☺
@ l╠F pFG ♦FG (■( H'
H' = h■( Ç⌠âv = ë⌠âv╛Z┘D
X'= H' = z·EwR►Hw $
H'= H' = ¿■( Ç⌠âv = ë⌠âv
┘D¶ \'= H' = z·EwR►Hw $
ÿ■( ♦ >╖äù¿■( ÷âv
╚╕F ╝■( Bñâv¿☻ìvⁿ■( ╥⌠â
Ä◄ävb◄äv*Z┘D ╚╕F ►:A
Mar 3, 2012 at 3:01am UTC
have you tried checking
if (!myFile.is_open())
Mar 3, 2012 at 3:12am UTC
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//Global Variables
const int ROWS = 12;
const int COLS = 30;
const int BOARD_ROWS(10);
const int BOARD_COLS(28);
const char LIVE = 'X' ; //life cells
const char DEAD = '.' ; //dead cells
char NewBoard[ROWS][COLS];
//functions
void MakeArray(string filename, char board[][COLS]);
void GameBoard(char board[][COLS]);
void SwitchBoard (char board[][COLS]);
void NextState(char board[][COLS]);
int main()
{
char board [ROWS][COLS];
string filename; //Name of the file
cout<<"Enter the filename: \n" ;
cin>>filename;
cout<<endl;
//call functions
MakeArray(filename, board);
//start loop
for (int l = 0; l<10; l++)
{
NextState(board);
GameBoard(board);
SwitchBoard(board);
}
//end loop
//stop terminal window from quitting after programs ends
char q;
cin >> q;
return 0;
}
void MakeArray(string filename, char board[][COLS])
{
ifstream myfile;
myfile.open (filename.c_str());
if (!myfile.is_open())
{
cout<< "File not found\n" ;
}
else
{
for (int r=0; r<ROWS; r++)
{
for (int c=0; c<COLS; c++)
{
myfile>>board[r][c];
}
}
myfile.close();
}
}
void GameBoard (char board[][COLS])
{
for (int r=1; r<=ROWS-2; r++)
{
for (int c=1; c<=COLS-2; c++)
{
cout<<board[r][c];
}
cout<<endl;
}
cout<<endl;
}
void NextState (char board[][COLS])
{
for (int r=0; r<ROWS; r++)
{
for (int c=0; c<COLS; c++)
{
int LiveCnt=0;
if (board[r-1][c-1]==LIVE)
{
LiveCnt++;
}
if (board[r-1][c]==LIVE)
{
LiveCnt++;
}
if (board[r-1][c+1]==LIVE)
{
LiveCnt++;
}
if (board[r][c-1]==LIVE)
{
LiveCnt++;
}
if (board[r][c]==LIVE)
{
LiveCnt++;
}
if (board[r][c+1]==LIVE)
{
LiveCnt++;
}
if (board[r+1][c-1]==LIVE)
{
LiveCnt++;
}
if (board[r+1][c+1]==LIVE)
{
LiveCnt++;
}
/*/
Rules:
1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
2. Any live cell with two or three live neighbours lives on to the next generation.
3. Any live cell with more than three live neighbours dies, as if by overcrowding.
4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
/*/
if (board[r][c] == LIVE && LiveCnt < 2) //rule 1
{
NewBoard[r][c]=DEAD;
}
else if (board[r][c]==LIVE && (LiveCnt==2 || LiveCnt==3))//rule 2
{
NewBoard[r][c]=LIVE;
}
else if (board[r][c]==LIVE && LiveCnt>3 ) //rule 3
{
NewBoard[r][c]=DEAD;
}
else if (board[r][c]==DEAD && LiveCnt==3) //rule 4
{
NewBoard[r][c]=LIVE;
}
}
}
}
void SwitchBoard(char board[][COLS])
{
for (int r=0; r<ROWS; r++)
{
for (int c=0; c<COLS; c++)
{
board[r][c]==NewBoard[r][c];
}
}
}
output:
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
Enter the filename:
h.txt
File not found
(↨G ¶ƒG h↕G ¶ƒG ▄²( ∞²( h↕
(↨G ¶ƒG ♠ ♠ ► ☺
@ \╠F pFG ♦FG (■( H'
H' W h■( Ç⌠âv W ë⌠âv!╥╛¼
X'W H' W z·EwR►Hw $
H'W H' W ¿■( Ç⌠âv W ë⌠âv
╛¼¶ \'W H' W z·EwR►Hw $
ÿ■( ♦ │╣╖c¿■( ÷âv
╕╕F ╝■( Bñâv¿☻ìvⁿ■( ╥⌠â
Ä◄ävb◄äv╡╥╛¼ ╕╕F :A
(↨G ¶ƒG h↕G ¶ƒG ▄²( ∞²( h↕
(↨G ¶ƒG ♠ ♠ ► ☺
@ \╠F pFG ♦FG (■( H'
H' W h■( Ç⌠âv W ë⌠âv!╥╛¼
X'W H' W z·EwR►Hw $
H'W H' W ¿■( Ç⌠âv W ë⌠âv
╛¼¶ \'W H' W z·EwR►Hw $
ÿ■( ♦ │╣╖c¿■( ÷âv
╕╕F ╝■( Bñâv¿☻ìvⁿ■( ╥⌠â
Ä◄ävb◄äv╡╥╛¼ ╕╕F :A
(↨G ¶ƒG h↕G ¶ƒG ▄²( ∞²( h↕
(↨G ¶ƒG ♠ ♠ ► ☺
@ \╠F pFG ♦FG (■( H'
H' W h■( Ç⌠âv W ë⌠âv!╥╛¼
X'W H' W z·EwR►Hw $
H'W H' W ¿■( Ç⌠âv W ë⌠âv
╛¼¶ \'W H' W z·EwR►Hw $
ÿ■( ♦ │╣╖c¿■( ÷âv
╕╕F ╝■( Bñâv¿☻ìvⁿ■( ╥⌠â
Ä◄ävb◄äv╡╥╛¼ ╕╕F :A
(↨G ¶ƒG h↕G ¶ƒG ▄²( ∞²( h↕
(↨G ¶ƒG ♠ ♠ ► ☺
@ \╠F pFG ♦FG (■( H'
H' W h■( Ç⌠âv W ë⌠âv!╥╛¼
X'W H' W z·EwR►Hw $
H'W H' W ¿■( Ç⌠âv W ë⌠âv
╛¼¶ \'W H' W z·EwR►Hw $
ÿ■( ♦ │╣╖c¿■( ÷âv
╕╕F ╝■( Bñâv¿☻ìvⁿ■( ╥⌠â
Ä◄ävb◄äv╡╥╛¼ ╕╕F :A
(↨G ¶ƒG h↕G ¶ƒG ▄²( ∞²( h↕
(↨G ¶ƒG ♠ ♠ ► ☺
@ \╠F pFG ♦FG (■( H'
H' W h■( Ç⌠âv W ë⌠âv!╥╛¼
X'W H' W z·EwR►Hw $
H'W H' W ¿■( Ç⌠âv W ë⌠âv
╛¼¶ \'W H' W z·EwR►Hw $
ÿ■( ♦ │╣╖c¿■( ÷âv
╕╕F ╝■( Bñâv¿☻ìvⁿ■( ╥⌠â
Ä◄ävb◄äv╡╥╛¼ ╕╕F :A
(↨G ¶ƒG h↕G ¶ƒG ▄²( ∞²( h↕
(↨G ¶ƒG ♠ ♠ ► ☺
@ \╠F pFG ♦FG (■( H'
H' W h■( Ç⌠âv W ë⌠âv!╥╛¼
X'W H' W z·EwR►Hw $
H'W H' W ¿■( Ç⌠âv W ë⌠âv
╛¼¶ \'W H' W z·EwR►Hw $
ÿ■( ♦ │╣╖c¿■( ÷âv
╕╕F ╝■( Bñâv¿☻ìvⁿ■( ╥⌠â
Ä◄ävb◄äv╡╥╛¼ ╕╕F :A
(↨G ¶ƒG h↕G ¶ƒG ▄²( ∞²( h↕
(↨G ¶ƒG ♠ ♠ ► ☺
@ \╠F pFG ♦FG (■( H'
H' W h■( Ç⌠âv W ë⌠âv!╥╛¼
X'W H' W z·EwR►Hw $
H'W H' W ¿■( Ç⌠âv W ë⌠âv
╛¼¶ \'W H' W z·EwR►Hw $
ÿ■( ♦ │╣╖c¿■( ÷âv
╕╕F ╝■( Bñâv¿☻ìvⁿ■( ╥⌠â
Ä◄ävb◄äv╡╥╛¼ ╕╕F :A
(↨G ¶ƒG h↕G ¶ƒG ▄²( ∞²( h↕
(↨G ¶ƒG ♠ ♠ ► ☺
@ \╠F pFG ♦FG (■( H'
H' W h■( Ç⌠âv W ë⌠âv!╥╛¼
X'W H' W z·EwR►Hw $
H'W H' W ¿■( Ç⌠âv W ë⌠âv
╛¼¶ \'W H' W z·EwR►Hw $
ÿ■( ♦ │╣╖c¿■( ÷âv
╕╕F ╝■( Bñâv¿☻ìvⁿ■( ╥⌠â
Ä◄ävb◄äv╡╥╛¼ ╕╕F :A
(↨G ¶ƒG h↕G ¶ƒG ▄²( ∞²( h↕
(↨G ¶ƒG ♠ ♠ ► ☺
@ \╠F pFG ♦FG (■( H'
H' W h■( Ç⌠âv W ë⌠âv!╥╛¼
X'W H' W z·EwR►Hw $
H'W H' W ¿■( Ç⌠âv W ë⌠âv
╛¼¶ \'W H' W z·EwR►Hw $
ÿ■( ♦ │╣╖c¿■( ÷âv
╕╕F ╝■( Bñâv¿☻ìvⁿ■( ╥⌠â
Ä◄ävb◄äv╡╥╛¼ ╕╕F :A
(↨G ¶ƒG h↕G ¶ƒG ▄²( ∞²( h↕
(↨G ¶ƒG ♠ ♠ ► ☺
@ \╠F pFG ♦FG (■( H'
H' W h■( Ç⌠âv W ë⌠âv!╥╛¼
X'W H' W z·EwR►Hw $
H'W H' W ¿■( Ç⌠âv W ë⌠âv
╛¼¶ \'W H' W z·EwR►Hw $
ÿ■( ♦ │╣╖c¿■( ÷âv
╕╕F ╝■( Bñâv¿☻ìvⁿ■( ╥⌠â
Ä◄ävb◄äv╡╥╛¼ ╕╕F :A
This code works:
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 53 54 55 56
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//Global Variables
const int ROWS = 12;
const int COLS = 30;
const int BOARD_ROWS(10);
const int BOARD_COLS(28);
const char LIVE = 'X' ; //life cells
const char DEAD = '.' ; //dead cells
char NewBoard[ROWS][COLS];
//functions
void MakeArray(string filename);
int main()
{
string filename; //Name of the file
cout<<"Enter the filename: \n" ;
cin>>filename;
cout<<endl;
//call functions
MakeArray(filename);
//start loop
//end loop
//stop terminal window from quitting after programs ends
char q;
cin >> q;
return 0;
}
void MakeArray(string filename)
{
ifstream myfile;
myfile.open (filename.c_str());
if (!myfile.is_open())
cout<< "File not found\n" ;
myfile.close();
}
but it doesn't work with the full game of life code.
Also im using code blocks
Last edited on Mar 3, 2012 at 3:17am UTC
Mar 3, 2012 at 10:31am UTC
Okay, so you are finding that the file was not found. This worked okay. The problem is that your program still continued to run with uninitialized values. These values are not initialized because the file was not there.
You have two options:
1. Loop the prompt that asks the user to enter a filename that exists until the file actually exists.
2. Quite the program if an invalid filename was entered.
To do this, it might be a good idea to have MakeArray return true if everything failed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
bool MakeArray(string filename, char board[][COLS])
{
ifstream myfile;
myfile.open (filename.c_str());
if (!myfile) return true ;
for (int r=0; r<ROWS; r++)
{
for (int c=0; c<COLS; c++)
{
myfile>>board[r][c];
}
}
myfile.close();
return false ;
}
For option 1 you would do this in your main:
1 2 3 4 5 6 7 8 9
while (true )
{
cout<<"Enter the filename: \n" ;
cin>>filename;
cout<<endl;
//call functions
if (!MakeArray(filename, board)) break ; // Exit only if the file was opened, otherwise, ask again.
}
For option 2 you would do this in your main:
1 2 3 4 5 6 7 8 9 10 11
cout<<"Enter the filename: \n" ;
cin>>filename;
cout<<endl;
//call functions
if (MakeArray(filename, board))
{
cout << "Bad file entered, now exiting..." << endl;
return 1;
}
//The rest of your code only runs if a valid file was entered. Otherwise the program stops.
Topic archived. No new replies allowed.