i dunno how to do loops and functions together

This is a discussion on i dunno how to do loops and functions together within the C++ Programming forums, part of the General Programming Boards category; In the below code, it works fine except if u enter "e" or some letter when it asks for the ...

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

    i dunno how to do loops and functions together

    In the below code, it works fine except if u enter "e" or some letter when it asks for the number of dice to roll, it spams u with

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

    and whenever its done rolling dice it does this

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

    ?

    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<<"\n\nEnter a number of 6-sided dice to roll:";
    	 	cin>>num6roll;
    
    	while (num6roll>0)
    		{
    			temproll=(rand() % 6+1);
    			roll6=temproll+roll6;
    			num6roll = num6roll - 1;
    		}
    
    	if (roll6!=0)
    		cout<<"\n\nYou rolled for a total of "<<(roll6)<<"!";
    	else
    		cout<<"Be sure to enter a number of dice...";
    
    	}
    
    
    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.roll them bones\n4.quit";
        	}
        else if(!strcmpi("quit", command))
          {
          	commain=-1;
          }
        else
        	{
          	cout<<"\nEnter help for assistance:";
             commain=0;
          }
    
        return (commain);
    	}
    Last edited by Noobie; 02-03-2003 at 04:13 PM.

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    This was asked just yesterday... same answer: http://www.parashift.com/c++-faq-lit....html#faq-15.3

  3. #3
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    wuts a std::cout ?

  4. #4
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    why is this text box not formatting the code above as I have it arranged in notepad?

  5. #5
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    while ((std::cout << "How old are you? ")
    && !(std::cin >> age))

    what does that ! mean?

  6. #6
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<int>::max(), '\n');
    }

    huh?

    its really not helpful to refer me to an explanation which has a bunch of unexplained stuff

  7. #7
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    My compiler says that cout is not a member of std

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Five posts after each other, That really sucks

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by Noobie
    My compiler says that cout is not a member of std
    That's because you're using <iostream.h>, use <iostream> if
    possible.

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by Noobie
    wuts a std::cout ?
    That's just the the cout function in the std namespace.

  11. #11
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    Wuts the difference between <iostream> and <iostream.h> i get the same error eitherway cout is not a member of std cin is not a member of std whats a std namespace? i dunno...

  12. #12
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by Noobie
    Wuts the difference between <iostream> and <iostream.h> i get the same error eitherway cout is not a member of std cin is not a member of std whats a std namespace? i dunno...
    <iostream> is a newer standard. <iostream.h> could work but
    isn't as good as <iostream>. If your compiler starts ####ting on
    you when you're using <iostream> you have a serious old
    compiler.

  13. #13
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    it doesnt have a problem with iostream, it has a problem with std::cout

  14. #14
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by Noobie
    it doesnt have a problem with iostream, it has a problem with std::cout
    Have you tried this?

    Code:
    <iostream>
    
    using namespace std;

  15. #15
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    put this in your code to check if num6roll is a number

    if(isdigit(num6roll))

    //do stuff
    else
    //do stuff!


    have u put this in there aswell
    after your includes
    put using namespace std;

Page 1 of 3 123 LastLast
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, 01: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, 01:49 PM

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