473,378 Members | 1,404 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,378 software developers and data experts.

Help on C problems...

Hi there

i have a problem in C which i cannot complete...can you guys help?


Write a program that asks the user the ff: data

full name
address
age
birthday

and store each record in an array, prints the contents of the array in the order of occurence. The program must have at least two functions

input_data()
print_data()

dont use global variables

Sample output of the problem:


Enter Record # 1
Full name: John Doe
Address: Alabama
Age: 20
Birthday: June 4

Do you want to enter another record? (Y/N)

if the user will press Y it will go back into inputing the data

Enter Record # 2
Full name: White Smith
Address: Vancouver
Age: 30
Birthday: December 15

but instead of Record # 1 it will change to Record # 2 and so on

if the user will Press N then it will show the inputted data from the user:

Enter Record # 1
Full name: John Doe
Address: Alabama
Age: 20
Birthday: June 4

Enter Record # 2
Full name: White Smith
Address: Vancouver
Age: 30
Birthday: December 15

one last problem is to put this program in a file


Help is very much appreaciated!
Jun 22 '06 #1
6 2843
pls...i really need this
Jun 22 '06 #2
heres my code...but theres a lot of errors that i dont uderstand


Expand|Select|Wrap|Line Numbers
  1.  #include <stdio.h> 
  2. #include <conio.h>
  3.  
  4. typedef struct {
  5.  
  6. char lname [50];
  7. char fname [50];
  8. char mi;
  9. char address[50];
  10. char birthday[50];
  11. char course[50];
  12. int age;
  13. int year;
  14. }personal;
  15.  
  16. typedef personal per_rec[50];
  17. personal input_data();
  18. void print_data(per_rec[], int ctr);
  19.  
  20. void main(void)
  21. {
  22. personal B; per_rec x[];
  23. int ctr=0;
  24. char choice;
  25. clrscr();
  26.  
  27. do
  28.  
  29. {
  30.  
  31. x[ctr]=input_data();
  32. printf("Enter record? Y/N");
  33. scanf("%c", &choice);
  34. ctr++;
  35.  
  36. }
  37.  
  38. while(choice != "N")
  39.     print_data(B[], ctr);
  40. }getch();
  41.  
  42. }
  43.  
  44. personal input_data();
  45. {
  46.  
  47. printf("last name"); gets(B.lname);
  48. printf("first name"); gets(B.fname);
  49. printf("middle initial")
  50. scanf("%c", &B.mi);
  51. printf("address"); gets(B.address);
  52. printf("age");
  53. scanf("%d", &B.age);
  54. printf("birthday"); gets(B.birthday);
  55. printf("course"); gets(B.course);
  56. printf("year");
  57. scanf("%d", &B.year);
  58. return(B);
  59.  
  60. }
  61.  
  62. void print_data(per_rec x[], int ctr)
  63. {
  64.  
  65. int b = 0;
  66.  
  67. while (b <= ctr)
  68.  
  69. {
  70.  
  71. printf(" ", B[b]);
  72.  
  73. b++;
  74.  
  75. }
  76.  
  77. }
  78.  
