Lesson 17 assignment problem

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Psychoman
Posts: 36
Joined: June 13th, 2013, 2:12 pm

Lesson 17 assignment problem

Post by Psychoman » July 19th, 2013, 1:40 pm

Hello, I have a problem with L17 homework.The problem is if I run the program normal it randoms some kind of number and later all numbers are the same ( f.e. roll 5 dice : 1,1,1,1,1 ), something like that.But if I am trying to debug the program it works just fine and this makes me anxious.I just get a feeling that something is wrong with pointers :? .

Code: Select all

int StrToInt( const char* InputStr )
{
	int maxLen = 0;
	int number = 0;
	for( ;*InputStr != '\0' ;InputStr++ )
	{
		maxLen++;
	}
        //Point on the last element
	InputStr--;
	for( int i = 0; i < maxLen ;InputStr--,i++ )
	{
		if( *InputStr < 57 || *InputStr > 48 && *InputStr!= '\0' )
		{
			char temp = *InputStr;
                        // -48 - change from char to int
                        // pow is for f.e 123 = 100*1 + 10*2 + 1*3
			number += ( temp - 48 )*pow( (float) 10,i );
		}
		else 
		{
			continue;
		}
	}
	return number;
}

void IntToStr( int num,char* buf,int buflen )
{
	for( int i = buflen;i>=0;i--,buf++ )
	{
                // leaves first number
		int temp = num / pow( (float) 10, i );
		// minuses this first number f.e. 123  - 100 = 23
                num -= temp * pow( (float) 10, i );
		//Changes to character
                *buf = temp + 48;
	}
	*buf = '\0';
}
void main()
{
	char mes[] = "Enter how many dices to roll: ";
	const int lengthNumber = 4;
	char NumberRolles[lengthNumber];
	
	GetString( NumberRolles, lengthNumber );
	
	PutString( mes );
	PutString( NumberRolles );
	PutString( "\n" );
        PutString( "Dice numbers: \n" );

        const int numbRolles = StrToInt( NumberRolles );
	char StrDice[2];
	int sum = 0;
	char DisplaySum [4];
	for( int i = 0;i<numbRolles;i++ )
	{
		srand( (unsigned int) time( NULL ) );
		int temp =  rand() % 6 + 1;
		sum += temp;
		IntToStr(temp ,StrDice, 0 );
		
		PutString( StrDice );
		PutString( " " );
	}
		
	IntToStr( sum ,DisplaySum, 2 );

	PutString( "\n" );
	PutString( "Total Sum is: " );
	PutString( DisplaySum );
	PutString( "\n" );
}

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Lesson 17 assignment problem

Post by LuisR14 » July 19th, 2013, 3:38 pm

why you using srand every run of the for loop?, it should just be used at the start of main() :)
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

Psychoman
Posts: 36
Joined: June 13th, 2013, 2:12 pm

Re: Lesson 17 assignment problem

Post by Psychoman » July 19th, 2013, 6:20 pm

I want to go kill myself , such .... I debugged it for 20 min.I got it why random does the same number every time.

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Lesson 17 assignment problem

Post by LuisR14 » July 19th, 2013, 10:42 pm

well when you're debugging the time elapsed is greater so srand gives off different rand numbers, but when you're not debugging it runs at a fraction of a second (too fast for the rand generator to notice since time() returns about the same value for the rand generator)
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

Psychoman
Posts: 36
Joined: June 13th, 2013, 2:12 pm

Re: Lesson 17 assignment problem

Post by Psychoman » July 21st, 2013, 5:04 pm

Yeap thx I thought of that)

Post Reply