Jump to content

C++ - Database Project


Hawks

Recommended Posts

I am tasked with independently studying C++, generally learning to code and stuff. My question is if you know of any good online resources. I've found two, cpp-tutorial.cpp4u.com and cplusplus.com. Right now I need to learn about strings... So anything you might have to share on that aspect would be helpful as well.

 

I am currently working on a database project. I have a class (Dbs) that contains stuff. I've made a vector <Dbs> people (1, Dbs()) in my constructor... Problem is when I tell other functions to use people[x].vars compiler complains it is undeclared, and understandably so. How do I fix this? It's probably some ridiculously simple thing I don't remember...

 

Also I get expected primary expression before ')' token in this line and those following it- probably a simple fix as well, but I don't know it.

std::getline(std::cin, people[x].setName(std::string));
std::cout << "\nAge:";
std::cin >> people[x].setAge(int age);
std::cout << "\nAddress:";
std::getline(std::cin, people[x].setAddress());

 

[hide=Code]

///////////////
//Database Project
//Jackie
///////////////

#include <iostream>
#include <string>
#include <fstream>
#include <vector>

class Dbs
{
     private:         //stuff lives in here as each item - don't mess with it
        int _num, _age, _zip;
        std::string _name, _address, _city, _state;     
     public:           //lets other parts of program get and edit (?) data
        void setAge(int age) 
             {_age=age;};
        void setZip(int zip)
             {_zip=zip;};
        void setName(std::string name) 
             {_name=name;};
        void setAddress(std::string address)
             {_address=address;};
        void setCity(std::string city)
             {_city=city;};
        void setState(std::string state)
             {_state=state;};
        std::string getName() 
             {return _name;};
        std::string getAddress()
             {return _address;};
        std::string getCity()
             {return _city;};
        std::string getState()
             {return _state;};
        int getAge() 
             {return _age;};
        int getZip()
             {return _zip;};
        Dbs(void); //constructor
};
////////////////////////////////////////////////////////
void viewEnt();
int addEnt();
void sortEnt();
void sortBy(std::string);

int main()
{
char choice;
int entries;

Dbs::Dbs();

    do {
   std::cout << "Database Menu\n\n1. View Entry\n2. Add Entry\n3. Sort Entries\n4. Exit" << std::endl;
   std::cin >> choice;

   switch (choice)
   {
          case '1':
               viewEnt();
               break;

          case '2':
               addEnt();
               break;

          case '3':
               sortEnt();
               break;

          case '4':     
               break;

          default:
               std::cout << "Please choose 1, 2, 3, or 4.\n";
               std::cin.ignore();
               break;
               }
      } while (choice!='4');     
return 0;   
std::cin.ignore();
}             


/////////////////////////////////////////////////////////////////
Dbs::Dbs() //define constructor here
{
std::vector<Dbs> people (1, Dbs());           
          }

//////////////////////////////////////////
void viewEnt()
{
    int entrynum;
std::cout<<"Which entry do you want to view?\n<Press 0 for all.>";
std::cin>>entrynum;

if (entrynum!=0)
{
std::cout<<"\nName: "<<people[entrynum-1].name<<std::endl;
std::cout<<"Age: "<<people[entrynum-1].age<<std::endl;
std::cout<<"Address:\n"<<people[entrynum-1].address<<"\n"<<people[entrynum-1].city<<", "<<people[entrynum-1].state<<" "<<people[entrynum-1].zip<<std::endl; 
} else
{
  for (int x=0; x<people.size; x++)
   {
    std::cout<<"\nName: "<<people[x].name<<std::endl;
    std::cout<<"Age: "<<people[x].age<<std::endl;
    std::cout<<"Address:\n"<<people[x].address<<"\n"<<people[x].city<<", "<<people[x].state<<" "<<people[x].zip<<std::endl; 
   } 
} 

    } 
///////////////////////////////////////////
int addEnt()     
{ 
 std::string name, address, city, state;
 int age, zip, entries;
 char x='y';   


if (x='y')
{ 
people.push_back();              
std::cout << "Add an Entry\nEntry Name:";
std::getline(std::cin, people[x].setName(std::string));
std::cout << "\nAge:";
std::cin >> people[x].setAge(int age);
std::cout << "\nAddress:";
std::getline(std::cin, people[x].setAddress());
std::cout <<"\nCity, State, and Zip Code";
std::cin << people[x].setCity(std::string city) << people[x].setState(std::string state) << people[x].setZip(int zip);
std::cout<<"Add another entry? <y/n>";
std::cin>>x;
} else {
return 0;         
}    
}
////////////////////////////////////
void sortEnt()
{
    char opt;               
std::cout << "Sort by:\n 1.Name\n 2.Age\n 3.Address\n 4.City\n 5.Exit Sort" << std::endl;
std::cin>>opt;

              switch(opt)
              {
                         case '1':
                              sortBy(people.name);
                              break;
                         case '2':
                              sortBy(people.age);
                              break;
                         case '3':
                              sortBy(people.address);
                              break;
                         case '4':
                              sortBy(people.city);
                              break;
                         default:
                              break;
                         }              
}              
///////////////////////////////////             

