I am new to this website, and coding as well, so please bear with me. I have a program that i'm trying to fortmat the output into a single line. The output should read :

HHHHHTTTTT

Number of Heads: 5
Number of Tails : 5

where the values change per the result of the coin flips.

here is the code:

#include <cstdlib> 
#include <ctime> 
#include <iostream> 
#include <iomanip>

using namespace std;

void flips();											// Function Prototype
						
int count;												//initializing the counters
int countH=0;											
int countT=0;
int flip;

int main() 
{ 
    srand((unsigned)time(0)); 
    
    for(count=0; count<10; count++)
		
	flips();										// Calling function flip()
	
								

	cout <<"\n\n\n\t\tNumber of Heads:  "<<countH<<endl;
	cout <<"\t\tNumber of Tails:  "<<countT<<endl<<endl<<endl; 
	
	
	return 0;
}

	void flips()									// Defination of flip() funtion
	{
		flip = (rand()%2)+1; 
		
		
	if (flip < 2)
		{
			cout <<"H";
			countH++;
		}
	else																
		{
			cout<<"T";
			countT++;
		}
	}

this is what i get for output:

HHHHTHHHTH


Number of Heads: 8
Number of Tails: 2


Press any key to continue . . .

so my question is how do i center the first line ?

thanks.

Recommended Answers

All 3 Replies

When using random numbers there is no guarentee you will get 5 heads and 5 tails.

When using random numbers there is no guarentee you will get 5 heads and 5 tails.

yes, i understand that . The issue is having the output formatted where the "H" and "T" line is center of the page, as one line ... as opposed to the top left of the page. if you run the code you will see what i mean.

thanks

so my question is how do i center the first line ?

From what I understand, you are wanting to center a line of text on the dos console screen.

Assuming that the standard screen will support 80 characters, we can derive the following formula:

40 - (string length / 2) = where you should start outputting your text in order to have a centered appearance.

We can also use the setw() function from <iomanip> to help us with our centering needs:

#include<iomanip>
#include<string>
#include<iostream>

string temp;
int spaces = 0;

if(flip < 2)
{
     temp =+ 'H';
}
else
{
     temp =+ 'T';
}

     spaces = 40-(temp.length() / 2);
     cout << setw(spaces) << temp;

So to summarize our code, we first perform a boolean test to determine which block of code to perform; if flip < 2 then 'H' else 'T'. We then use the plus/equals =+ operator to 'concantinate' (or accumulate) an array of H's and T's into a <string> class object. We then perfrom simple math to determine an offset of the midpoint of our screen in which to start outputting text. We used the setw() function (from <iomanip>) to set the number of preceding white spaces. We use the length() string member function to return the number of characters in the string.

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.