The following is the code for my program that is supposed to choose a random number and then get the user to guess it by using the basic hints. It seems like it should work but it keeps coming up with the error:'else' without a previous 'if'. it is getting really annoying. Hope someone can help.

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

int main()
{
    int secretnum;
    int getint;

    srand (time(NULL) );

    secretnum=rand() % 10;

    cout<<"guess a number between 1 and 10_";
    cin>>getint;

        if (secretnum<getint)
            cout<<"try a lower number";
            goto loop;
        else if (secretnum>getint)
            cout<<"try a higher number";
            goto loop;
        else
            cout<<"that is correct";
        loop:
            cout<<"Enter another number between 1 and 10_";
            cin>>getint;

    return 0;
}

I am using code::blocks. I hope its not to abvious i have only been programming for a couple weeks.

thanks

Until you know the rules for braces, it's best to always use them:

if (secretnum<getint)
{
    cout<<"try a lower number";
    goto loop;
}
else if (secretnum>getint)
{
    cout<<"try a higher number";
    goto loop;
}
else
{
    cout<<"that is correct";
}

By the way, you should also avoid goto in favor of explicit loop structures, especially when starting out.

You need to use braces if within the if (conditional) you have more than a single line.

...Does not require braces...
if (age>18)
cout << "You may enter the bar";

...Requires braces...
if (age < 18)
{
cout << "You're too young.";
return 0;
}

Also, try never to use 'goto', most of the time you can use a 'while loop' instead.

thanks for the help that error is now gone but when i run the program it always chooses the number 5 (i tested it quite a few times). I tried recompiling but it was still wrong.

Could you post your code again?

yeah i can post my new code here it is:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

int main()
{
    int secretnum;
    int getint;

    srand (time(NULL) );

    secretnum=rand() % 10;

    loop:
    cout<<"guess a number between 1 and 10_";
    cin>>getint;

        if (secretnum<getint)
        {
            cout<<"try a lower number";
            goto loop;
        }

        else if (secretnum>getint)
            {
                cout<<"try a higher number";
               goto loop;
            }
        else
            {
                cout<<"that is correct, press enter to end";
            }





    return 0;
}

It works ok for me. Maybe you should just try playing more. Here, I made a couple of modifications:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

int main()
{
    int secretnum;
    int getint;

    srand(time(NULL));

    again:

    secretnum = rand() % 10 + 1;

    loop:

    cout<<"guess a number between 1 and 10_";
    cin>>getint;

    if (secretnum<getint)
    {
        cout<<"try a lower number";
        goto loop;
    }
    else if (secretnum>getint)
    {
        cout<<"try a higher number";
        goto loop;
    }
    else
    {
        cout<<"that is correct. play again? <y/n>\n";

        char choice;
        cin >> choice;

        if (choice == 'y' || choice == 'Y')
            goto again;

        cout << "press enter to end";
    }

    return 0;
}

Notice that rand() % 10 will give a random number between 0 and 9. You need to add one to get a [1, 10] range.

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.