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 */
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 */