simple code bugs for a Console aplication
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: simple code bugs for a Console aplication

Hybrid View

  1. #1
    Join Date
    Jul 2011
    Posts
    4

    simple code bugs for a Console aplication

    I am getting a strange bug in my code. I only started coding verry recently and was wondering if someone could explain my problem in very basic terms. this is my code:

    Code:
    #include<iostream>
    #include<string>
    #include<ctime>
    #include<cstdlib>
    
    using namespace std;
    
    	int strength;
    	int endurance;
    	int dexterity;
    	int inteligence;
    	int stamina;
    	float health;
    	float essence;
    	float damageMultiplier;
    	float carryCapacity;
    	float critical;
    	float damageSkew;
    	
    int main()
    {
    	int atributes=0;
    	int assign=0;
    	string myFirstName("bob");
    	string myLastName("");
    	string space(" ");
    
    	cout<<"Character's first name? ";
    	cin>>myFirstName;
    	cout<<"Character's last name? ";
    	cin>>myLastName;
    	string myName=myFirstName+space+myLastName;
    	cout<<"\nyour name is "<<myName<<"?\n";
    
    	cout<<"there are 4 basic atributes. they are:\n1-strength\ndetermines your base damage and carry limit";
    	cout<<"\n2-endurance\ndetermines your health and stamina";
    	cout<<"\n3-dexterity\nreduces miss and mishap chance for all attacks and spells and increases critical chance";
    	cout<<"\n4-inteligence\ndetemines your essence and essence regeneration as well as spell effectiveness";
    	while(atributes<=3)
    	{
    	srand(static_cast<unsigned int>(time(0)));
    	int rolled=(rand()&#37;75+26);
    	while (rolled!=0)
    	{
    		cout<<"\nyou rolled a "<<rolled<<" what atribute do you want this assigned to"<<endl;
    		assign=0;
    		cin>>assign;
    	switch(assign)
    		{
    	case 1:
    		if(strength==0)
    			{
    			cout<<"you chose strength.";
    			strength=rolled;
    			++atributes;
    			rolled=0;
    			break;
    			}
    		else if (strength !=0)
    			{
    			cout<<"you already assigned strength. chose another.\n";
    			break;
    			}
    	case 2:
    		if(endurance==0)
    			{
    			cout<<"you chose endurance.";
    			endurance=rolled;
    			++atributes;
    			rolled=0;
    			break;
    			}
    		else if(endurance!=0)
    			{
    			cout<<"you already assigned endurance. chose another.\n";
    			break;
    			}
    	case 3:
    		if(dexterity==0)
    			{
    			cout<<"you chose dexterity.";
    			dexterity=rolled;
    			++atributes;
    			rolled=0;
    			break;
    			}
    		else if(dexterity!=0)
    			{
    			cout<<"you already assigned dexterity. chose another.\n";
    			break;
    			}
    	case 4:
    		if(inteligence==0)
    			{
    			cout<<"you chose inteligence.";
    			inteligence=rolled;
    			++atributes;
    			rolled=0;
    			break;
    			}
    		else if(inteligence!=0)
    			{
    			cout<<"you already chose inteligence. chose another.\n";
    			break;
    			}
    	default:
    			{
    			cout<<"thats not an option. chose another.\n";
    			break;
    			}
    		}
    		}
    	}
    	damageMultiplier=strength/10.0;
    	carryCapacity=(strength+endurance)*2.5;
    	stamina=endurance*1000.0;
    	essence=inteligence*(10.0-((100.0-inteligence)/100.0));
    	health=(stamina/10)+(essence/10);
    	critical=dexterity/5;
    	cout<<"\n"<<myName<<", ";
    	cout<<"your atributes are:\n";
    	cout<<"strength "<<strength<<"\n";
    	cout<<"endurance "<<endurance<<"\n";
    	cout<<"dexterity "<<dexterity<<"\n";
    	cout<<"inteligence "<<inteligence<<"\n";
    	cout<<"your basic abilities are:\n";
    	cout<<"health "<<health<<"\n";
    	cout<<"essence "<<essence<<"\n";
    	cout<<"stamina "<<stamina<<"\n";
    	cout<<"base damage multiplier "<<damageMultiplier<<"\n";
    	cout<<"Carry capacity "<<carryCapacity<<"\n";
    	cout<<"you will have a critical hit about %"<<critical<<"of the time.\n";
    	cout<<"wow, you suck at rolling. bye."<<endl;
    	return 0;
    }
    there are actualy two bugs I'm encountering in this code the first is that when the user puts in a value of an incorect type the program enters an infinite loop. the second bug is that when the user puts in a set of values seperated by spaces when prompted each one after the first is assigned to the next variable in the code.

    I Just started coding recently so if the errors seem self explanitory please explain them anyway. Also if this seems too low level for this site could you please recomend somewhere else I could find help.
    Last edited by Zeroandzerox; July 27th, 2011 at 01:13 PM. Reason: adding code tags

  2. #2
    Join Date
    Aug 2009
    Posts
    439

    Re: simple code bugs for a Console aplication

    Repost with code tags to preserve indentation and formatting.

  3. #3
    Join Date
    Jul 2011
    Posts
    4

    Re: simple code bugs for a Console aplication

    sorry
    Code:
    #include"Character Creator.h"
    
    using namespace std;
    
    	int strength;
    	int endurance;
    	int dexterity;
    	int inteligence;
    	int stamina;
    	float health;
    	float essence;
    	float damageMultiplier;
    	float carryCapacity;
    	float critical;
    	float damageSkew;
    	
    int main()
    {
    	int atributes=0;
    	int assign=0;
    	string myFirstName("bob");
    	string myLastName("");
    	string space(" ");
    
    	cout<<"Character's first name? ";
    	cin>>myFirstName;
    	cout<<"Character's last name? ";
    	cin>>myLastName;
    	string myName=myFirstName+space+myLastName;
    	cout<<"\nyour name is "<<myName<<"?\n";
    
    	cout<<"there are 4 basic atributes. they are:\n1-strength\ndetermines your base damage and carry limit";
    	cout<<"\n2-endurance\ndetermines your health and stamina";
    	cout<<"\n3-dexterity\nreduces miss and mishap chance for all attacks and spells and increases critical chance";
    	cout<<"\n4-inteligence\ndetemines your essence and essence regeneration as well as spell effectiveness";
    	while(atributes<=3)
    	{
    	srand(static_cast<unsigned int>(time(0)));
    	int rolled=(rand()%75+26);
    	while (rolled!=0)
    	{
    		cout<<"\nyou rolled a "<<rolled<<" what atribute do you want this assigned to"<<endl;
    		assign=0;
    		cin>>assign;
    	switch(assign)
    		{
    	case 1:
    		if(strength==0)
    			{
    			cout<<"you chose strength.";
    			strength=rolled;
    			++atributes;
    			rolled=0;
    			break;
    			}
    		else if (strength !=0)
    			{
    			cout<<"you already assigned strength. chose another.\n";
    			break;
    			}
    	case 2:
    		if(endurance==0)
    			{
    			cout<<"you chose endurance.";
    			endurance=rolled;
    			++atributes;
    			rolled=0;
    			break;
    			}
    		else if(endurance!=0)
    			{
    			cout<<"you already assigned endurance. chose another.\n";
    			break;
    			}
    	case 3:
    		if(dexterity==0)
    			{
    			cout<<"you chose dexterity.";
    			dexterity=rolled;
    			++atributes;
    			rolled=0;
    			break;
    			}
    		else if(dexterity!=0)
    			{
    			cout<<"you already assigned dexterity. chose another.\n";
    			break;
    			}
    	case 4:
    		if(inteligence==0)
    			{
    			cout<<"you chose inteligence.";
    			inteligence=rolled;
    			++atributes;
    			rolled=0;
    			break;
    			}
    		else if(inteligence!=0)
    			{
    			cout<<"you already chose inteligence. chose another.\n";
    			break;
    			}
    	default:
    			{
    			cout<<"thats not an option. chose another.\n";
    			break;
    			}
    		}
    		}
    	}
    	damageMultiplier=strength/10.0;
    	carryCapacity=(strength+endurance)*2.5;
    	stamina=endurance*1000.0;
    	essence=inteligence*(10.0-((100.0-inteligence)/100.0));
    	health=(stamina/10)+(essence/10);
    	critical=dexterity/5;
    	cout<<"\n"<<myName<<", ";
    	cout<<"your atributes are:\n";
    	cout<<"strength "<<strength<<"\n";
    	cout<<"endurance "<<endurance<<"\n";
    	cout<<"dexterity "<<dexterity<<"\n";
    	cout<<"inteligence "<<inteligence<<"\n";
    	cout<<"your basic abilities are:\n";
    	cout<<"health "<<health<<"\n";
    	cout<<"essence "<<essence<<"\n";
    	cout<<"stamina "<<stamina<<"\n";
    	cout<<"base damage multiplier "<<damageMultiplier<<"\n";
    	cout<<"Carry capacity "<<carryCapacity<<"\n";
    	cout<<"you will have a critical hit about %"<<critical<<"of the time.\n";
    	cout<<"wow, you suck at rolling. bye."<<endl;
    	return 0;
    }

  4. #4
    Join Date
    Aug 2009
    Posts
    439

    Re: simple code bugs for a Console aplication

    You will want to add logic to check the input. So, if you are expecting a number you can use isdigit(). If you want a character, a-z, A-Z, you can use isalpha() to check the input value. Each of these functions checks a character at a time. Another issue I see is that you are calling srand() each time you loop through your first while loop. You only need to seed once. Finally, when you ask for input using cin, you are working with a stream. So, if the user types "10 15 10" these are three separate values. These get put on the stream. So, using
    Code:
    cin >> foo
    (foo being an int) will strip 10 from the stream leaving "15 10". The next time you do
    Code:
    cin >> foo
    the stream isn't empty, so it automatically grabs something else on the stream. This is why it doesn't prompt you.

  5. #5
    Join Date
    Jul 2011
    Posts
    4

    Re: simple code bugs for a Console aplication

    thanks!! To follow up I was wondering if there was anything I could do about the values being added to the stream. Is there any way to clear it?

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    Re: simple code bugs for a Console aplication

    Quote Originally Posted by Zeroandzerox View Post
    there are actualy two bugs I'm encountering in this code the first is that when the user puts in a value of an incorect type the program enters an infinite loop.
    See http://www.parashift.com/c++-faq-lit....html#faq-15.2
    Quote Originally Posted by Zeroandzerox View Post
    thanks!! To follow up I was wondering if there was anything I could do about the values being added to the stream. Is there any way to clear it?
    You can let the stream skip a bunch of input characters (see 15.3 in above link), but it may be easier to always read an entire line from the input and read what you need from that.
    Code:
    #include <iostream>
    #include <sstream>
    int main()
    {
        std::string line;
        std::getline(std::cin, line);
        // get input from line
        std::istringstream iss(line);
        int number;
        if (iss >> number) {
            // ok
        }
        else {
            // input is not a number
        }
    }
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  7. #7
    Join Date
    Jul 2011
    Posts
    4

    Talking Re: simple code bugs for a Console aplication

    here is my new code.
    Code:
    #include"Character Creator.h"
    
    using namespace std;
    
    	int assign=0;
    	int strength;
    	int endurance;
    	int dexterity;
    	int inteligence;
    	int stamina;
    	float health;
    	float essence;
    	float damageMultiplier;
    	float carryCapacity;
    	float critical;
    	float damageSkew;
    	
    int main()
    {
    	int atributes=0;
    	string myName("bob");
    	string line("");
    
    	cout<<"Character's name? ";
    	getline(cin, line);
        istringstream iss(line);
    	myName=line;
    	cout<<myName<<", ";
    
    
    	cout<<"there are 4 basic atributes. they are:\n1-strength\ndetermines your base damage and carry limit";
    	cout<<"\n2-endurance\ndetermines your health and stamina";
    	cout<<"\n3-dexterity\nreduces miss and mishap chance for all attacks and spells and increases critical chance";
    	cout<<"\n4-inteligence\ndetemines your essence and essence regeneration as well as spell effectiveness";
    	srand(static_cast<unsigned int>(time(0)));
    	while(atributes<=3)
    	{
    	int rolled=(rand()%75+26);
    	cout<<"\nyou rolled a "<<rolled<<" what atribute do you want this assigned to"<<endl;
    	while (rolled!=0)
    		{
    		while(!(cin>>assign))
    		{
    			cin.clear();
    			cin.ignore(numeric_limits<streamsize>::max(), '\n');
    			assign=0;
    			break;
    		}
    		switch(assign)
    				{
    	case 1:
    		if(strength==0)
    			{
    			cout<<"you chose strength.";
    			strength=rolled;
    			++atributes;
    			rolled=0;
    			break;
    			}
    		else if (strength !=0)
    			{
    			cout<<"you already assigned strength. chose another.\n";
    			break;
    			}
    	case 2:
    		if(endurance==0)
    			{
    			cout<<"you chose endurance.";
    			endurance=rolled;
    			++atributes;
    			rolled=0;
    			break;
    			}
    		else if(endurance!=0)
    			{
    			cout<<"you already assigned endurance. chose another.\n";
    			break;
    			}
    	case 3:
    		if(dexterity==0)
    			{
    			cout<<"you chose dexterity.";
    			dexterity=rolled;
    			++atributes;
    			rolled=0;
    			break;
    			}
    		else if(dexterity!=0)
    			{
    			cout<<"you already assigned dexterity. chose another.\n";
    			break;
    			}
    	case 4:
    		if(inteligence==0)
    			{
    			cout<<"you chose inteligence.";
    			inteligence=rolled;
    			++atributes;
    			rolled=0;
    			break;
    			}
    		else if(inteligence!=0)
    			{
    			cout<<"you already chose inteligence. chose another.\n";
    			break;
    			}
    	default:
    			{
    			cout<<"thats not an option. chose another.\n";
    			break;
    			}
    			}
    	}		
    	}
    	damageMultiplier=strength/10.0;
    	carryCapacity=(strength+endurance)*2.5;
    	stamina=endurance*1000.0;
    	essence=inteligence*(10.0-((100.0-inteligence)/100.0));
    	health=(stamina/10)+(essence/10);
    	critical=dexterity/5;
    	cout<<"\n"<<myName<<", ";
    	cout<<"your atributes are:\n";
    	cout<<"strength "<<strength<<"\n";
    	cout<<"endurance "<<endurance<<"\n";
    	cout<<"dexterity "<<dexterity<<"\n";
    	cout<<"inteligence "<<inteligence<<"\n";
    	cout<<"your basic abilities are:\n";
    	cout<<"health "<<health<<"\n";
    	cout<<"essence "<<essence<<"\n";
    	cout<<"stamina "<<stamina<<"\n";
    	cout<<"base damage multiplier "<<damageMultiplier<<"\n";
    	cout<<"Carry capacity "<<carryCapacity<<"\n";
    	cout<<"you will have a critical hit about %"<<critical<<"of the time.\n";
    	cout<<"wow, you suck at rolling. bye."<<endl;
    	return 0;
    }
    now to make it do something fun...

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