void sortBy(std::string)
{
std::string str1,str2;

      for (int x=0; x<people.size; x++)
           {
                str1=people[x].;
                str2=people[x+1];

                if (str2.compare(str1) >0 ) 
               {      
                      std::string hold;
                      hold.swap (str2);
                      str2.swap (str1);
                      str1.swap (hold);
               }
           }
   }


[/hide]

Thanks!

Link to comment
Share on other sites

I've done a lot of work with C++ and am actually currently taking a 3D rendering with C++ course. If you have any specific questions please by all means feel free to PM me although I don't really have any 'handy resources' that I can grab off the top of my head. :)

Tecmaster532.png
Tecmaster532.png
lm9dzxe3.png
Owner of the Quest Point Cape

Link to comment
Share on other sites

  • 1 month later...

May aswell start with your constructor.

 

Dbs::Dbs()
{
   std::vector<Dbs> people (1, Dbs());           
}

 

First of all, people is initialized as a local variable because it is within the scope of a function, this is why you are getting "undeclared identifier". Should also point out that: the constructor Dbs() creates a std::vector of type class Dbs, and constructs another Dbs() object which is added to the std::vector. Then this newly constructed Dbs() creates a std::vector of type class Dbs, and constructs another Dbs() object which is added to the std::vector. Then this newly constructed Dbs()... :D You get the point.

 

Let's start some code:

 

Let's start with making the class that will store our data per entry. Note that ID is under public(just the way this was designed(not the best way!)). We have declared more than one constructor, a default constructor and a custom one.

class Entry
{
private:
int age;
int zip;

std::string name;
std::string address;
std::string city;
std::string state;
public:
int ID;

Entry();
Entry(int _age, int _zip, std::string _name, std::string _address, std::string _city, std::string _state);

void setAge(int _age) {age = _age;};
void setZip(int _zip) {zip = _zip;};
void setName(std::string _name) {name = _name;};
void setAddress(std::string _address){address = _address;};
void setCity(std::string _city) {city = _city;};
void setState(std::string _state) {state = _state;};

int getAge() {return age;}
int getZip() {return zip;}
std::string getName() {return name;};
std::string getAddress() {return address;};
std::string getCity() {return city;};
std::string getState() {return state;};
};

 

But as soon as you declare your own constructor for a class, the compiler no longer provides an implicit default constructor.

 

Entry::Entry()
{
}

 

Since we have declared our custom constructor we need to define it. This constructor allows us to initialized all variables in the entry without calling every setFunction.

 

Entry::Entry(int _age, int _zip, std::string _name, std::string _address, std::string _city, std::string _state)
{
age = _age;
zip = _zip;
name = _name;
address = _address;
city = _city;
state = _state;
}

 

Now let's create a class to represent our database. uID means unique ID (Im guessing this is what you intended your original "num" to be). We have a std::vector to store our entries in, a constructor and a function to addEntry.

 

class Database
{
private:
int uID;
std::vector<Entry> tableEntries;
public:
Database();

void addEntry(Entry e);
};

 

We must initialized uID before we use it, so we change the default constructor.

 

Database::Database()
{
uID = 0;
}

 

Define our addEntry function. We are passing a class object of type Entry to addEntry. Use uID to give the entry a unique id. Add the entry to the std::vector. Increment uID for use next time.

 

void Database::addEntry(Entry e)
{
e.ID = uID;
tableEntries.push_back(e);

uID++;
}

 

Let's give it a go. First initialized a class object of Database. Then were going to want to add an entry, so initialized a class object of Entry. Fill it in. Use our Database's class addEntry to add the entry to the database. The second addEntry uses our custom constructor to make things easier.

 

int main()
{
Database db;

Entry test;
test.setAge(12);
test.setZip(1337);
test.setName("Mark");
test.setAddress("a");
test.setCity("b");
test.setState("c");

db.addEntry(test);
db.addEntry(Entry(12, 1337, "using constructor", "a", "b", "c"));
}

 

All in one: http://pastebin.com/QdUJjaPj

 

These are the basics, try implementing your other stuff.

Link to comment
Share on other sites

As for

 

Also I get expected primary expression before ')' token in this line and those following it- probably a simple fix as well, but I don't know it.

std::getline(std::cin, people[x].setName(std::string));
std::cout << "\nAge:";
std::cin >> people[x].setAge(int age);
std::cout << "\nAddress:";
std::getline(std::cin, people[x].setAddress());

 

std::getline(std::cin, people[x].setName(std::string));

 

The actual error is that it expected an identifier (name) for the type std::string eg: std::getline(std::cin, people[x].setName(std::string lol));

But it wouldn't have worked anyway. Wrong I think.

 

See <string>'s @ http://www.cplusplus.com/reference/string/getline/

The 2nd parameter should be a "string object". You could modify getName to return the string object but this requires pointers which you'll learn about later on. With a modified getName you could then do: std::getline(std::cin, *(people[x].getName()));

 

Anyway, you could fix this with something like:

 

std::string line;
std::getline(std::cin, line);
people[x].setName(line);

 

 

 

 

You should follow cplusplus.com, don't jump ahead, always go backwards. Theres many different solutions to a given problem in programming, to me, it's about finding the most efficient solution.

Edited by Markup
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.