Pointers

'const char [7]' to 'char [50]'

Page 1 of 1

3 Replies - 3871 Views - Last Post: 11 April 2010 - 12:53 PM Rate Topic: -----

#1 Zango  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-April 10

Pointers

Posted 11 April 2010 - 08:03 AM

So here is my issue. I have this program for class and I've got everything down to work except for displaying the resistor type. The following is the error that I receive:

1>c:\users\randy\documents\visual studio 2008\projects\cpetersonweek6lab06exercise\cpetersonweek6lab06exercise\fancyresistorclasspointer.cpp(178) : error C2440: '=' : cannot convert from 'const char [7]' to 'char [50]'
1> There is no context in which this conversion is possible


It is the same for lines 178, 182, 186, and 190. I'm just looking for someone to point me in the right direction.

/******************************************************************
* Programmer: 
*
* Date: April 11, 2010
*
* Course: 
*
* Assignment: pointers with FancyResistorClass
*
* Description: This program uses pointers to display attributes for 
*				a resistor class. 
*  
* Output: screen - displays various resistor data
* 
********************************************************************/ 

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

//
//CLASS DECLARATION SECTION
//
class ResistorClass {
protected:
	double *m_dptrRes;
	char *m_sptrResName;
public:
	static int m_istResCounter;
	void DisplayResistor(void);
	void EnterResistance(void);
	ResistorClass();
	ResistorClass(char Name [], double nominalResistance, double Tolerance);
	~ResistorClass();
	enum resistorValues {NOMINAL, TOLERANCE, MAX, MIN};
};

int ResistorClass::m_istResCounter = 0; //intialize static counter


class FancyResistorClass:public ResistorClass {
protected:
	char *m_sptrResType;
public:
	void DisplayResistor(void);
	FancyResistorClass(char Name[],double nominalResistance,double Tolerance);
	~FancyResistorClass();                
};


int main() {
	//Create the first resistor using the default constructor
	ResistorClass oResOne;
	oResOne.DisplayResistor();
	//Create the second resistor using the parameterized constructor
	ResistorClass oResTwo("RESISTOR2", 4700, .20);	
	oResTwo.DisplayResistor();
	//Create a fancy Resistor
	cout << endl;
	FancyResistorClass oResThree("FancyResistor", 3000, .10);	
	oResThree.DisplayResistor(); 
} //end main

void ResistorClass::DisplayResistor (){
//Displays all Resistor object data members 
	//set the output parameters
	cout << setprecision(2); 
	cout << fixed << showpoint;
	//display the output
	cout << "\n\n";
	cout << setprecision(2) << fixed << showpoint << setfill(' ');
	cout << "Values for " << m_sptrResName << " are:\n" ;
	cout << left  << setw(25) << "Resistor Nominal Value = " 
           << right << setw(10) << m_dptrRes[NOMINAL] << "\n";
	cout << left  << setw(25) << "ohmsResistorTolerance =  " 
           << right << setw(10) << (100*m_dptrRes[TOLERANCE]) << "% \n";
	cout << left  << setw(25) << "Minimum Resistance =   "  
           << right << setw(10) << m_dptrRes[MIN] << " ohms\n";
	cout << left  << setw(25) << "Maximum Resistance =     " 
           << right << setw(10) << m_dptrRes[MAX] << " ohms\n" ;
}

void ResistorClass::EnterResistance (){
	//Displays the current value for m_dResValue
      //and prompt the user to enter a new value.
	bool hasError = false; //default error condition
	//continue prompting user for input until values are valid
	do {
		//input data 
		cout << "\n\n";
		cout << "Enter the Nominal Resistor Value :";
		cin >> m_dptrRes[NOMINAL];
		cout << "Enter the Resistor Tolerance Value :";
		cin >> m_dptrRes[TOLERANCE];
		//input error checking
		hasError = false;
		if (m_dptrRes[NOMINAL] < 0 || m_dptrRes[NOMINAL] > 10000) {
			cout << "\n\n";
			cout << "Error: Resistor Value" << endl << "Entry must be between 0 and 10,000\n";
			m_dptrRes[NOMINAL]  = -1; //denotes error value
			hasError = true;
		}
		if (m_dptrRes[TOLERANCE] > 1) {
			cout << "\n\n";
			cout << "Error: Tolerance Value" << endl << "Entry must be a decimal value <= 1\n";
			m_dptrRes[TOLERANCE] = -1; //denotes error value
			hasError = true;
		}
	} while (hasError);
	//Calculate new Values
	m_dptrRes[MIN] = m_dptrRes[NOMINAL] - (m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE]);
	m_dptrRes[MAX] = m_dptrRes[NOMINAL] + (m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE]);
	//display the new values
	DisplayResistor();
}

