noob c++ needs help :O

CrazYmonkeY159

Expert Member
Joined
Sep 13, 2007
Messages
2,142
I am having trouble with the program i am trying to write maybe i am declaring my class wrong or something but ive tried almost everything :(


main.cpp

http://pastebin.co.za/84883

in main.cpp i made a static method and it doesnt seem to work when i call it? how do i call it? (method displayText()

imp.cpp

http://pastebin.co.za/84884

this is my implementation file for my class called DataBase

and heres the header database.h

http://pastebin.co.za/84885

what is wrong?
 

alt146

Well-Known Member
Joined
Feb 2, 2010
Messages
464
It's been a while since I last coded anything in c++, but I'm reasonably sure you need to be calling aDB.method() to access the functions in your class. You might also wanna put a switch statement in there, rather than a gazillion if statements, it's better code.
 

sn3rd

Expert Member
Joined
Jan 18, 2008
Messages
4,305
Er...

Declare it first? The code is processed sequentially at compile time, so you need to declare

Code:
static void displayText();

before you can use it in your main(). Alternatively, you can just move the definition of displayText() to BEFORE main().
 

Drake2007

Expert Member
Joined
Oct 23, 2008
Messages
4,413
shouldn't the compiler spit out an error or a warning at least, if it's not declared? On that note what IDE are you using?
 

sn3rd

Expert Member
Joined
Jan 18, 2008
Messages
4,305
And because I'm such a nice guy:

main.cpp
Code:
#include <iostream>
#include <string>
#include "database.h"

using namespace std;

static void displayText();

int main()
{

  char input;
  
  DataBase aDB;
  displayText();
  cout << "\nWelcome to the student record database!\n";
  cout << "0:\t Add student\n";
  cout << "1:\t Delete student\n";
  cout << "2:\t read database\n";
  cout << "3:\t Save database\n";
  cout << "4:\t Display student\n";
  cout << "5:\t Grade student\n";
  cout << "-----------------------\n";
  cout << "q:\t Quit\n";
  cout << "Enter a number (or q to quit) and press return...\n";
  cin >> input;
  if(input == '0')
  {
    aDB.addStudent();
  }
  if(input == '1')
  {
    aDB.delStudent();
  } 
  if(input == '2')
  {
    aDB.readDB();
  }
  if(input == '3')
  {
    aDB.saveDB();
  }
  if(input == '4')
  {
    aDB.displayStudent();
  }
  if(input == '5')
  {
    aDB.gradeStudent();
  }
  if(input == 'q')
  {
    cout << "q was pressed";
  }
  if(input == 'Q')
  {
    cout << "q was pressed";
    //  break;
  }

  return 1;
  
}

static void displayText()
{
  cout << "\nWelcome to the student record database!\n";
  cout << "0:\t Add student\n";
  cout << "1:\t Delete student\n";
  cout << "2:\t read database\n";
  cout << "3:\t Save database\n";
  cout << "4:\t Display student\n";
  cout << "5:\t Grade student\n";
  cout << "-----------------------\n";
  cout << "q:\t Quit\n";
  cout << "Enter a number (or q to quit) and press return...\n";
}

database.h
Code:
#ifndef DATABASE
#define DATABASE

#include <string>

using namespace std;

class DataBase
{

  public:
  
  
  void addStudent();
  
  void delStudent();
  
  void readDB();
  
  void saveDB();
  
  void displayStudent();
  
  void gradeStudent();

};
#endif

database.cpp
Code:
#include <iostream>
#include "database.h"

void DataBase::addStudent()
{
  cout << "\naddStudent() was called\n";
}

void DataBase::delStudent()
{
  cout << "\ndelStudent() was called\n";
}

void  DataBase::readDB()
{
  cout << "\n readDB() was called\n";
}

void DataBase::saveDB()
{
  cout << "\n saveDB() was called \n";
}

void  DataBase::displayStudent()
{
  cout << "\n displayStudent() was called \n";
}

void DataBase::gradeStudent()
{
  cout << "\n gradeStudent() was called \n";
}

I tried to change as little as possible to make the code "correct", so that it preserves your own personal code.

Things to note!!!

1) You need to provide qualifiers for your definitions in database.cpp :
You can't just say "void addStudent()". Rather, you need to say "void DataBase::addStudent()" which says that addStudent() is a member of DataBase.
2) You provided empty implementations in your database.h file. In this case, you were defining methods of DataBase that did nothing!
3) You need to use the dot operator, ".", to access the members of a class instance.
 

CrazYmonkeY159

Expert Member
Joined
Sep 13, 2007
Messages
2,142
Thank you all :D its fixed now, i knew that this code looked quite bloated, but im trying to walk before i can run. i appreciate your inputs!
 
Top