Solved

Car registration program

Posted on 2001-08-18
23
352 Views
Last Modified: 2010-04-15
Hello all

I'm a student new to learning C so please excuse some of my lacking of the correct terms.

I have to write a program that gets the user to enter a registration and it then calculates the hash and checks the file to see if it exists etc.

Now I have added the program to this email so you can get the gist of what i'm trying to do. There is one problem with my do loop though and cant work out what it is. can anyone advise what i've done wrong?.

I know some errors may be there (it compiles OK but logic errors may be present).

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>

void enter_details(struct pnc *temp);
void enter_reg(char *temp_reg);
void check_reg();
int create_hash(char *temp_reg);



struct pnc {
     char V_RegNo[8];
     int  V_RegDate;
     char V_Maker[11];
     char V_Colour[9];
     char V_Owner[26];
     char V_Address[31];
     int  V_Expiry;
                 }details;

int main()
{
char choice;
struct pnc CurrDetails;
int hash;
FILE *fp_ptr;

 if ( (fp_ptr = fopen("cardata.dat", "rb+")) == NULL)
     {
     printf("Error opening file\n");
     return 1;
     }

 choice = 1;

do
{
printf("If you wish to make an amendment to an existing file type 'A'\n");
 printf("If you wish to delete a file type 'D'\n");
 printf("If you wish add a file type 'I'\n");
 printf("Type 'X' to exit the program  ");
choice = toupper((char)getchar());
}
while (choice != 'X');

switch (choice)
     {
          case 'A': enter_reg(CurrDetails.V_RegNo);
                        enter_details(&CurrDetails);
                hash = create_hash(CurrDetails.V_RegNo);
                      fseek(fp_ptr, SEEK_SET, hash*sizeof(struct pnc));
        case 'D': /*find the vehicle and change reg date to 9999*/
          case 'I': enter_reg(CurrDetails.V_RegNo);
                          enter_details(&CurrDetails);
          case 'X': break;
          default : printf("You used an invalid letter try a letter from the menu");
     }
}

void enter_reg(char *temp_reg)
{
while(1)
     {
     printf("Please enter registration of vehicle in x999xxx format ");
     fgets(temp_reg,8,stdin);
     isalpha(temp_reg[0])&& isdigit(temp_reg[1]) &&isdigit(temp_reg[2])
     && isdigit(temp_reg[3]) && isalpha (temp_reg[4]) && isalpha(temp_reg[5])
     && isalpha(temp_reg[6]);

   return;
     }
}

void enter_details(struct pnc *temp)
{

char str_one[5];
char str_two[5];


printf("\nEnter registration date\n ");
 fgets (str_one,5,stdin);
   temp->V_RegDate = atoi(str_one);
printf("\nEnter manufacturer\n ");
  gets (temp->V_Maker);
printf("\nEnter colour\n ");
  gets (temp->V_Colour);
printf("\nEnter owners name\n ");
  gets (temp->V_Owner);
printf("\nEnter owners address\n ");
  gets (temp->V_Address);
printf("\nEnter expiry date\n ");
 fgets (str_two,5,stdin);
    temp->V_Expiry = atoi(str_two);

return;
}
int create_hash(char *temp_reg)
{
return 1000*(temp_reg[0]- 'A') + 100*(int)temp_reg[1] +
10*temp_reg[2] + temp_reg[3];
}

Thanks in advance for your help

Lee
0
Comment
Question by:helee
  • 8
  • 5
  • 4
  • +3
23 Comments
 
LVL 11

Expert Comment

by:griessh
ID: 6400534
Lee

What do you think is wrong with your do loop? I would just suggest to add a "break" to each "case" or you will execute all the commands following the one you picked ...
Is that your problem?

BTW: a good debug trick is to add a printf into each 'case block'.

=====
Wener
0
 

Author Comment

by:helee
ID: 6400759
griessh

I put the break; in each case and I also put a printf statement in each case but I still have problems with the loop.

After I input say an 'A' it prints out my loop twice as such. Also my printf's are not printing out so something is wrong but I can't find out what.

Here is the updated code with comments I have added to show what I have added to this code from the original.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>

void enter_details(struct pnc *temp);
void enter_reg(char *temp_reg);
void check_reg();
int create_hash(char *temp_reg);



struct pnc {
     char V_RegNo[8];
     int  V_RegDate;
     char V_Maker[11];
     char V_Colour[9];
     char V_Owner[26];
     char V_Address[31];
     int  V_Expiry;
                 }details;