ResistorClass::ResistorClass(){
//default constructor
	string Name;
    m_dptrRes = new double[4];
	//default values
	m_dptrRes[NOMINAL] = 1000;
	m_dptrRes[TOLERANCE] = .1; 
    //calculate min and max values
	m_dptrRes[MIN] = m_dptrRes[NOMINAL] - (m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE]);
	m_dptrRes[MAX] = m_dptrRes[NOMINAL] + (m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE]);
	//input resistor's name
	cout << "Enter resistors name(default): ";
    m_sptrResName = new char[50];
	cin.getline(m_sptrResName, 50);
    m_istResCounter++;
	cout << "\nDefault constructor was called\n";
}//end default constructor

ResistorClass::~ResistorClass(){
	//destructor
	cout << "\nDestructor call for " << m_sptrResName << endl;
	delete[]m_dptrRes;
    delete[]m_sptrResName;
    m_dptrRes=NULL;
    m_sptrResName=NULL;
	m_istResCounter--;	 
}//end destructor

ResistorClass::ResistorClass(char Name[],double nominalResistance,double Tolerance) {
	m_dptrRes = new double[4];
    //default values
    m_dptrRes[NOMINAL] = nominalResistance;
    m_dptrRes[TOLERANCE] = Tolerance;
    //calculate min and max values
    m_dptrRes[MIN] = m_dptrRes[NOMINAL] - (m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE]);
	m_dptrRes[MAX] = m_dptrRes[NOMINAL] + (m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE]);
    //Resistors Name
    m_sptrResName = new char[50];
    strcpy_s(m_sptrResName,50,Name);
    m_istResCounter++;
    cout << "\nParameterized constructor was called\n";
}

FancyResistorClass::FancyResistorClass(char Name [],double nominalResistance, double Tolerance) : ResistorClass(Name, nominalResistance, Tolerance) {
	char userInput;
    char resType[50];
	do{
		//Prompt user for an single character entry
		cout << "\n";
        cout << "\nC for carbon resistor type";
        cout << "\nM for metal film resistor type";
        cout << "\nF for carbon film resistor type";
        cout << "\nP for power resistor type\n";
		//get the user’s input
        cin >> userInput;
		//Use a switch statement to map the user’s character to the full resistor type
		switch(userInput) {
				case 'C':
				case 'c':
                        resType = "Carbon";
						break;
                case 'M':
				case 'm':
                        resType = "Metal Film";
                        break;
                case 'F':
				case 'f':
                        resType = "Carbon Film";
						break;
                case 'P':
				case 'p':
                        resType = "Power";
						break;
                }
                }while(userInput == 'x');// x is an error indicator that keep’s the loop iterating when the
                                         //user enters invalid data
		m_sptrResType = new char[50];
		strcpy_s(m_sptrResType, 50, resType);
		cout << "\nFancyResistorClass Constructor Called\n";
}

void FancyResistorClass::DisplayResistor(){
	//overrides ResistorClass
	//Displays all Resistor object data members 
	//set the output parameters
	cout << setprecision(2); //see Ch3 page 136 in text book
	cout << fixed << showpoint;
	//display the output
	cout << "\n\n";
	cout << "Fancy Resistor:\n";
	ResistorClass::DisplayResistor();
	cout << "Resistor Type : " <<  m_sptrResType;
	cout << endl;
}//end display resistor

FancyResistorClass::~FancyResistorClass(){
//destructor
  cout << "\nDestructor call for " << m_sptrResType << endl;
  delete m_sptrResType; //avoid a memory leak
  m_sptrResType = NULL; //not really required here, the class will soon be destroyed
} //end destructor



Any help would be greatly appreciated.

This post has been edited by Zango: 11 April 2010 - 08:05 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Pointers

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Pointers

Posted 11 April 2010 - 08:10 AM

Use strcpy to assign data to your array of characters:
strcpy(resType, "Carbon");


etc ...
Was This Post Helpful? 2
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Pointers

Posted 11 April 2010 - 08:11 AM

char resType[50];
...
resType = "Carbon";



You cannot assign to arrays like that. You must use strcpy().

Ninja'd...knew it :)
Was This Post Helpful? 2
  • +
  • -

#4 Zango  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-April 10

Re: Pointers

Posted 11 April 2010 - 12:53 PM

Thank you both so much! That did it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1