Hi! I'm really new to C++ and posting to forums so please excuse any mistakes I may make.

I have to write a program that simulates throwing 2 die and printing their sum 25 times, seven sums per line. I'm stuck on the "seven sums per line." Do I need to do a separate loop for this? When I try to add something it causes an error to the srand.

This is how I started it:

#include <iostream>
#include <time.h>

using namespace std ;

int main()
{

	srand(time(0)) ;
	int count = 0 ;
	const int NUMBER_OF_SUMS_PER_LINE = 7 ;
			
	for (count = 0; count < 25; ++count)
	{	
		int die1 = rand() % 7 ;
		int die2 = rand() % 7 ;
		int answer = die1 + die2 ;
		cout << die1 << " + " << die2 << " = " << answer << ", " ;
	}
		
	

return 0 ;

}

Thank you in advance!!

Another way of reading "7 times per line" is "output a newline every 7 times". You could do this with a separate counter (in the same loop though!) that counts to 7, outputs a newline and then is reset. Or you might use the % operator with the current loop counter.

if ( count % 7 == 6 )
      {
         cout << "\n";
      }

Edited 7 Years Ago by Dave Sinkula: Good lord for editing and re-editing. :(

I have modified your code to satisfy the problem..

#include <iostream>
#include <time.h>

using namespace std ;

int main()
{

	srand(time(0)) ;
	int count = 0 ;
	const int NUMBER_OF_SUMS_PER_LINE = 7 ;		
	for (count = 0; count < 25; ++count)
	{	
		int die1 = rand() % 7 ;
		int die2 = rand() % 7 ;
		int answer = die1 + die2 ;
       for(int i=0;i<NUMBER_OF_SUMS_PER_LINE;i++){
                cout<<answer<<" ";
	}
       cout<<endl;
  }
		
	

return 0 ;

}

Please, if this has satisfied you, mark this thread as solved.
Thanks

"Show me your code and I will tell you who you are."---Tkud

Edited 7 Years Ago by tkud: n/a

Dave, thank you so much! I was just not getting it. Works like a charm!

This question has already been answered. Start a new discussion instead.