Jun 22 '06 #3
Banfa
9,065 Expert Mod 8TB
My comments embedded in bold

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. typedef struct {
  5.     char lname [50];
  6.     char fname [50];
  7.     char mi;
  8.     char address[50];
  9.     char birthday[50];
  10.     char course[50];
  11.     int age;
  12.     int year;
  13. }personal;
  14.  
  15. typedef personal per_rec[50];
  16. /* Personnaly I find it a little confusing typedef arrays of 
  17.    something, it also leaves your program set to a fix number 
  18.    of records. */
  19.  
  20. personal input_data();
  21. void print_data(per_rec[], int ctr);
  22. /* per_rec is already an array, I do not think you want an 
  23.    array of per_rec but an pointer to a variable of type per_rec 
  24.    or per_rec * for the first parameter. Of course per_rec[] actually 
  25.    declares a pointer so this is about how the programmer is 
  26.    interpreting what he sees*/
  27.  
  28. void main(void)
  29. /* main should be declared:
  30.        int main( int argv, char *argp[] )
  31.    or something with similarly typed arguments.  It must return int. */
  32. {
  33.     personal B; per_rec x[];
  34. /* Put separate declarations on separate lines, per_rec x[]; is of
  35.    no use .  Even if your compiler will declare zero length arrays you 
  36.    never allocate any memory to this array.  Anyway per_rec is already 
  37.    an array type, did you want an array of arrays or did you want
  38.        per_rec x;
  39.    which is an array of personel.*/
  40.     int ctr=0;
  41.     char choice;
  42.     clrscr();
  43. /* My system does not support this function, it is a 
  44.    non-standard library function. */
  45.  
  46.     do
  47.  
  48.     {
  49.  
  50. /* The order of this code assumes that the user will always 
  51.    enter at least 1 record */
  52.         x[ctr]=input_data();
  53.         printf("Enter record? Y/N");
  54. /* "Enter another record?" would be a more appropriate question. */
  55.         scanf("%c", &choice);
  56.         ctr++;
  57.  
  58.     }
  59.  
  60.     while(choice != "N")
  61. /* Choice is a char "N" is an array of char at 'N' and a '\0', use 'N' if you want the character constant N.  Also you are missing a ; at the end of the while statement.  Finally what if the user enters 'n' or what if they enter 'j' or 'H'? */
  62.     print_data(B[], ctr);
  63. /* B is not an array declared personal B; and it is never 
  64.    initialised, I suspect you mean per_rec*/
  65. }getch();
  66. /* The } at the beginning of this line is an error. */
  67. }
  68.  
  69. personal input_data();
  70. {
  71. /* You use but never declare the variable B in this function. */
  72.  
  73. /* Gets is an unsafe function as it has no limit on the amount 
  74.    of data it writed to the supplied buffer and can easily cause 
  75.    buffer overflow which can crash the program.  I suggest you use
  76.        fgets( <PointerToBuffer>, <BufferLength>, stdin );
  77.    instead which protects against buffer overflow. */
  78.  
  79. /* Your printf statements leave no space after the text so the input is squashed together with the prompt line.  Also printf with no newline character is not garuntted to output the data without an fflush(stdin). */
  80.  
  81. /* Only 1 line of code per line of file. */
  82.  
  83.     printf("last name"); gets(B.lname);
  84.  
  85. /* Putting all that together this should be 
  86.  
  87.        printf("last name : "); 
  88.        fflush(stdin);
  89.        fgets( B.lname, sizeof B.lname, stdin );
  90. */
  91.  
  92. printf("first name"); gets(B.fname);
  93. /* Smae here */
  94. printf("middle initial")
  95. scanf("%c", &B.mi);
  96. /* scanf input is fraught with trouble, you can use fgets here. */
  97.     printf("address"); gets(B.address);
  98.     printf("age");
  99.     scanf("%d", &B.age);
  100. /* scanf input is fraught with trouble, you could use fgets on a 
  101.    temporary buffer and use strtol to convert it to an integer.*/
  102.     printf("birthday"); gets(B.birthday);
  103.     printf("course"); gets(B.course);
  104.     printf("year");
  105.     scanf("%d", &B.year);
  106.     return(B);
  107. /* Returning a structure of ~260 bytes on the stack is not a 
  108.    good idea.  Perhaps you could pass a pointer to a structure to 
  109.    be filled in to this function rather than returning a structure. */
  110. }
  111.  
  112. void print_data(per_rec x[], int ctr)
  113. /* See comment about per_rec x[] at start of file. This function 
  114.    actually makes no attempt to print anything except spaces. */
  115. {
  116.  
  117.     int b = 0;
  118.  
  119.     while (b <= ctr)
  120. /* ctrl is the number of filled in records, the indexes in the array 
  121.    have the range 0 - <numberOfRecords>-1.  The condition 
  122.    b <= ctrl will mean that you access 1 record passed the filled in 
  123.    data.  You you have filled in all 50 records this will be an out of 
  124.    bounds memory access. 
  125.  
  126.    you could use a for loop here
  127.  
  128.     for( b=0; b<ctr; b++)
  129.  
  130.     that is how you have implemented your while loop. */
  131.     {
  132.  
  133.         printf(" ", B[b]);
  134. /* The is no variable B declared in this function, I suspect you 
  135.    want to be referencing x */
  136.         b++;
  137.  
  138.     }
  139.  
  140. }
Jun 22 '06 #4
i have figured it out

but my only problem is Record #, it wont increase.

ex:

Record #1 <-------- this is the problem how do i increament this?..what is the code?
Name: Ed
Age: 30
...
...

Record #2
Name: ....
Age: .....

i cant seem to make it work

