0

im having trouble with my output i want to justify it the name and the result but i dont dont how when i enter a number it is not centered......pls help me this is my codes

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>
#include <windows.h>
using namespace std;

void main()
{
	  int cartoon;
	  int voters;
	  int spongebob=0;
	  int patrick=0;
	  int y;//loop
	  char x='*';
	  int t;//total

	 
	  cout<<"Choose your favorite Cartoon Character"<<endl;
	  cout<<"PRESS 1 for spongebob"<<endl;
	  cout<<"PRESS 2 for patrick"<<endl;
	  cout<<"Enter the number of voters:";
	  cin>>voters;

	  for(y=1;y<=voters;y++)
	  {
		  cout<<"Survey for your favorite Cartoon Character:";
		  cin>>cartoon;

		  if(cartoon==1)
		  {
			  spongebob=spongebob+1;
		  }
		  else if (cartoon==2)
		  
			  patrick=patrick+1;
		  }
		  

		  cout<<"Total votes:"<<endl;  
		  x=spongebob/1;
		  cout<<"votes from spongebob:"<<spongebob<<endl;
		  for(t=1;t<=x;t++)
		  {
			  cout<<'*';
			  cout<<endl;
		  }
		   x=patrick/1;
		  cout<<"votes from patrick:"<<patrick<<endl;
		  for(t=1;t<=x;t++)
		  {
			  cout<<'*';
			  cout<<endl;
		  }
		  getch();
	  }

and this is my recent output
i want to justify it the result and the name please im not that good in c++ pls help.. this is my sample:

choose your favorite cartoon character
PRESS 1 for spongebob
PRESS 2 for patrick
Enter the number of voter:5
Survey for your favorite cartoon character:1
Survey for your favorite cartoon character:2
Survey for your favorite cartoon character:1
Survey for your favorite cartoon character:2
Survey for your favorite cartoon character:1
total votes:
Votes from spongebob:3
*
*
*
votes from ptrick:2
*
*

Edited by Narue: added code tags

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Satyrn
0

I am not sure I fully understand what you want to be centered. I believe that you want the star to be centered. I don't believe there is a way to say "Center This" in a console app but there is a way you can go about doing it.

Basic formatting for Standard Output can be found in the <IOMANIP> header which you can read about here...
http://www.cprogramming.com/tutorial/iomanip.html

As for your particular problem you may want to consider tabbing, which can be achieved by the \t escape character so...

cout << "\t \t *";

... would indent two tabs over.

If that method is not sufficient enough you may consider that a standard terminal has the width of 80 characters. You can definitely change this in properties, I certainly do, however most terminals will come up this way. You can calculate the number of spaces you would need...

char CharArray[] = "Center This Text!";
int Num_Spaces=(int)((80-strlen(CharArray))/2);
for(int i=0;i<Num_Spaces;i++)
    cout<<" ";
cout << CharArray;

this subtracts the length of the string from 80 and divides by 2. This is how many spaces you will need on each side. Then the for loop will insert a space that many times.

I did not compile this. I don't write to many console apps anymore. I would suggest forgetting about the accuracy and use tabbing(\t), however if your only centering the '*' then you need 39 spaces to center a single character in most terminals.

Edited by Satyrn: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.