By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,966 Members | 812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,966 IT Pros & Developers. It's quick & easy.

A few simple problems in a simple program.

P: n/a

Greetings fellow programmers,

I have created a C program that has a few bugs and would like to get
some help with working them out. Here is a list of the problems that
I am experiencing:

- The program calls for the input to be added in one line (? 100 Smith
24.98), but my right now my program skips a line each time for
example:
? 100
Smith
24.98

Is there a simple solution to this problem?

- Even though I put the tabs in correctly the table headings don't
line up with the columns.

- I need the balances to be right aligned below the BALANCE heading.

Please respond to the group. Below you will find the parameters for
the program along with my code.

Thanks in advance for your assistance.

=======================================

/*
----------------------------------------------------------------------------------------------

Write a C program that prompts the user to enter some data regarding
some clients to a business.
The user should prompt for customer account number (a positive
integer), a last name (a string),
and their account balance (a floating point number) as shown below.
Notice how the user input
terminates once the user enters a -999 as the account number:

Enter account number, last name, and balance.
Enter -999 to end input.
? 100 Smith 24.98
? 200 Verras 334.33
? 500 Jones 56.55
? 600 Smith 330.90
? 700 Stone 42.16
? 800 White 0.00
? -999

You could store this information in either 3 separate arrays or in an
array of type struct (something). Make the array sizes 10, and be sure
to test that the user does not enter more than 10 clients! Once the
user has completed entering the data, the program should then output
the client data in table form as follows:

ACCOUNT LAST NAME BALANCE
100 Smith 24.98
200 Verras 334.33
500 Jones 56.55
600 Smith 330.90
700 Stone 42.16
800 White 0.00

Hints:

* If you choose to store the information in arrays, the array for the
last name could look like: char last_name[10][30]; and you would
prompt as follows: scanf ("%s", last_name[x]); where x is for each
client and goes from 0 to 9. When outputting this information, you
simply use the %s format specifier again as: printf ("%s",
last_name[x]);

* When prompting for the information, use 3 separate scanf statements
for each client. That is, although the user will enter the information
all on 1 line, your scanf statements can look like (assume you have
chosen to store your data in arrays. However, you will still use 3
separate scanf statements even if you use files)

scanf ("%i", &client_num[x]);
scanf ("%s", last_name[x]);
scanf ("%f", &balance[x]);

That way, after you read the client_num, you can test for the value of
-999 before going on to read the last_name and balance.

* If using arrays, your program needs only 4 variables. It can be
accomplished in 16 lines of code (including 2 separate loops). This
line of code count does not include variable declarations or comments.

-----------------------------------------------------------------------------------------------
*/

#include <stdio.h>
#include <string.h>

#define MAX 10

int main (void)
{

int i;
int N = 10; // Buffer for last name
int accountNumber[MAX];
char lastName[MAX][30];
float accountBalance[MAX];

printf ("Enter account number, last name, and balance.\n");
printf ("Enter -999 to end input.\n");

for (i = 0; i < 10; i++)
{

printf ("? ");
scanf ("%i", &accountNumber[i]);

if (accountNumber[i] == -999)
{
printf("\n");

break;
}

scanf ("%Ns", lastName[i]);
scanf ("%f", &accountBalance[i]);

} /* End for loop */

printf("ACCOUNT \t\t LAST NAME \t\t BALANCE\n");

i = 0;

while ( i < 10 && accountNumber[i] != -999 )
{
printf("%d \t\t %s \t\t %.2f\n", accountNumber[i],
lastName[i], accountBalance[i]);
i++;
}

printf("\n\n");

return 0;

} /* End main */
Nov 13 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a


jm**@berkeley.edu wrote:
Greetings fellow programmers,

I have created a C program that has a few bugs and would like to get
some help with working them out. Here is a list of the problems that
I am experiencing:

- The program calls for the input to be added in one line (? 100 Smith
24.98), but my right now my program skips a line each time for
example:
? 100
Smith
24.98

Is there a simple solution to this problem?
I don't see the problem here. If you enter the data as instructed
on a single line: ie.
100 Smith 24.68
the code seems to work ok.
However there a a number of frailties in the code should the user
input data not as the format specifies.

- Even though I put the tabs in correctly the table headings don't
line up with the columns.
See below.
#include <stdio.h>
#include <string.h>

#define MAX 10

int main (void)
{

int i;
int N = 10; // Buffer for last name
int accountNumber[MAX];
char lastName[MAX][30];
float accountBalance[MAX];

printf ("Enter account number, last name, and balance.\n");
printf ("Enter -999 to end input.\n");

for (i = 0; i < 10; i++)
{

printf ("? ");
scanf ("%i", &accountNumber[i]);

if (accountNumber[i] == -999)
{
printf("\n");

break;
}

scanf ("%Ns", lastName[i]);
scanf("%s",lastName[i]);
or
scanf("%29s",lastName[i]);
scanf ("%f", &accountBalance[i]);

} /* End for loop */

printf("ACCOUNT \t\t LAST NAME \t\t BALANCE\n");
puts("ACCOUNT \t\t LAST NAME \t\t BALANCE");
i = 0;

while ( i < 10 && accountNumber[i] != -999 )
{
printf("%d \t\t %s \t\t %.2f\n", accountNumber[i],
lastName[i], accountBalance[i]);
printf("%.7d \t\t %-30s %.2f\n", accountNumber[i],
lastName[i], accountBalance[i]);
i++;
}

printf("\n\n");

return 0;

} /* End main */

--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #2

P: n/a
Hey Al,

I tried your suggestion for creating the table, but it didn't work
well. Can you offer me any other options?

Thanks
Nov 13 '05 #3

P: n/a


jw****@berkeley.edu wrote:
Hey Al,

I tried your suggestion for creating the table, but it didn't work
well. Can you offer me any other options?

Your use of scanf is:
scanf ("%i", &client_num[x]);
scanf ("%s", last_name[x]);
scanf ("%f", &balance[x]);

Perhaps you should try putting white space in the format string.
scanf("%i ",&client_num[x]);
scanf("%s ", last_name[x]);
scanf("%f ",&balance[x]);

--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #4

P: n/a


jw****@berkeley.edu wrote:
Hey Al,

I played with the tabs and got the table to output properly. I was
just overlooking some small details. I appreciate your help. My code
appears below if you care to take a look.

================================================== ====

#include <stdio.h>
#include <string.h>
There is nothing in the code shown that would require the include of
string.h.

int main (void)
{

/* Variable Declarations */
/* ---------------------- */

int i;
int accountNumber[10];
char lastName[10][30];
float accountBalance[10];

/* Prompts the user for input */
/* -------------------------- */

printf ("Enter account number, last name, and balance.\n");
printf ("Enter -999 to end input.\n");

/* For loop reads in the information. */
/* ---------------------------------- */

for (i = 0; i < 10; i++)
{

printf ("? ");
scanf ("%i", &accountNumber[i]);

if (accountNumber[i] == -999) // tests account number
to see if the stop value was entered
{
printf("\n");

break;
}

scanf("%s", lastName[i]);
scanf ("%f", &accountBalance[i]);

} /* End for loop */

/* Prints out the information that was entered in a table
format */
/*
------------------------------------------------------------- */

printf("ACCOUNT \t LAST NAME \t BALANCE\n");

i = 0;

while ( i < 10 && accountNumber[i] != -999 )
{

printf("%d \t\t %s \t %.2f\n", accountNumber[i],
lastName[i], accountBalance[i]);

You will soon find that it is better to remove the /t characters in
formatting your output.
For example try using a small name like Cox on the next line use a big
name like Washingtonstone. You will then see a problem.
One solution that removes the tabs:

printf("ACCOUNT LAST NAME BALANCE\n");

i = 0;
while ( i < 10 && accountNumber[i] != -999 )
{
printf("%-7d %-30s%8.2f\n", accountNumber[i],
lastName[i], accountBalance[i]);
i++;

i++;
}

printf("\n");

return 0;

} /* End main */


The code seems to satisfy the specifications of the assignment, but
note, in the real world, using scanf the way you do requires a rigid
input format from the user. If the user does not follow the correct
input format there are many ways in which the program will fail.

--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #5

P: n/a
In <bf************@ID-169908.news.uni-berlin.de> Al Bowers <xa*@abowers.combase.com> writes:

jw****@berkeley.edu wrote:
Hey Al,

I tried your suggestion for creating the table, but it didn't work
well. Can you offer me any other options?
Your use of scanf is:
scanf ("%i", &client_num[x]);
scanf ("%s", last_name[x]);
scanf ("%f", &balance[x]);

Perhaps you should try putting white space in the format string.


Nonsense. All the conversions used automatically skip white space.
scanf("%i ",&client_num[x]);
scanf("%s ", last_name[x]);
scanf("%f ",&balance[x]);


The space in the last scanf format is an attrocious idea. If you can't
figure out why, try your suggestion yourself and see what happens!

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6

P: n/a


Dan Pop wrote:
In <bf************@ID-169908.news.uni-berlin.de> Al Bowers <xa*@abowers.combase.com> writes:
jw****@berkeley.edu wrote:
Hey Al,

I tried your suggestion for creating the table, but it didn't work
well. Can you offer me any other options?


Your use of scanf is:
scanf ("%i", &client_num[x]);
scanf ("%s", last_name[x]);
scanf ("%f", &balance[x]);

Perhaps you should try putting white space in the format string.

Nonsense. All the conversions used automatically skip white space.

scanf("%i ",&client_num[x]);
scanf("%s ", last_name[x]);
scanf("%f ",&balance[x]);

The space in the last scanf format is an attrocious idea. If you can't
figure out why, try your suggestion yourself and see what happens!

Dan


Yeah, the suggestions were bad. My apology for posting such hooey.

--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #7

P: n/a
Al Bowers <xa*@abowers.combase.com> wrote:
scanf ("%Ns", lastName[i]);


scanf("%s",lastName[i]);


This one is as bad as gets(), which I'm sure you're aware of...
Stig
--
brautaset.org
Nov 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.