int main()
{
char choice;
struct pnc CurrDetails;
int hash;
FILE *fp_ptr;

 if ( (fp_ptr = fopen("cardata.dat", "rb+")) == NULL)
     {
     printf("Error opening file\n");
     return 1;
     }

 choice = 1;

do
{
printf("If you wish to make an amendment to an existing file type 'A'\n");
 printf("If you wish to delete a file type 'D'\n");
 printf("If you wish add a file type 'I'\n");
 printf("Type 'X' to exit the program  ");
choice = toupper((char)getchar());
}
while (choice != 'X');

switch (choice)
     {
          case 'A': enter_reg(CurrDetails.V_RegNo);
                        enter_details(&CurrDetails);
                hash = create_hash(CurrDetails.V_RegNo);
                      fseek(fp_ptr, SEEK_SET, hash*sizeof(struct pnc));
                printf("You chose A ");  /*added these 2 lines*/
                          break;
      case 'D': printf("You chose D ");
                          break;      /*added the breaks*/
          case 'I': enter_reg(CurrDetails.V_RegNo);
                          enter_details(&CurrDetails);
                          printf("You chose I ");
                          break;
          case 'X': break;
          default : printf("You used an invalid letter try a letter from the menu");
     }
}

void enter_reg(char *temp_reg)
{
while(1)
     {
     printf("Please enter registration of vehicle in x999xxx format ");
     fgets(temp_reg,8,stdin);
     isalpha(temp_reg[0])&& isdigit(temp_reg[1]) &&isdigit(temp_reg[2])
     && isdigit(temp_reg[3]) && isalpha (temp_reg[4]) && isalpha(temp_reg[5])
     && isalpha(temp_reg[6]);

   return;
     }
}

void enter_details(struct pnc *temp)
{

char str_one[5];
char str_two[5];


printf("\nEnter registration date\n ");
 fgets (str_one,5,stdin);
   temp->V_RegDate = atoi(str_one);
printf("\nEnter manufacturer\n ");
  gets (temp->V_Maker);
printf("\nEnter colour\n ");
  gets (temp->V_Colour);
printf("\nEnter owners name\n ");
  gets (temp->V_Owner);
printf("\nEnter owners address\n ");
  gets (temp->V_Address);
printf("\nEnter expiry date\n ");
 fgets (str_two,5,stdin);
    temp->V_Expiry = atoi(str_two);

return;
}
int create_hash(char *temp_reg)
{
return 1000*(temp_reg[0]- 'A') + 100*(int)temp_reg[1] +
10*temp_reg[2] + temp_reg[3];
}

Thanks again

Lee
0
 
LVL 11

Expert Comment

by:griessh
ID: 6401959
Is that a homework?

======
Werner
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6402182
while (choice != 'X');

//needs to be:

while (choice != 'X')
{
   //Do some stuff here.
   // print the menu
   // do the switch()
}
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6402371
A couple of other tips.
#1.
Some functions you call like atoi() require NULL terminated strings.  If your user inputs a string longer than you expect, your NULL may not land where you expect it.

#2.
The isalpha() and isdigit() functions should be used inside of an if() statement

if(     isalpha(temp_reg[0]) && isdigit(temp_reg[1]) &&
               isdigit(temp_reg[2]) && isdigit(temp_reg[3]) &&
               isalpha(temp_reg[4]) && isalpha(temp_reg[5]) &&
               isalpha(temp_reg[6]))
           {
           //Do Something here
           }

