Menu Problems

This is a discussion on Menu Problems within the C++ Programming forums, part of the General Programming Boards category; I have a project here (console address book program) Code: #include<iostream> #include<fstream> #include<windows.h> #include<cstdio> #include<string> using namespace std; void clrscr(void) ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    37

    Menu Problems

    I have a project here (console address book program)
    Code:
    #include<iostream>
    #include<fstream>
    #include<windows.h>
    #include<cstdio>
    #include<string>
    using namespace std;
    void clrscr(void) //Code by Sunlight, http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1031963460&id=1043284385
    {
         DWORD n; 
         DWORD size;
         COORD coord={0};
         CONSOLE_SCREEN_BUFFER_INFO csbi;
         HANDLE h=GetStdHandle(STD_OUTPUT_HANDLE);
         GetConsoleScreenBufferInfo(h,&csbi);
         size = csbi.dwSize.X * csbi.dwSize.Y;
         FillConsoleOutputCharacter(h,TEXT(' '),size,coord,&n);
         GetConsoleScreenBufferInfo(h,&csbi);
         FillConsoleOutputAttribute(h,csbi.wAttributes,size,coord,&n);
         SetConsoleCursorPosition(h,coord);
    }
    struct record
    {
           char name[30];
           char phone[15];
           record *next;
    }*start, *newptr, *save, *ptr, *rear;
    record *create_new_record();
    void insert(record *);
    int disp1(record *);
    int add();
    int main()
    {
        start=rear=NULL;
        char spc[6]="\n\t\t";
        int choice;
        do
        {
            clrscr();
            cout<<spc<<"--------------(  PowerContact  )-------------";
            cout<<spc<<" A console-based contacts management utility ";
            cout<<spc<<"     (c)ultrabot90/Kashish Sharma, 2007      ";
            cout<<spc<<"---------------(  Main Menu  )---------------";
            cout<<spc<<"       1.Display all contacts                ";
            cout<<spc<<"       2.Add a new contact                   ";
            cout<<spc<<"       3.Exit                                ";
            cout<<spc<<"Enter your choice...";
            cin>>choice;
            switch(choice)
            {
                       case '1':
                       {
                            ifstream readfile;
                            readfile.open("db.txt", ios::in);
                            if(!readfile)
                            {
                                         cout<<"\nFile read error.";
                                         return 1;
                            }
                            readfile.read((char *) &start, sizeof(record));
                            disp1(start);
                            break;
                       }
                       case '2':
                            cout<<"\n2 is it?";
                            add();
                            break;
                       case '3':
                            return 1;
                            break;
            }
        }while(choice!=3);
        return 0;
    }
    int add()
    {
         newptr=create_new_record();
         if(newptr!=NULL)
         {
                         cout<<"\nCreate record success.";
                         getchar();
         }
         else
         {
             cout<<"\nCreate record error.";
             return 1;
         }
         insert(newptr);
         getchar();
         return 0;
    }
    record *create_new_record(void)
    {
           ptr=new record;
           cout<<"\nName : ";
           cin.getline(ptr->name,30);
           cout<<"\nPhone : ";
           cin.getline(ptr->phone,15);
           ptr->next=NULL;
           ofstream writefile;
           writefile.open("db.txt", ios::binary|ios::app);
           if(!writefile)
           {
                         cout<<"\nFile write error.";
                         system("PAUSE");
           }
           writefile.write((char *) &ptr, sizeof(record));
           //writefile<<ptr->name<<"\n";
           //writefile<<ptr->phone<<"\n";
           writefile.close();
           return ptr;
    }
    void insert(record *np)
    {
         if(start==NULL)
                        start=rear=np;
         else
         {
             rear->next=np;
             rear=np;
         }
    }
    int disp1(record *np)
    {
         while(np!=NULL)
         {
                        clrscr();
                        cout<<"\nName : "<<np->name<<"\nPhone : "<<np->phone;
                        np=np->next;
                        cout<<"\nPress enter to continue.";
                        getchar();
         }
         return 0;
    }
    The problem is that when I enter 2 as a choice in the menu, nothing happens...Also, right now it can only add and display entries...I want to be able to 1. *search* the entries 2. sort and display all entries (the other basic functions - edit, delete - can be done by me, I guess)
    Edit - Maybe I should put it as an attachment...its filling up the page x_x
    -regards,
    ultrabot90
    Last edited by ultrabot90; 11-15-2007 at 09:28 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    choice is an int, '2' is a char with the integer corresponding value of 50. If you enter 50 as your "choice", I'm pretty sure it will do the '2' entry. But you may want to edit the code to either not use the single-quotes or to use a char as input - in this case, both are as valid, but if you intend to have more than 10 choices, you'll have to choose if you want multi-digit inputs (such as 11) or if you want single character, but use for example letters or symbols for the choices that can't be choosen with 0..9.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    37
    Quote Originally Posted by matsp View Post
    choice is an int, '2' is a char with the integer corresponding value of 50. If you enter 50 as your "choice", I'm pretty sure it will do the '2' entry. But you may want to edit the code to either not use the single-quotes or to use a char as input - in this case, both are as valid, but if you intend to have more than 10 choices, you'll have to choose if you want multi-digit inputs (such as 11) or if you want single character, but use for example letters or symbols for the choices that can't be choosen with 0..9.

    --
    Mats
    Ouch, thanks (But surely you didn't think I put those single quotes in deliberation? Awful mistake of mine.)
    Another question...When the program inputs a line from the user in create_new_record(), it simply skips that section (and shows both the couts). I looked up http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    , but when I use getline()...
    Code:
           cout<<"\nName : ";
           getline(ptr->name,30); //Error 1
           cout<<"\nPhone : ";
           getline(ptr->phone,15); //Error 2
    ...then it doesnt seem to work. Error 1 is "no matching function to call for 'getline(char[30], int)'" and Error 2 is "no matching function to call for 'getline(char[15], int)'".

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think your current code is abotu right, but you may want to add a cin.ignore() after your cin >> choice - that will remove the newline left behind by the cin >> choice and thus allow you to read the first line with getline.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    37
    Yet another problem...I tested the program thus -
    1. Compiled+Run once, then added an entry, quit.
    2. (no changes in code) Compiled+Run again, started function 1 (read from file), quit.
    It just gives me junk values...How do I do it? Also, I'd like to know how I can rid myself of that system("PAUSE") in create_new_record()...There's no process.h and exit() function...and return <value> wont work in a structure return type function...
    Thanks.

  6. #6
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    and return <value> wont work in a structure return type function...
    Isn't that what a class is used for (or can be)?

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    37
    Quote Originally Posted by mikeman118 View Post
    Isn't that what a class is used for (or can be)?
    But what should I do bout the junk values? o.o

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a menu that reads input via an array?
    By Nalif in forum C Programming
    Replies: 6
    Last Post: 09-29-2006, 09:21 PM
  2. menu - options Problems
    By wakish in forum C Programming
    Replies: 3
    Last Post: 07-05-2006, 03:23 AM
  3. Problems with a menu
    By L_U_K_E in forum C++ Programming
    Replies: 27
    Last Post: 04-28-2006, 10:45 AM
  4. Problems with my menu
    By Olidivera in forum Windows Programming
    Replies: 2
    Last Post: 07-07-2005, 12:44 PM
  5. Menu stuff
    By Shadow in forum C Programming
    Replies: 10
    Last Post: 04-28-2002, 09:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21