string

This is a discussion on string within the C++ Programming forums, part of the General Programming Boards category; i have a string and its in red and it dosne get an input i just king of skips over ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    47

    string

    i have a string and its in red and it dosne get an input i just king of skips over it... : (
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int x;
    class database
    {
          private:
                  int id_number,rank;
                  float salery;
                  char string[20];
          public:
                 void name ();
                 int position ();
                 void quarry ();
                 void respond ();
    };
    
    database data;
    
    void database::name ()
    {
         cout<<"what is the nameof your employee?"<<endl;
         cin.getline ( data.string, 20, '\n' );
    }
    int database::position ()
                 {
                      switch (rank)
                      {
                             case 1:
                                  cout<<"cashier";
                                  break;
                             case 2:
                                  cout<<"Bag boy";
                                  break;
                             case 3:
                                  cout<<"delly";
                                  break;
                             case 4:
                                  cout<<"janeter";
                                  break;
                             case 5:
                                  cout<<"banker";
                                  break;
                             case 6:
                                  cout<<"produce";
                                  break;
                             case 7:
                                  cout<<"vice-maneger";
                                  break;
                             case 8:
                                  cout<<"maneger";
                                  break;
                             case 10:
                                  cout<<"1) cashier \n2) bag boy \n3) delly \n4) janeter \n5) banker \n";
                                  cout<<"6) produce \n7) vice-maneger \n8) maneger \n";
                                  cin>> rank;
                                  position ();
                                  cin.get();
                                  break;
                             default:
                                     cout<<"not in database";
                                     break;
                      }
                 }
    void database::quarry ()
                 {
                      cout<<"Employee id number?"<<endl;
                      cin>> id_number;
                      cout<<"The salery of employee :"<< id_number<<endl;
                      cin>> salery;
                      cout<<"The current status of employee?     10 for listings"<<endl;
                      cin>> rank;
                 }
     void database::respond ()
                 {
                      cout<<"employee id number to employee ["<<string<<"] is :"<<id_number<<endl;
                      cout<<"employee salery :"<<salery<<" per hour"<<endl;
                      cout<<"your employee's position is :";
                      position ();
                      cout<<endl;
                 }
    
    int main()
    {
        cout<<"how many employees do u have to enter?"<<endl;
        cin>> x;
        while (x > 0)
        {
        data.name ();
        data.quarry ();
        data.position ();
        data.respond ();
        cin.get();
        x --;
        }
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    This is a common problem that has to do with mixing >> and getline in the same program. Basically you'll need to call the ignore() method of cin to clear the input stream before you call the getline() method. The exact syntax you use with ignore() is up to you. Often the default ignore() will work, but if you want to be more robust, there are lot's more sophisticated options available.
    You're only born perfect.

  3. #3
    *this
    Join Date
    Mar 2005
    Posts
    498
    btw, probably doesnt matter but....

    maneger -> manager
    vice-maneger -> vice-manager
    janeter -> janitor
    salery -> salary
    u -> you
    nameof -> name of
    delly -> deli

    And you should probably make yourself a default constructor so you can assign private variables.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    47
    i was really using it to get used to using a class but thanx

  5. #5
    *this
    Join Date
    Mar 2005
    Posts
    498
    lol ya I know but I am so lonely and have nothing better to do...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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