#3.
At this stage in your learning process, it wouldn't hurt to format your code in a strict fashion.  Also, add braces to ensure clarity.
Example:
          switch (choice)
               {
               case 'A':
                    {
                    enter_reg(CurrDetails.V_RegNo);
                    enter_details(&CurrDetails);
                    hash = create_hash(CurrDetails.V_RegNo);
                    fseek(fp_ptr, SEEK_SET, hash*sizeof(struct pnc));
                    printf("You chose A ");
                    break;
                    }
//... code continues ...


#4
The default also needs a break;
0
 

Author Comment

by:helee
ID: 6403150
Griessh

Yes it is coursework but I am studying a hamestudy course and I want to get this part of the program completed over the weekend. No tutors work over the weekend and to be honest the support network with the course isn't too great. I'm not asking for someone to do my work for me just point me in the direction of where I am going wrong. At the end of the day I want to learn myself not get someone else to do my problems and me not leanr anything.

Triskelion

I have changed the loop to a while loop and I am still getting the same problems I run the program and then the menu prints out. I choose 'A' and then my printf("you chose A"); doesnt print but 2 menus print out.

Why is this happening?

Lee
PS i have taken your other comments in and i'm changing the code now
0
 
LVL 6

Expert Comment

by:kotan
ID: 6403244
The switch() should be inside the do.. while loop.

     do
     {
            // print the menu
             choice = toupper((char)getchar());

            switch (choice)
            {
                ...
                ...
            }
     } while (choice != 'X');
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6403428
...or you can remove the do loop and simply use the while loop.

     while (choice != 'X')
          {
          printf(     "If you wish to make an amendment to an existing file type 'A'\n"
                    "If you wish to delete a file type 'D'\n"
                    "If you wish add a file type 'I'\n"
                    "Type 'X' to exit the program  ");

          choice = (char)toupper((char)getchar());

          switch (choice)
               {
               case 'A':
                    {
                    enter_reg(CurrDetails.V_RegNo);
                    enter_details(&CurrDetails);
                    hash = create_hash(CurrDetails.V_RegNo);
                    fseek(fp_ptr, SEEK_SET, hash*sizeof(struct pnc));
                    printf("You chose A ");
                    break;
                    }

               case 'D':
                    {
                    printf("You chose D ");
                    break;      /*added the breaks*/
                    }

               case 'I':
                    {
                    enter_reg(CurrDetails.V_RegNo);
                    enter_details(&CurrDetails);
                    printf("You chose I ");
                    break;
                    }

               case 'X':
                    {
                    break;
                    }

               default :
                    {
                    printf("You used an invalid letter try a letter from the menu");
                    break;
                    }
               }
          }
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6403430
Oh... by the way.
I have not tested the file read/write functions or create_hash you used, so the comment above is just rearrangement of your existing code.
0
 

Author Comment

by:helee
ID: 6403540
Ok I see what I was doing wrong now DOH!!!

It was me not putting the switch statement in the loop.

Now kotan and triskelion were both right answering my query.

I personally will use the while loop rather than the do loop but I can see that both would work.

I would like to split the points between you both how do I do that??

Lee(helee@totalise.co.uk)
0
 
LVL 6

Expert Comment

by:kotan
ID: 6403618
Wait...
Got a problem in the method provided by Triskelion.
In the case 'X', the "break" is only let you get out from the switch() but not the while loop.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 6

Expert Comment

by:kotan
ID: 6403629
Aiya! Sorry! Triskelion. Your method is worked. It will check in the while condition.
Sorry! please ignore it.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6411485
helee

>>I'm not asking for someone to do my work for me just point me in the direction

That's why I didn't go so far to give you any code. I think Kotan and Triskelion are pretty much in danger here, because they did not behave according to the membership agreement that we accpted. We are not allowed to do the work, but we can point you in the right direction. I tried to give you hints how to find your problems. The others tried to fix your code :-)

Sorry not to be of more help here ...

Kotan and Triskelion

I would suggest you read the member agreement again, ther is a link at the bottom of this page.

======
Werner
0
 
LVL 6

Accepted Solution

by:
Triskelion earned 25 total points
ID: 6412172
Um...
We didn't _do_ the homework.

There was already posted code and a direct question.

There are only so many leading questions and suggestive questions one can ask before posting some kind of example.

Were YOU _never_ going to post an example?

One still must know how to implement our suggestions.

0
 

Author Comment

by:helee
ID: 6412645
Griessh

I do understand the rules but IMO the other two members gave me the help I needed. It's ok saying I pointed you in the right direction but it wasn't clear enough.

I'm still on a steep learning curve as you can probably seeand a comment like 'Is this homework' doesn't help really does it.

It was help i needed and at the end of the day no-one coded for me just showed me examples of where I was going wrong. I believe they have taught me far more by showing me a simple mistake I made.

Thank-you to the two of you

Lee
0
 
LVL 3

Expert Comment

by:darinw
ID: 6412952
Hello everyone,

I've reduced the points to 25 from 50 for the split.

helee, you can now accept one of kotan's or Triskelion's comments as an answer. Then please post a new 25 point question for the other Expert in this topic area to complete the split.

A good title for the new point award question is 'For ExpertName -- ta=cprog&qid=20170487' as that will identify who the question is for and at a glance why it was posted.

darinw
Community Support
0
 
LVL 3

Expert Comment

by:darinw
ID: 6412994
On the situation with homework and our site...

The member agreement is located here:

http://www.experts-exchange.com/jsp/infoMemberAgreement.jsp

...and the applicable part is:

>* violating the guidelines for academic honesty or other unethical behavior

