•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 392,053 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,285 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 345 | Replies: 12
![]() |
•
•
Join Date: Jul 2008
Posts: 11
Reputation:
Rep Power: 1
Solved Threads: 0
Need a little help here on the frist part of this program. I have no finished the rest of code but i am trying to test out and run what i have so far. but i keep getting this error from the last brace of the code: syntax error : '}'. then when i remove that brace i get this error: fatal error C1075: end of file found before the left brace '{'. any help on this will be great thank you.
language Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <fstream> using namespace std; std::string word[5]; int main () { //Declaring Variables ifstream inFile; char choice; int secret_number; int user_number; // opening files inFile.open("secret number.txt"); //menu do{ cout << "A: To Play the number guessing game." << endl; cout << "B: To Play the letter game." << endl; cout << "C: To quit program." << endl; cin >> choice; while (choice != 'c' && choice != 'C'); switch (choice) { case 'A': case 'a': cout << "Welcome to the Guess the Number Game." << endl; cout << "\n I am thinking of a number between 1 and 100." << endl; // Loops forever until user finds the number while (user_number != secret_number) { cout<<"Pick a number : "; cin >> user_number; inFile >> secret_number; if (secret_number > user_number) cout << "Higher!"<<endl<<endl; else if (secret_number < user_number) cout << "Lower!"<<endl<<endl; else cout << "Good job! You found the number!"<<endl<<endl; } } break; } }
Last edited by greyghost86 : 28 Days Ago at 12:09 pm.
Well adopting a consistent approach to indentation will help, not some chaotic mix you have at the moment.
Eg.
It's immediately obvious that the break; on line 52 isn't part of the switch/case.
Also, the do at line 21, which ends at 53 is missing a condition on line 53.
Eg.
c++ Syntax (Toggle Plain Text)
#include <string> #include <fstream> using namespace std; std::string word[5]; int main () { //Declaring Variables ifstream inFile; char choice; int secret_number; int user_number; // opening files inFile.open("secret number.txt"); //menu do{ cout << "A: To Play the number guessing game." << endl; cout << "B: To Play the letter game." << endl; cout << "C: To quit program." << endl; cin >> choice; while (choice != 'c' && choice != 'C'); switch (choice) { case 'A': case 'a': cout << "Welcome to the Guess the Number Game." << endl; cout << "\n I am thinking of a number between 1 and 100." << endl; // Loops forever until user finds the number while (user_number != secret_number) { cout<<"Pick a number : "; cin >> user_number; inFile >> secret_number; if (secret_number > user_number) cout << "Higher!"<<endl<<endl; else if (secret_number < user_number) cout << "Lower!"<<endl<<endl; else cout << "Good job! You found the number!"<<endl<<endl; } } break; } }
Also, the do at line 21, which ends at 53 is missing a condition on line 53.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Well did you just copy/paste it and hope it would work?
Perhaps the do on line 21 is matched by the while on line 26 ?
Perhaps the do on line 21 is matched by the while on line 26 ?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
•
•
Join Date: Jul 2008
Posts: 11
Reputation:
Rep Power: 1
Solved Threads: 0
Nevermind I got it. This is what i ended up with. Thx for the help guys.
language Syntax (Toggle Plain Text)
[icode] #include <iostream> #include <string> #include <fstream> using namespace std; std::string word[5]; int main () { //Declaring Variables ifstream inFile; char choice; int secret_number; int user_number; // opening files inFile.open("secret number.txt"); //menu do{ cout << "A: To Play the number guessing game." << endl; cout << "B: To Play the letter game." << endl; cout << "C: To quit program." << endl; cin >> choice; switch (choice) { case 'A': case 'a': cout << "Welcome to the Guess the Number Game." << endl; cout << "\n I am thinking of a number between 1 and 100." << endl; // Loops forever until user finds the number inFile >> secret_number; do{ cout<<"Pick a number : "; cin >> user_number; if (secret_number > user_number) cout << "Higher!"<<endl<<endl; else if (secret_number < user_number) cout << "Lower!"<<endl<<endl; else cout << "Good job! You found the number!"<<endl<<endl; } while (user_number != secret_number); break; } }while (choice != 'c' && choice != 'C'); } [/icode]
•
•
Join Date: Nov 2007
Posts: 833
Reputation:
Rep Power: 4
Solved Threads: 170
A radical suggestion, instead of coding the games inside the switch, make the games functions and do something like
Or perhaps use
cplusplus Syntax (Toggle Plain Text)
while(1) { cout << "A: To Play the number guessing game." << endl; cout << "B: To Play the letter game." << endl; cout << "C: To quit program." << endl; cin >> choice; switch (choice) { case 'A': case 'a': // number guessing number_guessing(); break; case 'B': case 'b': // letter game letter_game(); break; case 'C': case 'c': // exit return 0; default: cout << "Correct choice please ...\n"; break; } }
Or perhaps use
if/else if/else ... instead of switch() . do{
} while (choice != 'c' && choice != 'C');Then your code becomes
do{
cout << "A: To Play the number guessing game." << endl;
cout << "B: To Play the letter game." << endl;
cout << "C: To quit program." << endl;
cin >> choice;
} while (choice != 'c' && choice != 'C');Then perhaps
do{
cout << "A: To Play the number guessing game." << endl;
cout << "B: To Play the letter game." << endl;
cout << "C: To quit program." << endl;
cin >> choice;
} while (choice != 'c' && choice != 'C');
switch (choice)
{
case 'A':
case 'a':
break;
}When you've done this a few times, then test what you've written so far to make sure it actually works as expected. If it doesn't, then fix those before adding more code.
In particular, note that the { } are never left out of place, or unbalanced.
What you've done is code beyond your ability to deal with the compiler throwing the whole mess back at you, so instead you dump it on a message board for someone else to fix. This isn't a good long term strategy.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
- Game Development FAQ's, Books and Resources (Game Development)
- Multiple key check (C++)
- lots of problems with my simple game (C++)
- game needs more adversaries (Java)
- Making Text Based Game (Game Development)
- school project: simple C++ game (need help with keyboard input) (C++)
- VB6 oRPG game: Book of Souls (Visual Basic 4 / 5 / 6)
- Computer Engeering (Pascal and Delphi)
- build java game (Java)
Other Threads in the C++ Forum
- Previous Thread: Visual C++ 2008 playing a sound
- Next Thread: Arrays



Linear Mode