A few simple problems in a simple program.

J

jmac

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);

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

break;
}

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

} /* End for loop */

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

i = 0;

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

printf("\n\n");

return 0;

} /* End main */
 
A

Al Bowers

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);

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

break;
}

scanf ("%Ns", lastName);
scanf("%s",lastName);
or
scanf("%29s",lastName);

scanf ("%f", &accountBalance);

} /* 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 != -999 )
{
printf("%d \t\t %s \t\t %.2f\n", accountNumber,
lastName, accountBalance);


printf("%.7d \t\t %-30s %.2f\n", accountNumber,
lastName, accountBalance);
 
J

jwatts

Hey Al,

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

Thanks
 
A

Al Bowers

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]);
 
D

Dan Pop

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
 
A

Al Bowers

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);

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

break;
}

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

} /* 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 != -999 )
{

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


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 != -999 )
{
printf("%-7d %-30s%8.2f\n", accountNumber,
lastName, accountBalance);
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.
 
A

Al Bowers

Dan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top