Public Group

This topic is 4174 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

:) [Edited by - Cool_Program_Guy on April 22, 2007 6:35:40 PM]

##### Share on other sites
Whenever you initialize variables in a case, you need to surround the statement with curly brackets:

switch(joke){  case 1:{    laugh();    int laughmeter = 5;    break;  }  case 0:    cout << "boo, hiss" << endl;}

##### Share on other sites
'case's of a 'switch' do not create their own scope. They are "labels", effectively the same as targets of a goto (but it is definitely better to use a switch for the things that a switch can do).

Possible ways to fix such problems:

- Find ways to avoid the variables entirely by "folding" together expressions. There is no need for a variable to hold something that is written only once and also read only once.

- Put the variable declarations outside the switch, if a given variable should be accessible from multiple cases.

- Manually create your own scope, by simply wrapping the code for the switch-case (not including the 'break;') within {}'s. The C++ grammar actually allows you to use such "anonymous" scopes basically anywhere you could use a for/if/while/etc. (as long as your braces nest properly).

There is no such thing as "an fstream file"; the fstream is the object in the code which you use to access the file on disk. "Where you put it" depends on how you referred to it in the code. If you give an absolute path (i.e., on Windows, starting with a drive letter), then the file should be at the absolute location that you named. If you give a relative path (as you have here - in this case, just the file name with no folders being named), then it should be placed relative to the .exe (in your case, test.txt should be in the same folder as the .exe). If you're using an IDE, I don't know where it puts your .exe, sorry.

Now, time to clean up the code (at least a little) and hopefully fix the errors.

// Please try to be more consistent with indentation. It really helps when// you're tracking down those missing/extra {'s or }'s or ;'s.int main() {	system("cls");	int choice;	cout << "Connect Four Version 1.0\n";	time_t rawtime;	time(&rawtime);	// Please don't just copy examples from tutorials. I happen to know	// where you took this one from; even the spacing matches :P *Think*	// about what's being shown in examples. There's no reason you can't	// feed the result of a ctime() call to std::cout.	// Also, mixing printf() calls in with use of std::cout can sometimes	// mess up, because they might not be synchronized with each other.	// Also, don't use std::endl as a newline substitute; and be aware	// that you can output multiple things at once...	cout << "The Date And Time Is: " << ctime(&rawtime)	     << "\nMenu\n\n"	     << "a) Start A Game\n"	     << "b) Top 20 High Players\n"	     << "c) Quit Game\n"	     << "Please Select 1 To 3:\n\n" << endl;	cin >> choice;	cout << endl;	// This actually does not validate the input properly, but we'll worry	// about that later...	while ((choice <= 0) || (choice >= 4)) {		// I fixed your English here. :s		cout << "Please enter a valid menu choice 1 To 3\n" << endl;		cin >> choice;	}	switch (choice) {		case 1: { // Note added braces.			system("cls");			string name1, name2;			GetNames(name1, name2);			Player player_one(name1, 'X');			Player player_two(name2, 'O');			setupGrid();			int players_turn = 1;			while(connect_four_loop_loop) {				if (players_turn == 1) {					PlayerTurn(player_one); 					players_turn = 2;				} else {					PlayerTurn(player_two); 					players_turn = 1;				}			}		}		break;		case 2: {			// Don't use the .getline() member function of istreams.			// Instead, use the free function std::getline(), which			// reads from the stream into a much nicer std::string			// object (no need to worry about remembering the 			// length or thinking about what to do if the line 			// doesn't fit).			string str;    			ifstream myfile("test.txt"); 			// no need for an 'f' prefix; are you really going to			// forget that "myfile" is a file?			if (!myfile) {         				cout << "ERROR: can't open test.txt\n"; 				// why split that up? :)				// exit() is pretty much never called-for. Here				// it certainly isn't; the program can continue				// just fine if the file isn't found.			}			// Don't test .eof() in loops. Instead, use the read 			// attempt for the while condition.			// (Note that if the file wasn't found, this simply does			// nothing; the getline() fails the first time and the 			// result evaluates as false.)			while (getline(myfile, str)) {        				cout << str << "\n";        			}			// There is no need to close fstreams explicitly in			// normal cases.   			// Don't pause your programs artificially at the end. 			// (Also, you had the pause in the wrong place.)		}	} } // You had an extra } at the end, causing that error. Of course, I effectively// added one back after fixing that, in order to close the anonymous scope for// case 2. ;)

##### Share on other sites
Quote:
 Original post by Zahlman'case's of a 'switch' do not create their own scope. They are "labels", effectively the same as targets of a goto (but it is definitely better to use a switch for the things that a switch can do).Possible ways to fix such problems:- Find ways to avoid the variables entirely by "folding" together expressions. There is no need for a variable to hold something that is written only once and also read only once.- Put the variable declarations outside the switch, if a given variable should be accessible from multiple cases.- Manually create your own scope, by simply wrapping the code for the switch-case (not including the 'break;') within {}'s. The C++ grammar actually allows you to use such "anonymous" scopes basically anywhere you could use a for/if/while/etc. (as long as your braces nest properly).There is no such thing as "an fstream file"; the fstream is the object in the code which you use to access the file on disk. "Where you put it" depends on how you referred to it in the code. If you give an absolute path (i.e., on Windows, starting with a drive letter), then the file should be at the absolute location that you named. If you give a relative path (as you have here - in this case, just the file name with no folders being named), then it should be placed relative to the .exe (in your case, test.txt should be in the same folder as the .exe). If you're using an IDE, I don't know where it puts your .exe, sorry.Now, time to clean up the code (at least a little) and hopefully fix the errors.*** Source Snippet Removed ***

Hey thanks i only just read wot u put now lol :). When you say the
 while ((choice <= 0) || (choice >= 4)) {		// I fixed your English here. :s		cout << "Please enter a valid menu choice 1 To 3\n" << endl;		cin >> choice;	}
whys that not right? I'm pretty noob ish on c++ still, and for the fstream how would I not only open a file but code it so that after someone playes a game it puts in the score of won, lost or draw? and then if the same person plays again to load up their last score and update it?? I would assume I'd have to use bubble sort to find that player name??

1. 1
2. 2
3. 3
Rutin
16
4. 4
JoeJ
13
5. 5

• 10
• 9
• 14
• 10
• 25
• ### Forum Statistics

• Total Topics
632646
• Total Posts
3007637
• ### Who's Online (See full list)

There are no registered users currently online

×