C++ Help Why wont it print anything (functions)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

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

  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,202

    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,202

    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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,738

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center