Use of String as a parameter for an if statement

Problem accessing information using a set and get function

Page 1 of 1

3 Replies - 515 Views - Last Post: 14 May 2010 - 06:04 AM Rate Topic: -----

#1 dhillon5573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 22-April 10

Use of String as a parameter for an if statement

Posted 13 May 2010 - 11:10 AM

Dear coders,

I really need a hand trying to get my program to recognize my user defined string choices.
Have made a function to set a string to the relevant name and one to get it,
But im facing problems trying to insert the get function into the parameters for my if statement.

This is my header file for School Class:


#ifndef SCHOOL_H
#define SCHOOL_H

#include<iostream>
#include<cmath>
#include<string>
#include <stdlib.h>
#include "Record.h"

using namespace std;


class School: public  virtual Record {

protected:

	 string SchoolName;

public:

	School(){}
	School(string pSchool){SchoolName = pSchool;}
	~School(){}

	void setSchool(string pSchool) {SchoolName = pSchool;}

    string getSchool() const {return SchoolName;}

	 
	School(const School &S)
		{
			SchoolName = S.getSchool();
		}			

	void InfoDisplay(){
		
		cout <<"The University of Manchester compartmentalizes its database according to various Schools"
 			 <<"\nPlease Select the School the Student"<<" is enrolled in: \n\n"
			 <<"School of Physics and Astronomy	[1]\n"
			 <<"School of Chemistry		[2]\n"
			 <<"The School of Rock		[3]\n"<<endl;

		//cout<<"Welcome to the record for"<<getFamilyName()<<endl;
	}


	void Info(){


	char choice;
	cin>>choice;

	  switch (choice)
	  {
		case '1':cout<<"Selection:\nSchool of Physics and Astronomy\n" <<endl;

				setSchool("School of Physics and Astronomy");
				//system("CLS");
																					break;

		case '2':cout<<"Selection:\nSchool of Chemistry\n"<<endl;

				setSchool("School of Chemistry");

			//	system("CLS");
			//	ChemistryCourses();
			//	system("CLS");
			//	ChemistryModules();
																					break;

		case '3':cout<<"Selection:\nSchool of Rock\n"<<endl;						break;

			break;

			

			
	  }

	}



};



#endif





And this is my main impementations:




#include <iostream>
#include <string>
#include <cmath>
#include <stdlib.h>				//Clean & Clear
#include <fstream>
#include "School.h"
//#include "Welcome.h"
#include "Record.h"
#include "Student.h"
#include "PhysicsCourses.h"
#include "ChemistryCourses.h"
#include "Gradebook.h"


int main()
{		
	// Open Outputfile
	//const char outputfile[] = "Output.txt";
	
//	ofstream output(outputfile);

	int nStudents;

	cout<< "Please enter number of students to add in database?" <<endl;
	cin>>nStudents;

	

	Record **Rec = new Record *[nStudents];

		for(int j=0;j<nStudents;j++)
    {	

		cout<<"Student "<<j+1<<":\nPlease provide the following details to add to the record:"  << "\n";

		Rec[j]= new Student ();
		Rec[j] ->Info();

		Rec[j] = new School ();
		
		Rec[j]->InfoDisplay();
		Rec[j] ->Info();

	


		if(School::getSchool()=="School of Physics and Astronomy"){				//This Does not Work!!

			Rec[j] = new PhysSchoolCourses();

			Rec[j]->InfoDisplay();
				Rec[j]->Info();
		
		}

		else if (/*What goes in here???*/)){

		Rec[j] =new ChemSchoolCourses();
		
		Rec[j]->InfoDisplay();
		Rec[j] ->Info();
		}


		//Rec[j]= new ModuleList();
		//Rec[j]->Info();
		Rec[j] = new GradeBook();
		Rec[j] ->Info();
Rec[j]->InfoDisplay();
		
		//GradeBook::printGradeBook();
		
	}


  for(int j=0;j<nStudents;j++)
    {	
		//Rec[j]->StudentInfo();
		

	//	cout<<"The information required for student "<<j+1<<" is :" << << "\n\n";
		
     // delete Rec[j];
	  //Rec[j]=NULL ;// Delete object 

  }

 // output.close();
 // delete Rec;// Delete array of base class pointers
			


  return 0;

}





I tried using the strcopy function but have trouble getting it to compile right as it seems i need to make an array for that to work. If someone can show me how to do the explicitly I would be very thankful. For the moment, my code gives this error::

1>c:\users\gurpreet\desktop\student database\main implementation.cpp(47) : error C2352: 'School::getSchool' : illegal call of non-static member function

Is This A Good Question/Topic? 0
  • +

Replies To: Use of String as a parameter for an if statement

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Use of String as a parameter for an if statement

Posted 13 May 2010 - 11:23 AM

In class "School", you have the following member function:
string getSchool() const {return SchoolName;}


which is NOT a static member function. Though, here:
if(School::getSchool()=="School of Physics and Astronomy"){ 


you are using the scope resolution operator '::' to access "getSchool()", which is wrong. '::' should be used only when that member function is static. Otherwise, you need to instantiate the class first, then call the function. Something like this:
School school;
// Do something here ...
if (school.getSchool()  ....)


Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Use of String as a parameter for an if statement

Posted 13 May 2010 - 11:26 AM

You need a school instance, or a pointer to a school instance, and then call that particular school object's getSchool() function.

e.g.
School sch( "School of Engineering");
School *sp = &sch;
// ... code 
if( sp->getSchool() == //...
// or:
if( sch.getSchool() == //...


Was This Post Helpful? 1
  • +
  • -

#4 dhillon5573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 22-April 10

Re: Use of String as a parameter for an if statement

Posted 14 May 2010 - 06:04 AM

Thank for that.. It was very helpful in helping me understand my problem. I tried working on it but there was still a few bugs that I needed correcting. Woke up this morning and thought maybe it would be a good idea to cast all my strings to ints using enums so it saves computing time. Thanks very much.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1