Switch Statement

This is a discussion on Switch Statement within the C++ Programming forums, part of the General Programming Boards category; Hey for my program I am using a switch statement to convert string numbers to int numbers. Its in a ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    109

    Switch Statement

    Hey for my program I am using a switch statement to convert string numbers to int numbers. Its in a function called Set, i'm going to post the whole thing. Its not working it prints out random things and I can't find a pattern to tell whats wrong. I also have another function called Print that prints out the numbers stored in the array and that might be the problem area but I am unsure.
    Code:
    Set(string num)
    {
    int i;
    if (num[0] == '-')
    	{
    		positive = false;
    		num.erase(0,1);
    		numDigits = num.size();
    		digits = new int[numDigits];	
    		for(i=0;i<numDigits;i++)
    		{
    			switch (num[i])
    			{
    				case '0':
    					digits[numDigits-i] = 0;
    					break;
    				case '1':
    					digits[numDigits-i] = 1;
    					break;
    				case '2':
    					digits[numDigits-i] = 2;
    					break;
    				case '3':
    					digits[numDigits-i] = 3;
    					break;
    				case '4':
    					digits[numDigits-i] = 4;
    					break;
    				case '5':
    					digits[numDigits-i] = 5;
    					break;
    				case '6':
    					digits[numDigits-i] = 6;
    					break;
    				case '7':
    					digits[numDigits-i] = 7;
    					break;
    				case '8':
    					digits[numDigits-i] = 8;
    					break;
    				case '9':
    					digits[numDigits-i] = 9;
    					break;
    			}
    		}
    	}
    else
    	{
    		positive = true;
    		numDigits = num.size();
    		digits = new int[numDigits];	
    		for(i=numDigits;i>=0;i--)
    		{
    			switch (num[i])
    			{
    				case '0':
    					digits[i] = 0;
    					break;
    				case '1':
    					digits[i] = 1;
    					break;
    				case '2':
    					digits[i] = 2;
    					break;
    				case '3':
    					digits[i] = 3;
    					break;
    				case '4':
    					digits[i] = 4;
    					break;
    				case '5':
    					digits[i] = 5;
    					break;
    				case '6':
    					digits[i] = 6;
    					break;
    				case '7':
    					digits[i] = 7;
    					break;
    				case '8':
    					digits[i] = 8;
    					break;
    				case '9':
    					digits[i] = 9;
    					break;
    			}
    		}
    	}
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    template<class T>
    void StrToNumber(std::string str, T& num)
    {
        std::stringstream ss; // include <sstream>
        ss << str;
        ss >> num;
    }
    
    // usage
    
    int a = 0;
    std::string str = "1234";
    StrToNumber(str, a);
    
    // a == 1234

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    stringstream will do this for you. However, on your note: If the problem is in the printing, then most likely it is the printing function. How did you implement that?
    PS: Your code could be consolidated into one switch for sure.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Suppose we're determined not to use stringstream or atoi.

    Code:
    //assuming ASCII char representation (if that isn't guaranteed)
    //uses <string> and <cmath>
    int get_int(std::string num_str)
    {
      int n = 0;  //the actual int
      unsigned short exp = 0;
      std::string::reverse_iterator iter = num_str.rbegin();
     
      while(iter != num_str.rend())
      {
         if(*iter >= 48 && *iter <= 57)   //if it's coded for ASCII num representation
         {
            n += (*iter - 48) * std::pow(10, exp);  //update n
            ++iter;
            ++exp;
         }
         else if(*iter == '-')   //if it's a minus sign
         {
            if(iter + 1 != num_str.rend()) error();  //negative sign not the first char? Error.
            n *= -1;
            return n;
         }
         else   //if it's anything else (no hex, sci notation -- just plain old int)
         {
            error();
         }
      } //end while loop
      return n;
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> if(*iter >= 48 && *iter <= 57)
    Why use 48 and 57? Just use '0' and '9'. It will work just the same. It will also work for any character set, not just ASCII.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    switch (num[i])
    			{
    				case '0':
    					digits[i] = 0;
    					break;
    				case '1':
    					digits[i] = 1;
    					break;
    				case '2':
    					digits[i] = 2;
    					break;
    				case '3':
    					digits[i] = 3;
    					break;
    				case '4':
    					digits[i] = 4;
    					break;
    				case '5':
    					digits[i] = 5;
    					break;
    				case '6':
    					digits[i] = 6;
    					break;
    				case '7':
    					digits[i] = 7;
    					break;
    				case '8':
    					digits[i] = 8;
    					break;
    				case '9':
    					digits[i] = 9;
    					break;
    			}
    OMG, so much code instead of 1 line:
    digits[i] = num[i] - '0';

    Code:
    std::pow(10, exp);
    Do you know how expensive it is? You are iterating from 0 - use multiplication on every iteration to get the multiplier...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    I'm trying to do this without iterators or vectors, just straight arrays and basic stuff.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The code is not relevantly different.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by DarkDot View Post
    I'm trying to do this without iterators or vectors, just straight arrays and basic stuff.
    Like this:
    Code:
        char myString[] = "-1200";
        signed int number = 0;
        int i;
        int neg = 0;
        
        for (i=0; i < strlen(myString); i++)
        {
            number *=10;
            if (myString[i] == '-' && number == 0)
                neg = !neg;
            else
                if (!neg)
                    number += (myString[i] - '0');
                else
                    number -= (myString[i] - '0');
        }

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    I don't understand the above code. I'm going to post my changed code as well as my print function.
    Code:
    Set(string num)
    {
    int i;
    if (num[0] == '-')
    	{
    		positive = false;
    		num.erase(0,1);
    		numDigits = num.size();
    		digits = new int[numDigits];	
    		for(i=0;i<numDigits;i++)
    		{
    			digits[i] = num[i]-'0';
    		}
    	}
    else
    	{
    		positive = true;
    		numDigits = num.size();
    		digits = new int[numDigits];	
    		for(i=numDigits;i>=0;i--)
    		{
    			digits[i] = num[i]-'0';
    		}
    	}
    }
    
    
    Print()
    {
    int i;
    if(positive == false)
    {
    	cout << "-";
    
    	for(i=1;i<=numDigits;i++)
    	{
    		cout << digits[numDigits-1];
    	}
    }
    else if(positive == true) 
    {
    	for(i=1;i<=numDigits;i++)
    	{
    		cout << digits[numDigits-1];
    	}
    }
    }

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> for(i=numDigits;i>=0;i--)
    Why does this attempt to go backwards when the other code all goes forward? You are starting one place too early here.

    Also, in your Print function you aren't using i at all in the loops, you are just printing the last digit over and over.

  12. #12
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    i think thats my problem it should print - i not -1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutli Switch statement help
    By elwad in forum C Programming
    Replies: 9
    Last Post: 05-09-2009, 03:19 AM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 02:14 PM
  3. switch statement
    By guillermoh in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 02:17 PM
  4. char switch statement
    By jmarsh56 in forum C++ Programming
    Replies: 7
    Last Post: 05-03-2006, 05:04 PM
  5. Efficiency with the switch() statement...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 02:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21