We do not want to do homework for students nor do we want to assist them with any sort of cheating on classwork, take home tests, or open book tests. Which is just another way of saying we'd like to maintain academic honesty.

And, I do appreciate your concern griessh. We need and want our Experts to be very aware of this policy. Thank you for bringing it to light here.

However, I think this question is almost a perfect example of a question between a student and you the Experts that maintains the policy of the site and the spirit of the member agreement.

darinw
Community Support
0
 
LVL 11

Expert Comment

by:griessh
ID: 6413446
darinw

Thanks for helping out here ...

Everybody:

>>but IMO the other two members gave me the help I needed. It's ok saying I pointed you in the right direction but it wasn't clear enough.

I am in the fortunate situation of having a college student myself. If he gives me some piece of code and asks me "What is wrong?" we start to dig through the code and try to UNDERSTAND what is wrong. It would be certainly easier and less time consuming for me to just fix his code. But that wouldn't help him. If you made an error in your program, the only way to FIX and AVOID that error is to UNDERSTAND what you did wrong. You gave some good feedback in your comment from 08/18/2001 09:56AM, but then we saw already the first code changes posted, there was no chance for me to give any further help. Yes, they gave you the help you needed right away. Will that experience help you with your next problem? I have my doubts!
When I see questions like this asked in EE, there is usually a tight schedule behind it (I didn't start programming for a while. I have some code now, but that doesn't work. Please fix it for me, because I need it tomorrow morning). Lee, it is not my intention to accuse you of cheating here, this is just a common observation. And that is where I think we have to hold back a bit with our knowledge.
I am not doing this support here for the points you offer. This place has helped me to accomplish my work and I try to give back. But depending on who is asking for what, I might not blow out the full solution right away ...

======
Werner
0
 

Author Comment

by:helee
ID: 6413506
Thanks Triskelion

It was a great help and as Darin says it was allowed :O)

Lee
0
 

Author Comment

by:helee
ID: 6413931
Triskelion

If you want to see the updated and finished code feel free to email me helee@totalise.co.uk.

Thanks again

Lee
0
 
LVL 3

Expert Comment

by:modder
ID: 6416595
Hello everyone,

I'm in perfect agreement with what darinw has posted in that question.

If someone asks you to do their homework for them you should not assist them. That is a clear case of academic dishonesty, and it will not be tolerated on EE. People who ask such questions, *and* the experts that know better and help them anyway, risk the closing of their account.

But, if someone makes a valiant attempt at doing their own homework and they get stuck on some detail or other, and finally post a question here to seek help, I believe that you can, and should, provide assistance.

As far as I can tell, that is exactly what happened here, and I'm also confident that helee will have learnt a lot from posting this question, so in my opinion nothing untoward has happened in this question.

But Werner makes a good point. Don't just provide the answer for such questions - at the very least, provide an explanation, but, even better, try and tease the answer out of asker themselves by pointing them in the right direction with some strategically placed suggestions..... but you do need to have some teaching experience to be able to do that well, so I think that all in all everyone did their best to handle this question as best they could.

Regards

modder
Community Support Admin
0
 

Author Comment

by:helee
ID: 6419135
Modder

I'm glad you see it that way the same as darinw.

I did learn a lot and at the end of the day i'm not going to pay #2000 to do an extensive programming course and get someone else to do it for me.

I am 28 years old work full time as a printer and want to change career what is the point of getting someone to do my work for me what will i learn?

Thanks again everybody for the advice it has helped :O)

Lee
0
 

Author Comment

by:helee
ID: 6419228
Werner

Sorry I didn't see your post dated 23/8/01. I understand your queries with me and I didn't think you was calling me a cheat. IMO it was a silly mistake I made after I see what was wrong I said to myself doh!! it was simple. I do have trouble debugging my code at times, I have been programming for about a year but this is in my spare time I work as a printer. There was no time limit for this question as I work hard to beat my deadlines and this work isn't needed in for 2 weeks. All I have to do is write a description of the program and it's limitations.

I do understand that you tried to point me in the right direction and I understand why you did this but on this occasion I needed someone to say look here is where it's wrong. If I didn't understand what was wrong I would have asked. I am on a steep learning curver and I do want to learn not just get someone to do my work for me. I will fail my exams at the end of the day and I am eager to get a job as a junior programmer as this will aid my learning curve I believe. To do this I will have to take a wage cut so I am commited in making it work as I have a wife and daughter to support so to be honest I can't afford to fail.

Thanks again

Lee
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now