473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

A few simple problems in a simple program.


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
7 4097


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


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


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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Brian Hlubocky | last post by:
I'm have a fairly simple (in terms of COM) python program that pulls info from an Access database and creates Outlook contacts from that information. It uses wxPython for gui and works without...
6
by: hoover_richard | last post by:
I am a newbie to C++ and I need help with a simple program I am trying to write. My program is designed to print all of the odd integers contained in an array and output the sum of the odd...
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
10
by: serge calderara | last post by:
Dear all, I need to build a web application which will contains articles (long or short) I was wondering on what is the correct way to retrive those article on web page. In orther words, when...
3
by: Dara Durum | last post by:
Hi ! Py2.4, Win32. I need to optimize a program that have a speciality: hash (MD5/SHA1) the file contents (many files). Now I do this in a simple python program, because (what a pity) the...
23
by: Marco | last post by:
Could anyone please tell me why the program has the following error? I copy the program from http://www.beyondlogic.org/parlcd/parlcd.htm ...
12
by: nickyeng | last post by:
here is the simple program for all the newbie to C++. i'm a newbie. i install cygwin and use g++, i compiled it no problems, but there is no output for the program. once i compiled it...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
4
by: Break2 | last post by:
I am trying to write a simple program which asks a user to enter 5 integers after which the average will be computed and written to the screen. That simple. However I want to do it according to a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.