User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jul 2008
Posts: 11
Reputation: greyghost86 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
greyghost86 greyghost86 is offline Offline
Newbie Poster

making a simple c++ game

  #1  
28 Days Ago
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.



  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6. std::string word[5];
  7.  
  8. int main ()
  9. {
  10. //Declaring Variables
  11. ifstream inFile;
  12. char choice;
  13. int secret_number;
  14. int user_number;
  15.  
  16.  
  17.  
  18. // opening files
  19. inFile.open("secret number.txt");
  20.  
  21. //menu
  22. do{
  23. cout << "A: To Play the number guessing game." << endl;
  24. cout << "B: To Play the letter game." << endl;
  25. cout << "C: To quit program." << endl;
  26. cin >> choice;
  27. while (choice != 'c' && choice != 'C');
  28. switch (choice)
  29. {
  30. case 'A':
  31. case 'a':
  32. cout << "Welcome to the Guess the Number Game." << endl;
  33. cout << "\n I am thinking of a number between 1 and 100." << endl;
  34.  
  35. // Loops forever until user finds the number
  36.  
  37. while (user_number != secret_number)
  38. {
  39. cout<<"Pick a number : ";
  40. cin >> user_number;
  41. inFile >> secret_number;
  42.  
  43. if (secret_number > user_number)
  44. cout << "Higher!"<<endl<<endl;
  45.  
  46. else if (secret_number < user_number)
  47. cout << "Lower!"<<endl<<endl;
  48.  
  49. else
  50. cout << "Good job! You found the number!"<<endl<<endl;
  51. }
  52. }
  53. break;
  54. }
  55. }
Last edited by greyghost86 : 28 Days Ago at 12:09 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2008
Posts: 20
Reputation: phouse512 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
phouse512 phouse512 is offline Offline
Newbie Poster

Re: making a simple c++ game

  #2  
28 Days Ago
I'm not a C++ type person, but at line 44 where you start the if statement, there are no brackets starting and ending that. Also, I think you need brackets for the else statements too. I don't know if that is supposed to be like that though.
Reply With Quote  
Join Date: Dec 2005
Posts: 3,264
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 368
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: making a simple c++ game

  #3  
28 Days Ago
Well adopting a consistent approach to indentation will help, not some chaotic mix you have at the moment.
Eg.
  1. #include <string>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5. std::string word[5];
  6.  
  7. int main ()
  8. {
  9. //Declaring Variables
  10. ifstream inFile;
  11. char choice;
  12. int secret_number;
  13. int user_number;
  14.  
  15.  
  16.  
  17. // opening files
  18. inFile.open("secret number.txt");
  19.  
  20. //menu
  21. do{
  22. cout << "A: To Play the number guessing game." << endl;
  23. cout << "B: To Play the letter game." << endl;
  24. cout << "C: To quit program." << endl;
  25. cin >> choice;
  26. while (choice != 'c' && choice != 'C');
  27. switch (choice)
  28. {
  29. case 'A':
  30. case 'a':
  31. cout << "Welcome to the Guess the Number Game." << endl;
  32. cout << "\n I am thinking of a number between 1 and 100." << endl;
  33.  
  34. // Loops forever until user finds the number
  35.  
  36. while (user_number != secret_number)
  37. {
  38. cout<<"Pick a number : ";
  39. cin >> user_number;
  40. inFile >> secret_number;
  41.  
  42. if (secret_number > user_number)
  43. cout << "Higher!"<<endl<<endl;
  44.  
  45. else if (secret_number < user_number)
  46. cout << "Lower!"<<endl<<endl;
  47.  
  48. else
  49. cout << "Good job! You found the number!"<<endl<<endl;
  50. }
  51. }
  52. break;
  53. }
  54. }
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.
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.
Reply With Quote  
Join Date: Jul 2008
Posts: 11
Reputation: greyghost86 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
greyghost86 greyghost86 is offline Offline
Newbie Poster

Re: making a simple c++ game

  #4  
28 Days Ago
not sure what condition to put on 53 i moved line 26 to line 53 but then my loops messed. after i guess once it went back to the menu.
Last edited by greyghost86 : 28 Days Ago at 12:33 pm.
Reply With Quote  
Join Date: Dec 2005
Posts: 3,264
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 368
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: making a simple c++ game

  #5  
28 Days Ago
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 ?
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.
Reply With Quote  
Join Date: Jun 2008
Location: Washington
Posts: 605
Reputation: Alex Edwards will become famous soon enough Alex Edwards will become famous soon enough 
Rep Power: 3
Solved Threads: 50
Alex Edwards Alex Edwards is online now Online
Practically a Master Poster

Re: making a simple c++ game

  #6  
28 Days Ago
Originally Posted by Salem View Post
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 ?


I think that while should be on line 53?
Reply With Quote  
Join Date: Jul 2008
Posts: 11
Reputation: greyghost86 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
greyghost86 greyghost86 is offline Offline
Newbie Poster

Re: making a simple c++ game

  #7  
28 Days Ago
well i did copy/paste it. I wasn't positive it would work but it helped me know that I need to move my while to 53, but it seems to screw the loop up. I am happy to see that my Case A is working properly just that after i guess once it goes back to the menu.
Reply With Quote  
Join Date: Jul 2008
Posts: 11
Reputation: greyghost86 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
greyghost86 greyghost86 is offline Offline
Newbie Poster

Re: making a simple c++ game

  #8  
28 Days Ago
Nevermind I got it. This is what i ended up with. Thx for the help guys.
  1. [icode]
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7. std::string word[5];
  8.  
  9. int main ()
  10. {
  11. //Declaring Variables
  12. ifstream inFile;
  13. char choice;
  14. int secret_number;
  15. int user_number;
  16.  
  17.  
  18.  
  19. // opening files
  20. inFile.open("secret number.txt");
  21.  
  22. //menu
  23. do{
  24. cout << "A: To Play the number guessing game." << endl;
  25. cout << "B: To Play the letter game." << endl;
  26. cout << "C: To quit program." << endl;
  27. cin >> choice;
  28.  
  29. switch (choice)
  30. {
  31. case 'A':
  32. case 'a':
  33. cout << "Welcome to the Guess the Number Game." << endl;
  34. cout << "\n I am thinking of a number between 1 and 100." << endl;
  35.  
  36. // Loops forever until user finds the number
  37. inFile >> secret_number;
  38. do{
  39. cout<<"Pick a number : ";
  40. cin >> user_number;
  41.  
  42. if (secret_number > user_number)
  43. cout << "Higher!"<<endl<<endl;
  44.  
  45. else if (secret_number < user_number)
  46. cout << "Lower!"<<endl<<endl;
  47.  
  48. else
  49. cout << "Good job! You found the number!"<<endl<<endl;
  50.  
  51. } while (user_number != secret_number);
  52. break;
  53. }
  54. }while (choice != 'c' && choice != 'C');
  55. }
  56. [/icode]
Reply With Quote  
Join Date: Nov 2007
Posts: 833
Reputation: mitrmkar is a jewel in the rough mitrmkar is a jewel in the rough mitrmkar is a jewel in the rough 
Rep Power: 4
Solved Threads: 170
mitrmkar mitrmkar is offline Offline
Practically a Posting Shark

Re: making a simple c++ game

  #9  
28 Days Ago
A radical suggestion, instead of coding the games inside the switch, make the games functions and do something like
  1. while(1)
  2. {
  3. cout << "A: To Play the number guessing game." << endl;
  4. cout << "B: To Play the letter game." << endl;
  5. cout << "C: To quit program." << endl;
  6. cin >> choice;
  7.  
  8. switch (choice)
  9. {
  10. case 'A': case 'a': // number guessing
  11. number_guessing();
  12. break;
  13. case 'B': case 'b': // letter game
  14. letter_game();
  15. break;
  16. case 'C': case 'c': // exit
  17. return 0;
  18. default:
  19. cout << "Correct choice please ...\n";
  20. break;
  21. }
  22. }

Or perhaps use if/else if/else ... instead of switch() .
Reply With Quote  
Join Date: Dec 2005
Posts: 3,264
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 368
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: making a simple c++ game

  #10  
28 Days Ago
    do{
    } while (choice != 'c' && choice != 'C');
This should be your code to start with, then you compile it to see what the result is. If it doesn't compile, then fix it.

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');
Compile it again, and fix any new errors.

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;
    }
Notice how you can add just a few lines at a time, and it will compile.

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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:33 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC