Help converting while loop to for loop?



string see;

while (getline(ifile, line))
{
istringstream iss(line);

while (iss >> see)
{
cout << see;
}
cout << endl;
}

I really want to put the data into a 3d array, its a string called board[x][y][1],
I'm still new to C++ and im not sure if there is a way to write the data that is output from these while loops to an array without a for loop.

any help converting it to a for loop is greatly appreciated!
Yea, anything that's possible with while loops is also possible with for loops, and vice versa. Think about what for loops are. That should help.

1
2
3
4
for(/*Do this when we start for all the loops*/,/*Condition*/,/*Something to do at the start of each loop, usually something simple like incrementing a variable*/)
{
//Main body of loop
}
Last edited on
I did it like this but my output is not long enough, here is a photo of the output:

https://sites.google.com/site/officialhosting/Capture23432.PNG

this is a picture of my ifstream file:

https://sites.google.com/site/officialhosting/Capture2343432.PNG


notice how the last value isnt getting displayed,


This is for loop im concerned about(line 209 below):

1
2
3
4
5
6
7
8
9
10
11
12
13
	for(x=0; x<5;x++)
	{

		for(y=0;y<5;y++)
		{

			getline (ifile, line);
			board[x][y]=line;
			cout<<board[x][y] << endl;

		}

	}	


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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/////////////////////////////////////////////////////
////////////     math jeoperdy     //////////////////
/////////////////////////////////////////////////////
/*
   date:
process:

*/
//
//   headers and includes //
#include <iostream>
#include <ctime>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>

using namespace std;


//   global Constants    //
////////////////////////////////////////////////////////////////



//   function prototypes //
////////////////////////////////////////////////////////////////
void fileOpen(ifstream& ifile, ofstream& ofile);
void saveGame(string filename, ofstream& ofile, string username);
void loadGame(string filename, ifstream& ifile);
void randNumber(double& num1 ,double& num2 ,int& level);
void randEquation(char sign, double num1, double num2, ofstream& ofile);
void jeopardy();
void scoreUpdate();
void dailyDouble();
void answercheck();


//   main  game loop  //
///////////////////////////////////////////////////////////////////


int main()
{
ifstream ifile; ofstream ofile;
	
//seeding random number generator with Time
	
srand((unsigned)time(0));

// function fileOpen( )

fileOpen(ifile, ofile);

// function jeopardy()

jeopardy();

// function

 
// function

// end of main()
return 0;
}

 

//functions implementations


//fileOpen
void fileOpen(ifstream& ifile, ofstream& ofile)
{

string filename;
string user1;
string user2;
string user3;
double num1;
double num2;
int level;
int addition;
int iteration;
char sign;

// promt for "game name" if "game name" exists, call loadGame()

cout << "Welcome to Jeopardy!!" << endl;
cout << "Please enter filename (without file extension): ";
cin >> filename;

filename += ".txt";

ifstream(filename.c_str());

if (!ifile.is_open())
    {

	cout << "File does not exist, but will now be created...";
    ofile.open(filename.c_str());

	cout << "Please enter 3 Usernames:" << endl << "User 1: ";
	cin >> user1;
	cout << endl << "User 2: ";
	cin >> user2;
	cout << endl << "User 3: ";
	cin >> user3;
	
	//Creation of new Gamesave with $0 as scores and user Defined usernames.
	ofile << user1 << endl;
	ofile << user2 << endl;
	ofile << user3 << endl;
	ofile << "0" << endl;
	ofile << "0" << endl;
	ofile << "0" << endl;

	//addition
	for (((addition=0),(level=1)); addition<5; ((addition++),(level++)))
		{
			randNumber(num1, num2, level);
			randEquation(sign = '+', num1, num2, ofile);
		}
	//subtraction
	for (((addition=0),(level=1)); addition<5; ((addition++),(level++)))
		{
			randNumber(num1, num2, level);
			randEquation(sign = '-', num1, num2, ofile);
		}
	//multiplication
	for (((addition=0),(level=1)); addition<5; ((addition++),(level++)))
		{
			randNumber(num1, num2, level);
			randEquation(sign = '*', num1, num2, ofile);
		}
	//division
	for (((addition=0),(level=1)); addition<5; ((addition++),(level++)))
		{
			randNumber(num1, num2, level);
			randEquation(sign = '/', num1, num2, ofile);
		}
	//Mixed
	for (((addition=0),(level=1)); addition<5; ((addition++),(level++)))
		{
			randNumber(num1, num2, level);
			randEquation(sign = 'm', num1, num2, ofile);
		}
	
	for (iteration=1; iteration<6; iteration++)
	{
		for(addition=0; addition<5; addition++)
		{
			ofile << (iteration*100) << endl;
		}	
	}
	ofile.close();
	loadGame(filename, ifile);

    }
else loadGame(filename, ifile);

// if does not exist, prompt for usernames
// open an array called create
// create the players names in a way
// create their score
// create round num
// create the gameBoard
// put it into an array called gameBoard[32]
// close file
}


