Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hey guys, this is one of my university exercises which I have a bit struggling with. It'd be great if you help me out with its coding :

Write a program for the management of a book of contacts of maximum 100 contacts.
The program shall allow the storage of following data in a structure:
C++
typedef struct names {
char name[20]; /*Name*/
char surname[20]; /*Surname*/
char number[20]; /*Phone Number*/
char mobile[20]; /*Mobile Number*/
} Names;


The program shall allow the user to perform, by means of a menu, the input of a new contact (allowing eventual duplicates of name and surname, but shall give a warning to the user in case of duplicates and shall ask to confirm the input), and displays the complete list.

===

Update: I can't believe I didn't put my attempts in its following, I am sorry and I am definitely not putting my duties on somebody else shoulder :)

Anyway, this is my codes which I have been faced with many errors and I don't know exactly why!
Thanks for your considering :


C#
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100


typedef struct names {
char name[20]; /*Name*/
char surname[20]; /*Surname*/
char number[20]; /*Phone Number*/
int mobile[20]; /*Mobile Number*/
}Names;
int main()
{
    int i;

    Names n[20];
    for(i=0;i<MAX;i++){
        printf("Enter the name %d: ",i);
        scanf("%s", n[i].name);
        printf("Enter the surname %d: ",i);
        scanf("%s", n[i].surname);
        if((strcmp(n[i].name, n[i++].name)==0) && strcmp(n[i].surname, n[i++].surname==0)){
        printf("Warning, There is another name the same!!!");
        exit(1);
        }
        printf("Enter the mobile %d: ",i);
        scanf("%d", &n[i].mobile);
    }
    for(i=1;i<MAX+1;i++){
        printf("%d. %s %s: %d",n[i].name,n[i].surname,n[i].mobile);
    }

    return 0;
}
Posted
Updated 14-Jan-16 9:36am
v6
Comments
Sergey Alexandrovich Kryukov 14-Jan-16 13:09pm    
Sorry, this is not a question. You can get some help only as an answer to some question, if it is valid and answerable.
Please understand that no one will volunteer to do you assignment for you, but you can get so good advise, references to some algorithms, techniques or libraries, and so on. But you need to ask some questions.

—SA
Sina Msv 14-Jan-16 14:58pm    
I am not looking for others taking responsibility for my duties. It was just a mistake which I immediately solved as I noticed. I'd be grateful for some hands :)
Sergey Alexandrovich Kryukov 14-Jan-16 15:24pm    
All you need is to ask some questions. Then we will gladly try to help you.
—SA
Sina Msv 14-Jan-16 15:35pm    
Thank you :) I just did, I've been faced w/ some errors by debugging these codes. Can you tell me where I've made a mistake ?
Sergey Alexandrovich Kryukov 14-Jan-16 15:48pm    
Do you mean exception (in case of compilation error, you would not be able to debug)? But in what line? How about comprehensive exception information?
—SA

Here is a plan for you how to proceed:

(1) Get your IDE and debugger running. Nobody can work without the proper tools and without a working debugger you simply get nowhere.

(2) Read the task description. For example, I miss the menu. Then you chose another data type for the mobile phone number. And the program should give a warning if a duplicate name is entered, but not abort!

(3) Check your program logic: If you are checking whether a given name is already present, you have to check against ALL names already entered! Take a look at what you are doing with index i! Don't mess it up, while you are checking for duplicates.

(4) Take a look at the variable and structure names you are using: MAX is not a good name for the size of your array. Such names are likely to be used by libraries or other program components. How about MAX_PERSON_ENTRIES - that tells another reader what you mean without even writing a comment. Also, the name of your data structure is not chosen very well: The structure contains data about a single person; why is the name in the plural then? If at all it should be called "Name". Better would be Person, as it not only contains the name of a person, but some other data also. (If the naming of the data structure was chosen by your teacher, I would become suspicious about the quality of the course).

(5) What if the user wants to enter five data sets only? You are forcing him to enter 20. No good idea.

(6) Run your program in the debugger and in single-step mode. Never ever even think about using a piece of code that you have not run in the debugger and through every possible branch. While doing that you will notice what is going wrong in your program and learn to fix most problems before you even do the first real run. (And yes, even many professionals with many years experience stick to this rule! So why not getting used to it from the very beginning.)

Cheers!
 
Share this answer
 
Comments
Sina Msv 14-Jan-16 18:09pm    
Thank you so much for your analysis, information and kindly considering every little bit of everything. I'd consider your suggestions and it'd be probably a life saver as it seems to be :)
Dishi_Gupta 15-Jan-16 5:59am    
Can you just list some of the errors being given so as to proceed with some hints..
nv3 15-Jan-16 6:38am    
Sorry, but the errors are clearly visible to everyone who is beyond the beginner stage.
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Comments
Sina Msv 14-Jan-16 15:00pm    
I am not asking to :) It was just a mistake
I will say this: If I were your instructor, having given you that structure to use, I would definitely test for how you address buffer overflow(s).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900