loop problem

This is a discussion on loop problem within the C++ Programming forums, part of the General Programming Boards category; This code works for like one iteration and then it quits working-I donno why. Code: #include <iostream.h> #include <stdlib.h> #include ...

  1. #1
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167

    loop problem

    This code works for like one iteration and then it quits working-I donno why.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    #include <ctype.h>
    
    int commandmain();
    
    void funcroll6(void)
    {
    int roll6=0, num6roll=0, temproll;
    
    cout<<"Enter a number of 6-sided dice to roll:";
    cin>>num6roll;
    
    while (num6roll>0)
    {
    temproll=(rand() % 6+1);
    roll6=temproll+roll6;
    num6roll = num6roll - 1;
    }
    
    cout<<"\nYou rolled for a total of "<<(roll6)<<"!";
    
    }
    
    
    int main()
    {
    
    time_t seconds;
    
    time(&seconds);
    
    srand((unsigned int) seconds);
    
    
    while (commandmain()==0)
    commandmain();
    
    return 0;
    }
    
    int commandmain()
    {
    
    char command[256], commain=0;
    
    cout<<"\nEnter a command:";
    cin.getline(command, 256, '\n');
    
    if(!strcmpi("dice", command))
    {
    funcroll6();
    }
    else if(!strcmpi("roll dice", command))
    {
    funcroll6();
    }
    else if(!strcmpi("roll them bones", command))
    {
    funcroll6();
    }
    else if(!strcmpi("help", command))
    {
    cout<<"\nSince this is a dice program, try ''roll dice'' or ''dice'' or ''roll them bones''...And yes, the dragon is out sick.";
    }
    else if(!strcmpi("quit", command))
    {
    commain=-1;
    }
    else
    {
    cout<<"\nEnter help for assistance:";
    commain=0;
    }
    
    return (commain);
    }

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Putting your source in code tags when it is not indented is not exactly ideal.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Use breakpoints and determine where the loop fails. Variable commain is declared as a char, not int. Make it an int.

    Again, determine where the loop fails first then post.

    Kuphryn

  4. #4
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    its failing right after it rolls the dice

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Do you mean this line?

    Code:
    else if(!strcmpi("roll dice", command))
       funcroll6();
       // Failure
    Kuphryn

  6. #6
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    are u implying that something is wrong with that?

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    No. I need to know where the program fails, i.e. line #.

    Kuphryn

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    I had to add string.h to get it to compile, but after I did that, it ran, and allowed me to try all of the command options.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    It runs once, I can do any command, but after I roll dice it gives me Enter a command: Enter help for help.. Enter a command: and then none of the commands work anymore

  10. #10
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    hello?

  11. #11
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Originally posted by Noobie
    hello?
    Please don't post one liners, specially when they are not needed. Patience is a virtue.

    Run it to where it stops, right click in the window and select "select all". When it highlights press enter, then come here and paste in code tags. It'll show use the state of the screen at programs error.

  12. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    First of all, responding to your own thread saying 'hello' is just plain annoying. People will respond when they feel they should, not when you feel they should.

    Secondly. Look at the logic of your code here. commain is default set to 0 on function call. You do your input, it does it, then it exist's your while loop in main, and calls your function once again, takes input, and then quits.

    Learn how to make a flow chart.

  13. #13
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    The current code is below

    [code]
    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    C:\BC5\BIN\stuff>num6roll.exe

    Enter a command:h

    Enter help for assistance:
    Enter a command:help

    Since this is a dice program, try ''roll dice'' or ''dice'' or ''roll them bones
    ''
    Enter a command:dice
    Enter a number of 6-sided dice to roll:36

    You rolled for a total of 114!
    Enter a command:
    Enter help for assistance:
    Enter a command:dice
    Enter a number of 6-sided dice to roll:36

    You rolled for a total of 119!
    Enter a command:
    Enter help for assistance:
    Enter a command:
    [code]
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    #include <ctype.h>
    
    int commandmain();
    
    void funcroll6(void)
    {
    int roll6=0, num6roll=0, temproll;
    
    cout<<"Enter a number of 6-sided dice to roll:";
    cin>>num6roll;
    
    while (num6roll>0)
    {
    temproll=(rand() % 6+1);
    roll6=temproll+roll6;
    num6roll = num6roll - 1;
    }
    
    cout<<"\nYou rolled for a total of "<<(roll6)<<"!";
    
    }
    
    
    int main()
    {
    
    time_t seconds;
    
    time(&seconds);
    
    srand((unsigned int) seconds);
    
    
    while (commandmain()==0)
    commandmain();
    
    return 0;
    }
    
    int commandmain()
    {
    
    char command[256], commain=0;
    
    cout<<"\nEnter a command:";
    cin.getline(command, 256, '\n');
    
    if(!strcmpi("dice", command))
    {
    funcroll6();
    }
    else if(!strcmpi("roll dice", command))
    {
    funcroll6();
    }
    else if(!strcmpi("roll them bones", command))
    {
    funcroll6();
    }
    else if(!strcmpi("help", command))
    {
    cout<<"\nSince this is a dice program, try ''roll dice'' or ''dice'' or ''roll them bones''";
    }
    else if(!strcmpi("quit", command))
    {
    commain=-1;
    }
    else
    {
    cout<<"\nEnter help for assistance:";
    commain=0;
    }
    
    return (commain);
    }

  14. #14
    Registered User
    Join Date
    Jan 2003
    Posts
    14
    I dont know but this sounds awfully similar to the problem with cin.ignore. Try putting a cin.ignore(257, '\n'); before you ask for a command. I was taught to use cin.get so i dunno but that was always a problem for us.
    ~fin

  15. #15
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>>
    It runs once, I can do any command, but after I roll dice it gives me Enter a command: Enter help for help.. Enter a command: and then none of the commands work anymore
    <<<

    As I said before, that is not the behaviour I see. All I did to make your code compile was add string.h, when I run it, it allows me to roll dice as often as I like. I have attached a bmp of it doing just that...
    Attached Files Attached Files
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21