1

It's my first time posting here, so I apologize if the post is not formatted correctly. I'm new to C++ and was looking for some help.

I can't seem to figure out what is stopping my code after roughly the 18th line. It processes the first cout and cin, but dosen't continue with the next cout?

If you enter a key, it will run through all of the conditions listed in the if/then statement and finish. However, my goal for what is here, is to have the computer generate a random number, followed by asking me for input (Y/N). Given the input, it's either supposed to generate another random number or end.

Using the compiler generate no errors, so I'm a bit dumbfounded right now as to what the issue is.

I'd appreciate any help, thank you.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<iostream.h>

int comp;

using namespace std;
int main ()
{


comp= 1+(rand() % 10);

randomnumber:
string again;

cout <<"The computer chose this random number: "<< comp << endl;
cin >> comp;

cout << "Would you like to run this again? Y/N" << endl;
cin >> again;



if((again == "y")||(again == "Y"))
{
    goto randomnumber;
}

else if((again == "n")||(again == "N"))
{
 cout << "OK" << endl;
}

else if((again != "y")||(again != "Y")||(again != "n")||(again !="N"))
{
 cout << "Please type Y or N" << endl;

}
    return 0;
}
  • 3
    [Don't use `goto`!](https://xkcd.com/292/) – scohe001 May 08 '18 at 18:35
  • 2
    Don't use labels a `goto` for loops! Use actual loops instead. As for your problem I suggest you [learn how to debug your code](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/), as it will help you figure out the problem very easily. And more problems as well. – Some programmer dude May 08 '18 at 18:36
  • Maybe this `cin >> comp;` should be removed? – DimChtz May 08 '18 at 18:36
  • Oh, and don't use global variables. There's definitely no need for `comp` to be global here. – Some programmer dude May 08 '18 at 18:36
  • 2
    Lastly, either stop skipping classes, or stop guessing about things (which it kind of seems like you do). [Get a good book or two instead](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude May 08 '18 at 18:37
  • Run your program in the debugger - line by line. Inspect variable contents and behaviour and match that to your expectations. In a word, this is called "debugging" - give it a try. – Jesper Juhl May 08 '18 at 18:42
  • 1
    Before you do anything else, see if you can update your compiler tools. `#include` has the smell of 20+ year old pre-standardization C++ code. C++ has changed a lot since those days and using tools that old makes it hard to find help and post-school employment. – user4581301 May 08 '18 at 18:42
  • Why on earth is `comp` a global variable? Why are you using `rand()` (without even seeding it) rather than the [modern random number facilities](http://en.cppreference.com/w/cpp/numeric/random)? Prefer headers like `` over deprecated ones like ``. `using namespace std;` at global scope, please don't. `randomnumber: string again; ... goto randomnumber;` - no, just No - use a function. Your `% 10` is introducing bias, use [std::uniform_int_distribution](http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution). Etc. – Jesper Juhl May 08 '18 at 18:50
  • 1
    `if((again != "y")||(again != "Y")||(again != "n")||(again !="N"))` Think about this statement carefully. – Lightness Races in Orbit May 08 '18 at 19:08
  • I appreciate the replies. How would I tell my program to jump to another area without using a "goto" in this case? My goal was to have it generate another number if I selected "Y". Again I realize this is probably trivial for most, but it's all new to me. – NomadicAssassin May 09 '18 at 04:25

1 Answers1

0

First lets take a look at what the code should look for your requirements to be fulfilled.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<iostream.h>
//int comp; No need for comp to be a global variable here.
using namespace std;
int main ()
{
    int comp;
    randomnumber://if you were to use goto, the label should have been here.
    comp= 1+(rand() % 10);
    //randomnumber: avoid goto statements at all if possible.
    //string again; No need for it to be a string. 
    char again;
    cout <<"The computer chose this random number: "<< comp << endl;
    //cin >> comp; since you want computer to generate a random no, why ask the  user for it? 
    cout << "Would you like to run this again? Y/N" << endl;
    cin >> again;
    if((again == "y")||(again == "Y"))
    {
        goto randomnumber;
    }
    else if((again == "n")||(again == "N"))
    {
             cout << "OK" << endl;        
    }
    else if((again != "y")||(again != "Y")||(again != "n")||(again !="N"))
    {
             cout << "Please type Y or N" << endl;

    }
    return 0;
 }

Now lets take a look at how to do this in a more non-complex, simple way.

#include<iostream.h> //#include<iostream> if No such file error.
#include<time.h> // for seeding rand()
#include<stdlib.h> // for rand
using namespace std;
int main()
{
    srand(time(NULL)); //seeding rand()
    while(true)
    {
        int comp=1+(rand() % 10);
        cout <<"The computer chose this random number: "<< comp << endl;
        cout<<"Would you like to run this again?Y/N"<< endl;
        char choice;
        cin>>choice;
        if ((choice == 'N')|| (choice =='n'))
        {
            cout<<"OK"<< endl;
            break;
        }
        else
            while(choice!='y' && choice!='Y') // forcing user to enter a valid input.
            {
                cout<<"Please type Y or N" << endl;
                cin>>choice;
            }
    }
    return 0;
}

I hope this helps.

Manu S Pillai
  • 899
  • 8
  • 13
  • While certainly a great improvement, you haven't addressed the potential problems caused by `using namespace std;`. Nor the bias introduced to the random numbers by the use of modulo. Nor the fact that there are *better* random number facilities available these days. I'd also suggest advocating `nullptr` instead of `NULL`. The use of deprecated headers also doesn't get addressed. In short: nice answer, but could be a lot better. IMHO. – Jesper Juhl May 08 '18 at 19:39
  • Btw `srand(time(NULL));` is a horrible seed. `time()` only has second resolution, so anyone running the program within the same second will get the same seed. Also, the time of day is fairly predictable, making a seed based purely on that fairly guessable. – Jesper Juhl May 08 '18 at 19:50
  • Thank you for the advice and example. I'll see to fixing some of my errors. – NomadicAssassin May 09 '18 at 04:22