I am totally stuck. I have worked on this for a week. I clean up the bugs and find more. It didn't look too hard to start with. I have to write an OOP program that displays the default info and then asks users for input. I have two headers two function files and a driver file. When I started out adding things slowly, the program compiled but the output from studyCPP was garbage. Not sure how to fix that either.

Here are the last bastions of errors. Everytime I try and fix it, I just cause more.

1>c:\users\shell\documents\visual studio 2008\projects\quiz4-taketwo\lightcpp.cpp(35) : error C2664: 'strncmp' : cannot convert parameter 1 from 'std::string' to 'const char *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>StudyCPP.cpp
1>c:\users\shell\documents\visual studio 2008\projects\quiz4-taketwo\studycpp.cpp(29) : error C2664: 'strncmp' : cannot convert parameter 1 from 'std::string' to 'const char *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Any help or guidance is greatly appreciated. - Thanks, Mikhala

light.h

#ifndef LIGHT_H
#define LIGHT_H
#include<string>

using namespace std;

class LightBulb
{
public:
	//no argument constructor
	LightBulb();
	//constructer with arguments
	LightBulb(string, int);
	
	void setWattage(int);
	void setType(string);
	
	int getWattage()const;
	string getType()const;

	void printL()const;

private:
	int w; //wattage
	string type;

};//end class LightBulb
#endif

study.h

#ifndef STUDY_H
#define STUDY_H
#include<string>

using namespace std;

#include"Light.h"

class Study:public LightBulb
{
public:
	//no argument constructor
	Study();

	//constructor with arguments
	Study(string, int, double, string);

	//set functions
	void setLength(double);
	void setModel(string);

	//get functions
	double getLength()const;
	string getModel()const;

	//print function
	void printS()const;

private:
	double length;
	string model;
	
};//end Study
#endif

lightCPP.cpp

#include "Light.h"
#include <iostream>
#include <string>

using namespace std;

LightBulb::LightBulb()
{
	w=75;
	type="incandescent";

}//end constructor
LightBulb::LightBulb(string typ, int ww)
:w(ww)
{
	type=typ;

}//end constructor
void LightBulb::setWattage(int a)
{
	w=a;
}//end setWattage
void LightBulb::setType(string ty)   
{	
	string str[3] = {"Incandescent","Flourescent","LED"};
	int n;
		for (n=0 ; n<3 ; n++)
		if (strncmp (str[n],ty,1) != 0)
		{
			cout<<("found %s\n",str[n]);
		}
}//end setType
int LightBulb::getWattage() const
{
	return w;
}//end getWattage
string LightBulb::getType() const
{
	return type;
}//end getType
void LightBulb::printL() const
{
	cout<<"\nLight Type is: " <<getType();
	cout<<"\nWattage is: " <<getWattage();
}//end of print

studyCPP.cpp

#include "Study.h"
#include <iostream>
#include <string>

using namespace std;

Study::Study(string t, int wat, double len, string m)
:LightBulb(t, wat)
{
	m="Table Top";
}//end constructor
void Study::setLength(double ln)
{
	ln=13.5;
	length=(ln>0? ln : 0.0);
	cout<<"Length can't be zero or negative! Try again ";
}//end setLength
void Study::setModel(string md) 
{
	string str[3] = {"Clip-On","TableTop","Hanged"};
	int n;
		for (n=0 ; n<3 ; n++)
		if (strncmp (str[n],md,1) != 0)
		{
			cout<<("found %s\n",str[n]);
		}	
}//end setModel
double Study::getLength() const
{
	return length;
}//end getLength
string Study::getModel() const
{
	return model;
}//end getModel
void Study::printS() const
{
	LightBulb::printL();
	cout<<"\nLength = " <<getLength();
	cout<<"\nModel is: " <<getModel();

}//end print

driver.cpp

#include "Study.h"
#include <iostream>
#include <string>

using namespace std;

void main()
{

	cout<<"\n*** Info about Program defined StudyLight ***\n\n";
	Study myStudy("incandescent",75,13.5,"TableTop");
	myStudy.printS();
	cout<<endl<<endl;

	//ask for user input
	//define variables
	int watt;
	double len;
	string typ, mod;

	cout<< "\nEnter wattage ";
	cin>>watt;

	cout << "\nEnter LightBulb Type ";
	cout << "\nI: Incandescent";
	cout << "\nF: Flourescent";
	cout << "\nL: LED";
	cout << endl;
	cin>>typ;

	cout << "\nEnter Light Model ";
	cout << "\nC: Clip";
	cout << "\nT: TableTop";
	cout << "\nH: Hanged";
	cout << endl;
	cin>>mod;

	cout<< "\nEnter Study Light length ";
	cin>>len;

	//show Light Study from User input
	cout<<"\n*** Info about your Study Light ***\n\n";
	Study yourStudy(typ,watt,len,mod);
	yourStudy.printS();
	cout<<endl<<endl;



}//end main

Even though it's wordy, the compiler is trying to tell you something. Namely, that std::string variables can't be used in a strncmp (or other related) function. You have 2 alternatives (actually, it's C++, so you probably have 9 or 10 options, but 2 will do).

1) use the "copy" method on the std::string to copy a char * out of the string and into a variable (one for each string) so you can use the strncmp function :icon_eek: .
2) the better option, use the substr method on the strings you're comparing to specify the portion of the strings to compare. Then you can just use:

if (strA.substr(0,1) == strB.substr(0,1))
{
    // true case
}
Member Avatar for jencas

Use string.c_str() to get a char const *

So, I add this into my function.cpp for each of the two classes. Now if I can remember the string lessons!

taking a stab at it...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.