CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2014
    Posts
    4

    C++ Help Why wont it print anything (functions)

    \\this program is used for the user enter a number and guess the correct number where the int jackpot is the function and analyzes it.It debugs and all but it wont work help please.Im new to programming.
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    using std::srand;
    using std::time;
    using std::rand;


    int jackpot(int a){

    srand((unsigned int)time(0));
    int loop;
    int rand1 =rand() % 100 +1;
    for(loop=0;a==rand1;loop++){
    cout<<"Enter a number:";
    cin>>a;
    if(a<rand1)
    {cout<<"Its too low ";
    }
    else if(a>rand1){cout<<"Its to high";
    }}

    cout<<"You have tried"<<loop<<"\n";
    cout<<"Congratulations";


    }

    int main ()
    {
    cout<<"Hello,Please enter your 3 bets\n";
    int guess1;
    cout<<"Please enter your 1st bet:\n";
    cin>>guess1;

    int jackpot(guess1);

    return 0;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: C++ Help Why wont it print anything (functions)

    Doesn't compile for me. jackpot needs to return a value.

    Please use code tags.

    Why are you prompting for a guess in main, passing that to jackpot, then asking again in jackpot and ignoring the value you got in main?

    Your loop condition is incorrect. The loop will only execute while a == rand1. Typically you'll use a for loop for a known number of iterations. In your case, a while loop would be better.

  3. #3
    Join Date
    Jun 2014
    Posts
    4

    Re: C++ Help Why wont it print anything (functions)

    I found the problem by myself thanks anyways!

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    using std::srand;
    using std::time;
    using std::rand;


    int jackpot(int a){

    srand((unsigned int)time(0));
    int loop;
    int rand1 =rand() % 100 +1;
    for(loop=0;a!=rand1;loop++){
    cout<<"Enter a number:";
    cin>>a;
    if(a<rand1)
    {cout<<"Its too low ";
    }
    else if(a>rand1){cout<<"Its to high";
    }}

    cout<<"You have tried"<<loop<<"times\n";
    cout<<"Congratulations\n\n";
    return a;

    }

    int main ()
    {
    cout<<"Hello,Please enter your 3 bets\n";
    int guess1=0;
    cout<<"Please enter your 1st bet:\n";
    cin>>guess1;


    cout<<jackpot(guess1)<<"\n";


    }

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: C++ Help Why wont it print anything (functions)

    You still have the other problems I mentioned.

    You're prompting for a number in main and not using it, and a while loop is the loop to use here. This isn't a case for a for loop.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: C++ Help Why wont it print anything (functions)

    You're prompting for a number in main and not using it
    It is being used for the first condition test of the for loop.

    Code:
    if(a<rand1)
    {cout<<"Its too low ";
    }
    else if(a>rand1){cout<<"Its to high";
    }
    This code is only executed if a is not equal rand1. If a is not equal, then it must either be less than or greater than. As the first test is for less than, the second test for greater than is not needed.

    Code:
    if (a < rand1)
         cout << "Its too low";
    else 
         cout << "Its too high";
    srand() is used to seed the random number generator is is usually called just once at the start of a program in main().
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured