0

I tried and looked everywhere on the internet, I can't seem to find much.
Now, without too much talking, to the problem I have: I have to make a project for college that consists of making an "electronic phone-book" using OOP programming logic in C++. The materials I`ve received to do this are extremely vague so I must do on my own somehow.

Here is the code I have done so far(with help from the internet):

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <string.h>
#include <fstream.h>

void op1()
{
    printf("\n Add contact");
    getch();
}
void op2()
{
    printf("\n Delete contact");
    getch();
}
void op3()
{
    printf("\n Edit a contact");
    getch();
}
void op4()
{
    printf("\n Find a contact");
    getch();
}
void op5()
{
    printf("\n Sort the contacts");
    getch();
}

void op6()
{
    printf("\n 'About the program, details etc. etc.");
    getch();
}



void print_menu()
{
  system("cls");        // clearing window
    printf("\nMenu:");
    printf("\n##############################\n");
  printf("\n1. Add contact");
  printf("\n2. Delete contact");
  printf("\n3. Edit a contact");
  printf("\n4. Find a contact");
  printf("\n5. Sort the contacts");
  printf("\n6. About");
  printf("\n7. Exit");
    printf("\n\n##############################");

  printf("\n\nInput your option: ");
}

// Text centering function - begin
void centerText(char* s)
{
     int l=strlen(s);
     int pos=(int)((80-l)/2);
     for(int i=0;i<pos;i++)
         cout<<" ";
     cout<<s<<endl;
}
// Text centering functon - end

void main()
{

    {   

        printf("\n 'My University' 'My City' ");
        printf("\n Faculty of Electrical Engineer and Computer Science");
        printf("\n Study program used: C++\n\n");
        centerText("C++ Project");
        centerText("<The Phonebook>");
        printf("\n");
        centerText("<my name, Year I of study, Group>");
        printf("\n");
        printf("\n");
        centerText("<june.2013>");
        system("pause>nul"); // Screen is paused until a key is pressed (to allow this text to be viewed)

    }
    // ... Sequence for password verification ...
    {
        char password[20], my_password[20]="2013";
        system("cls");
        printf("WARNING!\n");
        printf("Authentication required!\n");
        printf("\nType input password: ");
        scanf("%19s",password);

            if (strcmp(password, my_password)!=0)
            {

                printf("\n\nIncorrect password !!!\n");
                printf("The program will now exit...\n");
                getch();
                return;
            }

        printf("\n\nPassword is correct !\n");
        printf("The program is executed !\n");
        getch();
    }
  char optiune;

  // ... Sequence for option choosing ...

  do
  {
  print_menu();
  fflush(stdin);
  cin>>optiune;
  switch(optiune)
  {
    case '1':  op1(); break;
    case '2':  op2(); break;
    case '3':  op3(); break;
    case '4':  op4(); break;
    case '5':  op5(); break;
    case '6':  op6(); break;
    case '7':  exit(0);
    default :   printf("\n\nIncorrect option !"); 
                fflush(stdin);
                getch();
  }
  }
  while(1);
}

The idea was that I could maybe use this menu in a way like, just inserting in one of the op() functions the redirect to another file, which is 1 function in a separate file. So I would then have this program as a main program and each function that "adds, edits, deletes ..etc will be outside of this program and I would deal with them separately. Thing is ..I have no clue in doing so. I've looked in the "header" working system and I didn't really find anything of value there. Maybe I don't know to look but trust me I've really tried. Any feedback is much appreciated, but remember I am extremely newbie in this. Please, if you can, explain with as much detail as you can. I appreciate anyone who read this entire thing. I will thank the beginning.

KevinDTimm
  • 14,226
  • 3
  • 42
  • 60
Emil Porim
  • 1
  • 1
  • 2
  • 2
    http://www.learncpp.com/cpp-tutorial/18-programs-with-multiple-files/ – BoBTFish Aug 08 '13 at 16:15
  • 1
    start small - really small, then build more and more complexity as you get each piece working. – KevinDTimm Aug 08 '13 at 16:17
  • Is the question about working with multiple files or OOP? You don't have any "class" in your code. – Neil Kirk Aug 08 '13 at 16:19
  • @ BoBTFish thank you very much! I am working on it right now! – Emil Porim Aug 08 '13 at 16:23
  • @Neil Kirk it is about bouth, I have pretty little understanding of the notion "class" but I`m a fast learner. If you can poing me in a direction. :) – Emil Porim Aug 08 '13 at 16:24
  • 1
    I know quality of C++ teaching can vary a lot, but shouldn't your college give you learning material about it? – Neil Kirk Aug 08 '13 at 16:26
  • `char password[20]; /*...*/ scanf("%19s",password);` indicates you're light-years ahead of where I was on my intro to programming final project. My program crashed because I was over-writing the ends of arrays holding strings. (My instructor was a Pascal programmer and never quite got across to us how to deal with basic input/output in C++) Looks like the next thing you might focus on is where/how you want to store these contacts. –  Aug 08 '13 at 16:32
  • @ebyrob That is not how to deal with input in C++!? – Neil Kirk Aug 08 '13 at 16:37
  • @NeilKirk C++ was the sins of omission in what he taught us about `cin` and `cout`. –  Aug 08 '13 at 16:51

2 Answers2

3
  1. You say you can't find help anywhere, but then you say "I have to make a project for college". So presumably you already have instructors and/or professors to help you learn? Other than that, any introductory C++ book ever written will cover what you're asking for, here.

  2. You say "using OOP programming logic in C++", yet you don't use any OOP other than built in IO classes.

  3. Indent your code properly.

  4. Don't use these:

    #include <conio.h>
    
    getch()
    
    system("cls")
    
  5. You're mixing calls to printf() with std::cout, and calls to scanf() with std::cin - pick one or the other, and never use scanf().

  6. If you're using C++, using std::string is better than this:

    void centerText(char* s)
    
  7. The cast here is unnecessary, when you're assigned to an int it'll automatically convert:

    int pos=(int)((80-l)/2);
    
  8. main() returns int, don't do this:

    void main()
    
  9. You can't fflush(stdin), flushing is not defined on input streams.

  10. Don't put things on one line like this, because it looks awful:

    case '1':  op1(); break;
    
  11. It's better to return 0 than exit(0) under normal circumstances, as exit(0) will not destroy your objects.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Crowman
  • 25,242
  • 5
  • 48
  • 56
  • Surprisingly, he actually seemed to use `scanf()` correctly. In fact, I don't think you've caught a single technical failing in anything he's provided. (Everything you mention is stylistic) –  Aug 08 '13 at 16:36
  • @ebyrob: Other than his definition of `main()` and the call to `fflush(stdin)`, you presumably mean. While technically you're correct on the `scanf()` point, I'm of the opinion that there is no correct way to use it, that the very act of employing it is a mistake. – Crowman Aug 08 '13 at 16:38
  • In Visual Studio (Which I assume due to `system("cls");`) `void main()` compiles and runs. As does `fflush(stdin)` (as noop). PS - I use `sscanf()` all the time during code-jam. It's there and it's easy (though brittle and insecure). –  Aug 08 '13 at 16:46
  • @ebyrob: Whether something "compiles and runs" is irrelevant to whether or not it's *correct*. Conforming C++ implementations are allowed to successfully compile non-comforming C++ programs if they want to. If the OP doesn't care about writing correct C++, he shouldn't expect anyone to care about answering his questions. `sscanf()` is an entirely different beast to `scanf()` and the same objections do not apply, although I'd still suggest not using it in C++, as opposed to C. – Crowman Aug 08 '13 at 16:50
  • @Paul Griffiths I have added to note everything you said above. But I must correct you in only 1 matter, I really don`t have where to turn for my field, the teachers in my country are not the same with the ones you have where you live, I`m from a very corrupt country where anyone can teach, anyone can get their degree on paid money not study, they only need to have basic knowledge of their material and repeat it every year. I don`t want to enter that political subject, but trust me I will do anything I can to finish this project because I know I can. – Emil Porim Aug 08 '13 at 16:53
  • @EmilPorim: Maybe you should buy your degree instead of completing this assignment, in that case. Does "do[ing] anything [you] can" extend to reading a C++ book? Because in all seriousness, doing that for a few hours will answer most questions of this nature you may have, and suggesting that is probably the best advice or help anyone could give you, at this stage. – Crowman Aug 08 '13 at 16:58
  • @EmilPorim I'm sorry to hear you come from a corrupt place. Fortunately, if you have access to the internet and a reasonable computer, anyone can learn programming! You will need to buy several books if you are truely on your own though. – Neil Kirk Aug 08 '13 at 17:03
  • @ Paul Griffiths recommend me a good book that I can learn from, with meaningfull content and I will read it and I promise you that, so far I only found books with little examples nothing that can extend to this size that can help me for a project of this scale. It doesn`t bother me to read. – Emil Porim Aug 08 '13 at 17:03
  • @EmilPorim: you can start here for book recommendations -> http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Crowman Aug 08 '13 at 17:05
  • @EmilPorim in particular think about making classes "phonebook" and "contact". Then make sure you look at cin, cout, fstream, and std::string. –  Aug 08 '13 at 17:05
  • Thank you, everyone, for your feedback, I won`t borrow anymore of your time. Your advices really helped me a lot! Thank you again for your support! – Emil Porim Aug 08 '13 at 17:12
0

In many applications there are three main components to building the solution. There is the View, which is how you interact with the user. Your work so far looks as though you are building a fairly sensible command line interface for that. Then there is the back end where the contacts will be stored. This could be a file, a database or some kind of JSON or XML structure. In between is the controller that keeps the view and database in sync and executes commands and queries from the user. Maybe this is going too far for your coursework and you are happy if everything is forgotten when you kill the program. In that case you need a memory-based structure to hold the data. I suggest you make a class to hold all the fields in your contact and store in in a std::vector<Contact>.

David Elliman
  • 1,379
  • 8
  • 15
  • Yes, so far I have done only the "appearance". I am working in Microsoft Visual C++ 2006 and the interface is a command line one. The idea of holding the data in a "text file" is good but I don`t know how to get it from another file, which is the function separately. (As i will be working with different files for functions) or maybe using classes would suffice and i should just keep them all in 1 file along-side with the main program? – Emil Porim Aug 08 '13 at 16:28
  • @EmilPorim Look up how to create header files (*.h). Then when you compile, be sure to include all relevant .cpp files by right-click add to project or including them on the command line to `cl` (or g++). –  Aug 08 '13 at 17:07