Thread: At My Wit's End (concerns passing char to constructor)

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    17

    At My Wit's End (concerns passing char to constructor)

    PURPOSE OF PROG: user enters an arithmetic problem with two terms. Based upon the rules of significant digits, the program calculates the answer, and returns what the answer should be, adjusting for sig digs.

    (This next part makes more sense having read the code)

    PROBLEM: I input, e.g.: 24.6 + 78.
    The program takes ea. member of the first array of numbers, call the Digit constructor, and passes val to the constructor, which should declare the passed value to be itsVal.
    After getting screwy feedback, I tried putting a cout<< in the constructor call so that it would output what it considered val to be. It output: 50 52 54 , or something similar to that. Sure enough, this corresponds to "2","4", and "6" on the ASCII chart.

    I'm aware that there are probably many more errors to be found in my newbie-esque code.

    Many thanks to any and all who shed even the faintest light on my little problem.

    Code:
    #include <iostream>
    
    enum {ZERO, NONZERO, DECIMAL};
    enum {DEC_DECLARED};
    enum {TRUE, FALSE};
    
    class Digit
    {
     public:
    	Digit(char val);
       ~Digit(void) { }
        void Show(void) const {std::cout<< itsValue << std::endl;}
        int get_isItZero() const {return isItZero;}
    	int get_itsValue() const {return itsValue;}
    
     private:
    	int itsValue;
    	int isItZero;
    };
    
    Digit::Digit(char value)
    {					
     if(value != '.')
     {
      itsValue = value; // how on earth do I cast this correctly?
      std::cout<< ""<< itsValue <<" ";
     }
     else
      itsValue = DECIMAL;
    
     if(itsValue==0)
     {
      isItZero = ZERO;
     }
     else
      isItZero = NONZERO;
    }
    
     // I excised all of the linked list stuff.  I determined that the issue
     // was arising before it was even inserted into the LL.
    
    int main(void)
    {
     char numA[50];
     char numB[50];
     char oper;
    
     std::cin>> numA >> oper >> numB;
    
     Digit * p_Digit;
     LinkedList ll;
    
     for(short iA=0; iA<50; iA++)
     {
      // regarding this clumsy switch(case) -
      // any suggestions as to making this less klunky?
    
      switch(numA[iA])
      {
       case '0':
    	  p_Digit = new Digit(numA[iA]);
    	  ll.Insert(p_Digit);
    	  break;
       case '1':
    	  p_Digit = new Digit(numA[iA]);
    	  ll.Insert(p_Digit);
    	  break; 
       case '2':
    	  p_Digit = new Digit(numA[iA]);
    	  ll.Insert(p_Digit); 
    	  break;
       case '3':
          p_Digit = new Digit(numA[iA]);
    	  ll.Insert(p_Digit);
    	  break;
       case '4':
          p_Digit = new Digit(numA[iA]);
    	  ll.Insert(p_Digit);
    	  break;
       case '5':
          p_Digit = new Digit(numA[iA]);
    	  ll.Insert(p_Digit);
    	  break;
       case '6':
          p_Digit = new Digit(numA[iA]);
    	  ll.Insert(p_Digit); 
    	  break;
       case '7':
          p_Digit = new Digit(numA[iA]);
    	  ll.Insert(p_Digit); 
    	  break;
       case '8':
          p_Digit = new Digit(numA[iA]);
    	  ll.Insert(p_Digit); 
    	  break;
       case '9':
          p_Digit = new Digit(numA[iA]);
    	  ll.Insert(p_Digit);
          break;
       case '.':
          p_Digit = new Digit(numA[iA]);
    	  ll.Insert(p_Digit);
    	  break;
       default:
    	  break;	  		
      }
     }
    
     return 0;
    }
    "None are more hopelessly enslaved than those who falsely believe they are free."
    -Goethe

    [paraphrased] "Those who would sacrifice essential liberties for a little temporary safety deserve neither."
    -Benjamin Franklin

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    value is the character '2' or 50. You want 2. So try:
    Code:
    itsValue = value - 48;  // or
    itsValue = value - '0';
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  2. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM