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.

#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;
}
}

Recommended Answers

All 12 Replies

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.

Well adopting a consistent approach to indentation will help, not some chaotic mix you have at the moment.
Eg.

#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;
    }
}

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.

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.

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 ?

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?

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.

Nevermind I got it. This is what i ended up with. Thx for the help guys.

#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'); 
}   

A radical suggestion, instead of coding the games inside the switch, make the games functions and do something like

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');

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.

commented: Great advice. +5
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.

I am sorry I came to message board to ask for help on something. I thought that was what it was used for. also if I filled in my program they way you have it above.

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;
    }

It wouldn't have compiled right anyway cause I need the while after the switch like so.

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':
        break;
    }
} while (choice != 'c' && choice != 'C');

This works like a charm on my program. Sorry I dumped my problem on you.

Salem was trying to teach you a better way to fish, not just toss you a fish. Sometimes you're going to get a direct answer to answer a given problem, and sometimes you're going to get a better way to solve the problem in the first place. I hope you can enjoy both approaches.

commented: Exactly! +19
commented: 100% correct. Good advice helps the programmer not just in the current program, but in future programs too. +5

> Salem was trying to teach you a better way to fish
Exactly!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.