Anybody know C++?

Discussion in 'Computers & Tech' started by PosterBoy, Sep 16, 2016.

  1. PosterBoy

    PosterBoy New Member

    Joined:
    Aug 10, 2016
    Messages:
    143
    Likes Received:
    3
    Trophy Points:
    0
    Why is this loop an infinite loop?
    int main ()
    {

    int number = 0, // This variable contains the value from the function getNum()
    randNum = 0; // This variable contains the value from the the function getRand()

    do

    {
    while (number != randNum)
    {
    randNum = getRand();
    number = getNum();

    if (number == 1 && randNum == 2)
    cout << "The computer chose paper, you chose rock. You lose!\n";
    if (number == 1 && randNum == 3)
    cout << "The computer chose scissors, you chose rock. You win!\n";
    if (number == 2 && randNum == 1)
    cout << "The computer chose rock, you chose paper. You win!\n";
    if (number == 2 && randNum == 3)
    cout << "The computer chose scissors, you chose paper. You lose!\n";
    if (number == 3 && randNum == 1)
    cout << "The computer chose rock, you chose scissors. You lose!\n";
    if (number == 3 && randNum == 2)
    cout << "The computer chose paper, you chose scissors. You win!\n";
    }
    cout << "You tied with the computer!\n";
    }
    {
    while (number < 4)



    This is just a code snippet obviously, if anybody would like to see more just let me know. Its for a console application.
     
  2. smallblue

    smallblue Well-Known Member

    Joined:
    Jan 20, 2013
    Messages:
    4,380
    Likes Received:
    570
    Trophy Points:
    113

    Let's get rid of the non-pertinent code first:


    do

    {
    while (number != randNum)
    {
    randNum = getRand();
    number = getNum();
    }

    }
    while (number < 4)

    What are the return values ranges of getRand() and getNum()?

    Does getNum() ever return a number greater than 3, does getRand() ever return a number greater than 3? If not then you will loop forever.

    Your while loop will loop until randNum == number, and your do while will loop until number is greater than three.
     
  3. DarkSkies

    DarkSkies Well-Known Member

    Joined:
    Dec 15, 2014
    Messages:
    4,522
    Likes Received:
    583
    Trophy Points:
    113
    I don't know c++ but I do see that you are missing a "counter" or something that advances the loop or exits the loop. Let me give you an example to explain what you are doing and why it'll never exit:

    a=2
    b=2

    Do
    While (a+b==4){
    'Do something
    }loop


    ^^see 2+2 will always be 4. You have to write this so that the code goes on to the next iteration of the variable(s)!

    Somewhere in your loop, tell the program how to iterate. For example, going through a recordset, the next iteration is the next record and is moved with something like "next". In your example, you have to find a way to get different values for your "number" variable. For example, add some value to your "number" variable at each loop pass. Otherwise, you are looping the same values and you'll never escape.
     
  4. ArmySoldier

    ArmySoldier Well-Known Member Past Donor

    Joined:
    Sep 11, 2014
    Messages:
    32,222
    Likes Received:
    12,253
    Trophy Points:
    113
    How did you learn C++? I already finished college but would love to learn it.
     
  5. LangleyMan

    LangleyMan Well-Known Member

    Joined:
    Nov 14, 2017
    Messages:
    44,923
    Likes Received:
    12,501
    Trophy Points:
    113
    Gender:
    Male
    Get a book and teach it to yourself.
     
  6. ArmySoldier

    ArmySoldier Well-Known Member Past Donor

    Joined:
    Sep 11, 2014
    Messages:
    32,222
    Likes Received:
    12,253
    Trophy Points:
    113
    Thanks for responding to a post over a year old, and stating the ****ing obvious.

    There are other things one must learn prior to even learning C++ from a book.
     
  7. LangleyMan

    LangleyMan Well-Known Member

    Joined:
    Nov 14, 2017
    Messages:
    44,923
    Likes Received:
    12,501
    Trophy Points:
    113
    Gender:
    Male
    Not much. I taught computers for years.
     
  8. ArmySoldier

    ArmySoldier Well-Known Member Past Donor

    Joined:
    Sep 11, 2014
    Messages:
    32,222
    Likes Received:
    12,253
    Trophy Points:
    113
    Good for you.
     
  9. LangleyMan

    LangleyMan Well-Known Member

    Joined:
    Nov 14, 2017
    Messages:
    44,923
    Likes Received:
    12,501
    Trophy Points:
    113
    Gender:
    Male
    Have you learned C++ yet?
     
  10. ArmySoldier

    ArmySoldier Well-Known Member Past Donor

    Joined:
    Sep 11, 2014
    Messages:
    32,222
    Likes Received:
    12,253
    Trophy Points:
    113
    Nope. Learning SQL for an unrelated reason. Putting everything on hold because I ship out for different military training again for a few months.
     
    DoctorWho likes this.
  11. miketx

    miketx Banned

    Joined:
    Oct 28, 2015
    Messages:
    456
    Likes Received:
    135
    Trophy Points:
    43
    Because the variable "number" is always less than 4.
     
  12. LangleyMan

    LangleyMan Well-Known Member

    Joined:
    Nov 14, 2017
    Messages:
    44,923
    Likes Received:
    12,501
    Trophy Points:
    113
    Gender:
    Male
    Good for you on learning SQL.

    BTW, thanks for your service.
     
  13. ArmySoldier

    ArmySoldier Well-Known Member Past Donor

    Joined:
    Sep 11, 2014
    Messages:
    32,222
    Likes Received:
    12,253
    Trophy Points:
    113
    Thanks for your support
     
    DoctorWho likes this.
  14. arborville

    arborville Well-Known Member

    Joined:
    Sep 13, 2013
    Messages:
    2,725
    Likes Received:
    620
    Trophy Points:
    113
    Gender:
    Female
    You could begin with YouTube videos. You can find excellent groups of videos tailored to beginners. I learned C++ years ago but mostly use Java now.

     
  15. ArmySoldier

    ArmySoldier Well-Known Member Past Donor

    Joined:
    Sep 11, 2014
    Messages:
    32,222
    Likes Received:
    12,253
    Trophy Points:
    113
    which do you like more?
     

Share This Page