// loadGame();
void loadGame(string filename, ifstream& ifile)
{

    int x;
    int y;
	int lines;

	string line;

	// Original Arrays
	string username[3];
    int scores[3];
    string board[5][5];
	int values[5][5];

//use array
//use ofstream to overwrite file with current data seperated with endl's. (player1,player2,player3,.

    ifile.open(filename.c_str());
	cout << "Usernames" << endl;
    for(x=0; x<3; x++)
    {
		ifile>>username[x];
		cout << username[x] << endl;
    }
	cout << "Scores" << endl;
    for (x=0; x<3; x++)
    {
        ifile>>scores[x];
		cout << "$" << scores[x] << endl;
    }

	cout << "Gameboard State:" << endl;
    
	for(x=0; x<5;x++)
	{

		for(y=0;y<5;y++)
		{

			getline (ifile, line);
			board[x][y]=line;
			cout<<board[x][y] << endl;

		}

	}	

/*	
	cout << "Gameboard Display Values:" << endl;
	for(x=0; x<5;x++)
	{

		for(y=0;y<5;y++)
		{

        ifile >> values[x][y];
		cout << "$" << values[x][y] << endl;

		}

	}
*/
}





// saveGame();
void saveGame(string filename, ofstream &ofile, string username[3])
{
    int nav;
    int van;  

    int scores[3];
    string board[5][5];
//use array
//use ofstream to overwrite file with current data seperated with commas. (player1,player2,player3,.

    ofile.open(filename + ".txt");

    for(nav=0; nav<3; nav++)
    {
        ofile << username[nav] << endl;
    }

    for (nav=0; nav<3; nav++)
    {
        ofile << scores[nav] << endl;
    }


    for (van=0; van<5; van++)
    {
        for (nav=0; nav<5; nav++)
        {
            ofile << board[van][nav] << endl;
        }

    }

}


// jeopardy()
void jeopardy()
{

 

}

void randNumber(double& num1 ,double& num2 ,int& level)
{
	int randomizer = rand() % 1;

	{
  

    switch(level)
     {
       case 1:
           {
             num1 = rand() % 9 +1;
			 num2 = rand() % 9 +1;
           }
           break;
       case 2:
           {
               num1 = rand() % 99 +1;
			   num2 = rand() % 99 +10;
           }
           break;
       case 3:
           {
              num1 = rand() % 999 +100;
			  num2 = (rand() % 9 +1) * (-1);

           }
           break;
       case 4:
           {
              num1 = rand() % 9999 +1000;
           if (randomizer == 1)
              num2 = (rand() % 9 +1) * (-1);
           else
              num2 = rand() % 99 +10;
           }
           break;
       case 5:
           {
			 num1 = rand() % 99999 +10000;
          if (randomizer == 1)
		     num2 = (rand() % 9 +1) * (-1);
          else
			 num2 = rand() % 9 +1;
           }
           break;

     }

    }    


}

void randEquation(char sign, double num1, double num2, ofstream& ofile)
{

 switch (sign)
 {
     case '+':
         {
			 ofile << num1 << " " << sign << " " << num2 << " = ?" << endl;
         }
         break;

     case '-':
         {
			ofile << num1 << " " << sign << " " << num2 << " = ?" << endl;
		 }

         break;

     case '*':
         {
			ofile << num1 << " " << sign << " " << num2 << " = ?" << endl;
		 }
         break;

     case '/':
         {
			ofile << num1 << " " << sign << " " << num2 << " = ?" << endl;
		 }
         break;
	
	 case 'm':
       {
		   int randomsign = rand() % 4;

		   switch(randomsign)
			{
				case 0:
			   { 
				   ofile << num1 << " + " << num2 << " = ?" << endl;
			   }
			   break;
			    case 1:
			   {
				   ofile << num1 << " - " << num2 << " = ?" << endl;
			   }
			   break;
				case 2:
			   {
				   ofile << num1 << " * " << num2 << " = ?" << endl;
			   }
			   break;
				case 3:
			   {
				   ofile << num1 << " / " << num2 << " = ?" << endl;
			   }
			   break;


		   }
		   break;
	   }
         break;     

 }

}
Last edited on
I jerry rigged it by adding

1
2
3
	getline (ifile, line);
	board[4][4]=line;
	cout <<board[4][4] << endl;


at line 222

:P
Topic archived. No new replies allowed.