I cant figure out why my second switch stament is not working. Can you please help? I am still new at this still.

// Project 8.4. Guesses the number the user enters between 1-100.
#include <iostream>
#include <ctime> 
using namespace std;

char H,L,Y,N;
int UsersNum, YesNo, HighLow, Re_Use;
int n = 50, lastguess;
int HigherThan = 0, LowerThan =101;

int main ()
{
    cout << "Welcome to the number guessing game! \n"; 
    cout << "I will try and guess the number you will enter. \n";
    cout << "Please enter a number between 1 - 100. \n";
    cin >> UsersNum;

    cout << "Is your number 50? Enter 1 for Yes or enter 2 for No. \n";
    cin >> YesNo;

    switch (YesNo)
    {   case 1:
            cout << "I guessed your number!\n";
        break;
do
{
        case 2:
            cout << "Higher or lower? \n";
            cout << "Enter H for higher or enter L for lower. \n";
            cin >> HighLow;
        break;

        default:
            cout << "Invalid entry. \n" ;
        break ;
    }

    switch (HighLow);
    {   case 'H':
            n =(LowerThan-n) /2 + n;
            LowerThan =lastguess;
            cout << "Is this your number " <<n<< "? \n";
            cout << "Please enter 1 for yes or 2 for no. \n";
            cin >> Re_Use;
        break;

        case 'L':
            n =(n-HigherThan)/2 + HigherThan;
            HigherThan = lastguess;
            cout << "Is this your number " <<n<< "? \n";
            cout << "Please enter Y for yes or N for no. \n";
            cin >> Re_Use;
        break;

        default:
            cout << "Invalid Entry. \n";
        break;
    }
    if (Re_Use == Y)
        return 0;
}
while ( Re_Use == N)
return 0;
}

Errors:

1>------ Build started: Project: project84, Configuration: Debug Win32 ------
1>Compiling...
1>project84.cpp
1>g:\visual studio 2008\projects\project84\project84.cpp(38) : error C2059: syntax error : 'switch'
1>g:\visual studio 2008\projects\project84\project84.cpp(55) : error C2048: more than one default
1>Build log was saved at "file://g:\Visual Studio 2008\Projects\project84\Debug\BuildLog.htm"
1>project84 - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Lines 25 and 26 are in the wrong place.

How are they in the wrong place? i dont want the while loop to repeat the first case of the switch statment.

How are they in the wrong place

By adding it there you are breaking your switch statements (creating incorrect syntax). You'll need to find a different way to do this.

oh okay... thanks

I would recommend against using a switch statement in this situation.
A switch statement is meant to look at an integer value, compare it against several options, and execute simple actions based on that comparison. It should not be used to branch into compound statements. Especially not if the initial condition can only have one of two values. Why not use an if statement for the first check?

OKay i used if statments, but now i cant get the do while loop to work. There are no errors or warnings. Can you help?

//Project 8.4. Guesses the number the user enters between 1-100.
#include <iostream>
#include <ctime> 
using namespace std;

int UsersNum, YesNo, HighLow, Re_Use;
int n = 50, lastguess;
int HigherThan = 0, LowerThan =101;

int main ()
{
    cout << "Welcome to the number guessing game! \n"; 
    cout << "I will try and guess the number you will enter. \n";
    cout << "Please enter a number between 1 - 100. \n";
    cin >> UsersNum;

    cout << "Is your number 50? Enter 1 for Yes or enter 2 for No. \n";
    cin >> YesNo;

    if (YesNo == 1){
        cout << "I have guessed your number correctly. Thanks for playing. \n";
        return 0;   }               
do
{
    if (YesNo == 2){
        cout << "Higher or lower? \n";
        cout << "Enter 1 for higher or enter 2 for lower. \n";
        cin >> HighLow;}

    switch (HighLow)
    {   case 1:
            n = (LowerThan - n) / 2 + n;
            LowerThan = lastguess;
            cout << "Is this your number " <<n<< " ? \n";
            cout << "Please enter 3 for yes or 4 for no. \n";
            cin >> Re_Use;
        break;

        case 2:
            n = (n - HigherThan) / 2 + HigherThan;
            HigherThan = lastguess;
            cout << "Is this your number " <<n<< " ? \n";
            cout << "Please enter 3 for yes or 4 for no. \n";
            cin >> Re_Use;
        break;

        default:
            cout << "Invalid Entry. \n";
        break;
    }

    if (Re_Use == 3)
        cout << "I have guessed your number correctly! Thanks for playing. \n" ;
        return 0;
}
while ( Re_Use == 4);

return 0;
}

Okay never mind. i got the do while statment to work now, but now the math part isnt right. when it runs it always goes lower.. Is my math statment wrong?

//Project 8.4. Guesses the number the user enters between 1-100.
#include <iostream>
#include <ctime> 
using namespace std;

int UsersNum, YesNo, HighLow, Re_Use;
int n = 50, lastguess;
int HigherThan = 0, LowerThan =101;

int main ()
{
    cout << "Welcome to the number guessing game! \n"; 
    cout << "I will try and guess the number you will enter. \n";
    cout << "Please enter a number between 1 - 100. \n";
    cin >> UsersNum;

    cout << "Is your number 50? Enter 1 for Yes or enter 2 for No. \n";
    cin >> YesNo;

    if (YesNo == 1){
        cout << "I have guessed your number correctly. Thanks for playing. \n";
        return 0;}              
do
{
    if (YesNo == 2){
        cout << "Higher or lower? \n";
        cout << "Enter 1 for higher or enter 2 for lower. \n";
        cin >> HighLow;}

    switch (HighLow)
    {   case 1:
            n = (LowerThan - n) / 2 + n;
            LowerThan = lastguess;
            cout << "Is this your number " <<n<< " ? \n";
            cout << "Please enter 1 for yes or 2 for no. \n";
            cin >> Re_Use;
        break;

        case 2:
            n = (n - HigherThan) / 2 + HigherThan;
            HigherThan = lastguess;
            cout << "Is this your number " <<n<< " ? \n";
            cout << "Please enter 1 for yes or 2 for no. \n";
            cin >> Re_Use;
        break;

        default:
            cout << "Invalid Entry. \n";
        break;
    }

    if (Re_Use == 1)
        cout << "I have guessed your number correctly! Thanks for playing. \n" ;
}
while (Re_Use == 2);

return 0;
}
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.