tags:

views:

39

answers:

2
#include <iostream>
using namespace std;
class CResistor
{
public:

 CResistor(){m_dResValue=1000;
    m_dTolerance=0.10;
    m_dMinResistance=m_dResValue-(m_dResValue*m_dTolerance);
    m_dMaxResistance=m_dResValue+(m_dResValue*m_dTolerance);
    cout<<"Please input resistor name"<<endl;
    cin>>m_cResistorName[20];};

 void DisplayResistor(){
     cout<<"Constructor called m_dResValue= "<<m_dResValue<<" Ohms  "<<" m_dTolerance= "<<m_dTolerance<<endl;
           cout<<" Max resistance= "<<m_dMaxResistance<<" Ohms  "<<" Min resistance= "<<m_dMinResistance<<" Ohms "<<endl;};
 void EnterResistance(){
      cout<<"Current resistor is "<<m_dResValue<<" Ohms  "<<endl;

      cout<<"Please enter new resistor value "<<endl;
      cin>>m_dResValue;
      cout<<"not working? "<<m_dResValue<<" Ohms  "<<endl;
         };

private:
 double m_dResValue,m_dTolerance,m_dMinResistance,m_dMaxResistance;
 char m_cResistorName[20];
};

void main()
{


 {  

 CResistor a;
    a.DisplayResistor();
 a.EnterResistance();

 }

       system("PAUSE");
}
A: 

You want to replace the line that says

cin>>m_cResistorName[20];

to read

cin>>m_cResistorName;

The syntax as you originally have it stores a character in the 21st character after the start of m_cResistorName. Then the rest of the string feeds in to the next cin, where you try to read a resistor value.

Brian
The cin extraction stops reading as soon as if finds any blank space character, so in this case we will be able to get just one word for each extraction and this behavior may or may not be desirable.
jschmier
A: 

Try the following to read in the resistor name:

cin.get(m_cResistorName, 20);

The istream::get member function (istream& get (char* s, streamsize n );) extracts characters from the stream and stores them as a c-string into the array beginning at the location pointed to by s. Characters are extracted until either (n - 1) characters have been extracted or the delimiting character '\n' is found. If the delimiting character is found, it is not extracted from the input sequence and remains as the next character to be extracted.

jschmier