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!!

Recommended Answers

All 4 Replies

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";
      }

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

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

tkud, thank you also!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.