pls help

here is my program btw:

Expand|Select|Wrap|Line Numbers
  1.  #include <stdio.h> 
  2. #include <conio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. typedef struct
  7. {
  8. char sur[20];
  9. char first[20];
  10. char mid[20];
  11. }name;
  12.  
  13. typedef struct
  14. {
  15. name fname;
  16. char address[40];
  17. char age[10];
  18. char bday[20];
  19. char course[20];
  20. char ylevel[10];
  21. }data;
  22.  
  23. void input_data (data a[], int);
  24. void print_data (data a[], int);
  25.  
  26. void main (void)
  27. {
  28. data record[100];
  29. int y=0,cnt=0, x=0;
  30. char ans ='a';
  31.  
  32. while(x<100)
  33. {
  34. if(toupper(ans)!='N')
  35. {
  36. input_data(record,x);
  37. flushall();
  38. clrscr();
  39. printf("There are %d vacant entries \n",100-(cnt-1));
  40. printf("Fill another Record?\t");
  41. ans = getchar();
  42. cnt++;
  43. }
  44. x++;
  45. }
  46.  
  47. while (y<cnt)
  48. {
  49. print_data (record,y);
  50. y+=1;
  51. getch();
  52. }}
  53.  
  54. void input_data (data rec[], int x)
  55. {
  56. clrscr();
  57. printf("\nFullname: \n");
  58. printf("Surname:\t");
  59. flushall();
  60. gets(rec[x].fname.sur);
  61. printf("First Name:\t");
  62. gets(rec[x].fname.first);
  63. printf("Middle Name:\t");
  64. gets(rec[x].fname.mid);
  65. printf("Address:\n");
  66. gets(rec[x].address);
  67. printf("Age:\n");
  68. gets(rec[x].age);
  69. printf("Birthday:\n");
  70. flushall();
  71. gets(rec[x].bday);
  72. printf("Course:\n");
  73. gets(rec[x].course);
  74. printf("Year Level:\t");
  75. gets(rec[x].ylevel);
  76.  
  77. }
  78.  
  79.  
  80. void print_data(data rec[], int x)
  81. {
  82. printf("Fullname: %s %s %s \n",rec[x].fname.sur, rec[x].fname.first,
  83.         rec[x].fname.mid);
  84. printf("Address: %s\n", rec[x].address);
  85. printf("Age: %s\n", rec[x].age);
  86. printf("Birthday: %s\n", rec[x].bday);
  87. printf("Course: %s\n", rec[x].course);
  88. printf("Year level: %s\n", rec[x].ylevel);
  89.  
  90. }
  91.  
Jun 24 '06 #5
D_C
293 100+
In print_data(), something like
Expand|Select|Wrap|Line Numbers
  1. printf("Record #%s \n", x);
should be the first line.
Jun 24 '06 #6
thanks a lot i forgot about that one,

change %s to %d since its integers...

thanks again!
Jun 24 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed...
0
by: Creativy, writing and more | last post by:
Hi all, I'm hoping to find some additional help with a site I'm maintaining for my landlord. I'm trying to grow a web hosting and web design business. I was to have this site as a job that would...
1
by: malcolm | last post by:
Hello, We have a small team building a project that involves some 30 or so c# assembly dlls. It is a client server application with 1 exe as the starting point. The dlls and exe are sharing an...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
2
by: Vladislav Moltchanov | last post by:
Recently I have changed from Acc97 to Acc2000 (I had to supply some data entry tools for field data collection for users still using Acc97). Among lot of other complications coming with Acc2000...
3
by: inkexit | last post by:
I need help figuring out what is wrong with my code. I posted here a few weeks ago with some code about creating self similar melodies in music. The coding style I'm being taught is apparently a...
4
by: Robin Haswell | last post by:
Okay I'm getting really frustrated with Python's Unicode handling, I'm trying everything I can think of an I can't escape Unicode(En|De)codeError no matter what I try. Could someone explain to...
4
by: beg_c++ | last post by:
hi i am a beginner in c++, i have a project to be done by few weeks. This project is a maths help for kids!! it is supposed to ask the kid how many problems the kid want and then show the problems...
0
by: Billie Boy | last post by:
Hi to all. I’m new here and am coming to you from Melbourne Australia. So a big HELLO 2 ALL. Now I am encountering an annoying problem in the SQL builder of the copy of VB.6 that I am using at...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.