Thread: i dunno how to do loops and functions together

  1. #16
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    traav!

  2. #17
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by pode
    traav!
    I'm flattered....

  3. #18
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    well that compiles fun if i add that one line, but it still not understand std:cout or std:cingetline but im getting other errors too which isnt suprising since I don't know what any of the tutorial code means or what its doing or how im supposed to use it

  4. #19
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by Noobie
    well that compiles fun if i add that one line, but it still not understand std:cout or std:cingetline but im getting other errors too which isnt suprising since I don't know what any of the tutorial code means or what its doing or how im supposed to use it
    What compiler you have? Post the code that isn't working.

  5. #20
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    here is my code, sorry but I dont know how to format it so it looks like it does in notepad - i now have isdigit but it still has the same crash error if i enter a letter

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    #include <ctype.h>
    #include <limits>
    
    using namespace std;
    
    int commandmain();
    
    void funcroll6(void)
      	{
    	 int roll6=0, num6roll=0, temproll;
    
    	  	cout<<"\n\nEnter a number of 6-sided dice to roll:";
    	 	cin>>num6roll;
    
          if(isdigit(num6roll))
          {
          	cout<<"Be sure to enter a number of dice...";
          }
    	else
       	while (num6roll>0)
    		{
    			temproll=(rand() % 6+1);
    			roll6=temproll+roll6;
    			num6roll = num6roll - 1;
    		}
    		cout<<"\n\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];
    	 int commain=0;
    
        cout << "\n\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:\n1.roll dice\n2.dice\n3.r";
             cout<<"oll them bones\n4.quit";
        	}
        else if(!strcmpi("quit", command))
          {
          	commain=-1;
          }
        else
        	{
          	cout<<"\nEnter help for assistance:";
             commain=0;
          }
    
        return (commain);
    	}

  6. #21
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by Noobie
    here is my code, sorry but I dont know how to format it so it looks like it does in notepad - i now have isdigit but it still has the same crash error if i enter a letter

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    #include <ctype.h>
    #include <limits>
    
    using namespace std;
    
    int commandmain();
    
    void funcroll6(void)
      	{
    	 int roll6=0, num6roll=0, temproll;
    
    	  	cout<<"\n\nEnter a number of 6-sided dice to roll:";
    	 	cin>>num6roll;
    
          if(isdigit(num6roll))
          {
          	cout<<"Be sure to enter a number of dice...";
          }
    	else
       	while (num6roll>0)
    		{
    			temproll=(rand() % 6+1);
    			roll6=temproll+roll6;
    			num6roll = num6roll - 1;
    		}
    		cout<<"\n\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];
    	 int commain=0;
    
        cout << "\n\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:\n1.roll dice\n2.dice\n3.r";
             cout<<"oll them bones\n4.quit";
        	}
        else if(!strcmpi("quit", command))
          {
          	commain=-1;
          }
        else
        	{
          	cout<<"\nEnter help for assistance:";
             commain=0;
          }
    
        return (commain);
    	}
    It compiles without a glitch on my compiler, Wich one do you have?

  7. #22
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    It compiles fine, run the program, it doesnt work right. Enter e for a number of dice, it crashes. Also note how if u roll the dice, it says

    Enter a command:
    Enter help for assistance:
    Enter a command:
    Last edited by Noobie; 02-03-2003 at 05:10 PM.

  8. #23
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Do you understand this code? Or is this a tutorial? Learn this
    code well until you know how it works, Because i can't explain
    everything step by step here!

  9. #24
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    Well, I understood it enough to write it but I obviously don't understand it all because its got errors.

  10. #25
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    It compiles fine
    its got errors
    Come again?

  11. #26
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    basically if something isnt in this code, I probably have no idea what it is

  12. #27
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    it compiles fine, but it doesnt work right when i run it-as I said

    Entering a letter for the number of dice causes it to crash

    After rolling dice, it states

    Enter help for assistance:
    Enter a command:
    Enter help for assistance:_

  13. #28
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    #include <stdlib.h>
    #include <time.h>
    #include <ctype.h>

    Change those to

    #include <cstdlib>
    #include <ctime>
    #include <cctype>


    Also, isdigit is not going to work in your situation... you need to go here and READ THROUGH CAREFULLY.

    Remove if (isdigit(num6roll)) ...
    and add
    Code:
    while ((std::cout << "Enter a number of 6-sided dice to roll:  ")
              && !(std::cin >> num6roll)) 
    {
         std::cout << "That's not a number; ";
         std::cin.clear();
         std::cin.ignore(std::numeric_limits<int>::max(), '\n');
    }

  14. #29
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Didn't we already go over this? isdigit first of all only checks to see if a character is a in the ASCII range of '0' to '9' (decimal values 48-57 i think).

    This means isdigit(47) and isdigit(1) will return false while isdigit(48) will return true.

    Checking to see if input is correct is slightly advanced so its unlikely you'll understand most of what needs to be done at this point. One way is to use chars instead of ints, but then you'll need to figure out how to change them back. Another way is to do something like this:
    Code:
    bool good=false;
    while (!good)
    {
    	cin>>choice;
    	if (cin.fail())
    	{
    	   while (getchar()!='\n') {};
    	   cin.clear();
    	}
    	else
    	   good=true;
    }
    You are trying to do things without learning the basics first. Start with the basics and then everything else will slowly begin to make sense.
    Last edited by PJYelton; 02-03-2003 at 05:25 PM.

  15. #30
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    std::cout <---My Borland 5.02 compiler is not understanding this. In addition, Im not understanding it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with functions and for loops
    By slava in forum C Programming
    Replies: 1
    Last Post: 11-08-2008, 01:30 PM
  2. Loops or recursive functions?
    By mariano_donati in forum C Programming
    Replies: 5
    Last Post: 05-12-2008, 12:41 PM
  3. Loops in functions?
    By AmbliKai in forum C Programming
    Replies: 16
    Last Post: 11-29-2007, 06:33 AM
  4. new to Void functions and loops
    By Loraine13 in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2001, 10:00 AM
  5. Loops OR Functions!! HELP WITH THIS CODE!!!!!!!!!!
    By WIshIwasGooD in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2001, 12:49 PM