C machine problem

closed account (yC4jURfi)
does anyone know how to do this layout in C? the inputs are account no, depositors name and account balance.... while the interest would be 10% from the account balance and total acct balance is the sum of account balance and interest... pls help me, i'm a first year college and just beginning to learn it . the layout should be the same as below (pls imagine it as if it was centered)

ABC BANK
XBC Ave., City
DEPOSITORS’STATEMENT OF ACCOUNT
AcctNo. Depositor Name AccountBalance InterestEarned TotalAccountBalance
(int type)(string of 25 characters)(float type) (float type) (float type)

You start with something like
1
2
3
4
5
6
7
struct account {
  int account_number;
  char depositor_name[25];
  float balance;
  float interest_earned;
  float account_total;
};
are you asking how to print centered text or how to solve a problem?
salem, does C still require a global variable on structs? I have not done serious C since the mid 90s.
Last edited on
closed account (yC4jURfi)
i'm working on it right now, in the solving, i kinda know how to do it except for string because i'm still kinda confused in there. and for the centered text im using "\t", is that fine or is there other way to center it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std;

void writeCentred( const string &s, int linelength, string bound = "" )
{
   int left = ( linelength - s.size() ) / 2, right = linelength - s.size() - left;
   if ( left < 0 ) left = right = 0;             // Disaster; line length not long enough
   cout << bound << string( left, ' ' ) << s << string( right, ' ' ) << bound << '\n';
}

int main()
{
   string bound = "|";
   int linelength = 40;

   writeCentred( "ABC BANK", linelength, bound );
   writeCentred( "XBC Ave., City", linelength, bound );
   writeCentred( "DEPOSITOR'S STATEMENT OF ACCOUNT", linelength, bound );
}


|                ABC BANK                |
|             XBC Ave., City             |
|    DEPOSITOR'S STATEMENT OF ACCOUNT    |


Last edited on
AcctNo. Depositor Name AccountBalance InterestEarned TotalAccountBalance
(int type)(string of 25 characters)(float type) (float type) (float type)


Is Name always exactly 25 chars - or is name up-to 25 chars? If not always exactly 25 chars, can it contain white-space chars?

Perhaps:

1
2
3
4
5
6
7
typedef struct account {
  int account_number;
  char depositor_name[26];  // Include null-terminator
  float balance;
  float interest_earned;
  float account_total;
};


Can you provide a file sample?
Last edited on
Hello justinejerome,

Question: when you say center what size screen will it be centered on?

My screen is about 23 1/2", (58.75 cm), wide, but the console window that programs run in is about 13 1/2", (33.75 cm), wide.

First you have to know how wide the display area is then how many characters will fit on a line, this would be a fixed width font. Then you will have a start at knowing how to center the output.

You could use (strlen()) to determine the length to calculate where to start the string and just pad the left with however many spaces you need.

Andy
Hello justinejerome,

I wroth this little program. Give it a run and see what you get:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <stdlib.h>

int main()
{
	printf("%d", 0);
	
	for (int lco = 1; lco < 11; lco++)
	{
		for (int lci = 1; lci < 10; lci++)
		{
			printf("%d", lci);
		}

		printf("%d", lco);
	}

    return 0;  // <--- Not required, but makes a good break point.
}

On line 8 change the (11) up or down as needed. What you want here is for the outer loop to do enough times for the inner loop to wrap the line.

My output looks like this:

012345678911234567892123456789312345678941234567895123456789612345678971234567898123456789912
345678910

At the end of line 1 the (9) means 90. After that the (2) measn that I can get 92 characters on a line before it wraps.

Andy
Topic archived. No new replies allowed.