Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > A few simple problems in a simple program.

Reply
Thread Tools

A few simple problems in a simple program.

 
 
jmac@berkeley.edu
Guest
Posts: n/a
 
      07-22-2003

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.9, 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 */
 
Reply With Quote
 
 
 
 
Al Bowers
Guest
Posts: n/a
 
      07-22-2003


http://www.velocityreviews.com/forums/(E-Mail Removed) 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.9, 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: (E-Mail Removed) (remove the x)
http://www.geocities.com/abowers822/

 
Reply With Quote
 
 
 
 
jwatts@berkeley.edu
Guest
Posts: n/a
 
      07-22-2003
Hey Al,

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

Thanks
 
Reply With Quote
 
Al Bowers
Guest
Posts: n/a
 
      07-22-2003


(E-Mail Removed) 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: (E-Mail Removed) (remove the x)
http://www.geocities.com/abowers822/

 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      07-22-2003
In <bfjgph$fbnpo$(E-Mail Removed)-berlin.de> Al Bowers <(E-Mail Removed)> writes:


>(E-Mail Removed) 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: (E-Mail Removed)
 
Reply With Quote
 
Al Bowers
Guest
Posts: n/a
 
      07-22-2003


(E-Mail Removed) 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: (E-Mail Removed) (remove the x)
http://www.geocities.com/abowers822/

 
Reply With Quote
 
Al Bowers
Guest
Posts: n/a
 
      07-22-2003


Dan Pop wrote:

> In <bfjgph$fbnpo$(E-Mail Removed)-berlin.de> Al Bowers <(E-Mail Removed)> writes:
>
>
>
>>(E-Mail Removed) 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: (E-Mail Removed) (remove the x)
http://www.geocities.com/abowers822/

 
Reply With Quote
 
Stig Brautaset
Guest
Posts: n/a
 
      07-23-2003
Al Bowers <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A simple way to print few line stuck to the same position TheSaint Python 9 06-07-2011 02:08 PM
Internet connects for a few seconds then disconnects for a few sec Cody Wireless Networking 2 06-28-2009 08:24 PM
Very simple speed benchmark of few string substitions ways Rafal Zawadzki Python 1 07-16-2006 06:59 PM
To delete few lines and add few lines at the end of a text file using c program Murali C++ 2 03-09-2006 04:45 PM
Can someone write me a simple Perl script for a few bucks? mikeand1@comcast.net Perl Misc 18 02-06-2006 11:17 PM



Advertisments