Back | Reverse | Quick Reply | Post Reply |

Strings and C++
Link | by psoplayer on 2006-09-19 11:20:38 (edited 2006-09-20 09:26:29)
I'm teaching myself C++ and I usually get started by playing with strings. I need to convert a string of numbers into an integer, so I wrote this, which hasn't worked. Any insight? Or maybe I'm just ignorant of a function in the string class that I could use for this.
#include <iostream>
#include <string>

using namespace std;

int value10(string j)
{
    int a,b,c,d,len;
    len = j.length();
    c = 0;
    for (int i = len - 1; i < 0; --i) {
        d = j.at(i);
        switch(d)
        {
            case '0':
            a = 0;
            break;
            case '1':
            a = 1;
            break;
            case '2':
            a = 2;
            break;
            case '3':
            a = 3;
            break;
            case '4':
            a = 4;
            break;
            case '5':
            a = 5;
            break;
            case '6':
            a = 6;
            break;
            case '7':
            a = 7;
            break;
            case '8':
            a = 8;
            break;
            case '9':
            a = 9;
            break;
            default:
            return 0;
        }
        b = b + a * 10 ^ c;
        c++;
    }
    return b;
}

int main()
{
    string input;
    cin >> input;
    int len,v;
    len = input.length() - 1;
    v = value10(input);
	cout << input.at(len) << endl << v;
	return 0;
}



Re: Strings and C++
Link | by gendou on 2006-09-19 13:31:37 (edited 2006-09-19 13:42:49)
there are a few things that i noticed. first and foremost, you should choose more descriptive variable names!
that aside, you forgot to initialize your cumulating variable (i call it "sum", you called it "b").
another bug i found was that you are using the symbol ^, which does NOT stand for "X raised to the Y power" like in mathematics.
The ^ symbol performs a "Bitwise exclusive OR". :P
In fact, there is no need to keep track of the place value at all.
Instead, read from left to right (like a human does).
Each time you read a new number, just multiply the old sum by 10 before adding the new digit.
This has the added benefit of allowing your function to (partially) parse strings like "100km", "20.0" and "132lbs" without failing.
Here it is all cleaned up:
#include <iostream>
#include <string>

using namespace std;

int value10(string str)
{
    int sum = 0;
    int digit;

    for(int i = 0; i < str.length(); i++)
    {
        switch(str[i])
        {
            case '0': digit = 0; break;
            case '1': digit = 1; break;
            case '2': digit = 2; break;
            case '3': digit = 3; break;
            case '4': digit = 4; break;
            case '5': digit = 5; break;
            case '6': digit = 6; break;
            case '7': digit = 7; break;
            case '8': digit = 8; break;
            case '9': digit = 9; break;
            default: return sum;
        }
        sum = (sum * 10) + digit;
    }
    return sum;
}

int main()
{
    string input;
    cin >> input;
    cout << value10(input) << endl;
    return 0;
}



Re: Strings and C++
Link | by Sheep on 2006-09-20 11:11:47 (edited 2006-09-20 11:12:55)
And here is yet another solution from the less-code-for-the-same-buck-departement.
We go ascii-value style on it!

#include 
#include 

using namespace std;

int value10(string str)
{
  int sum = 0;

  /* 0 in string has ascii value 48, 1 has 49, etc... */
  for(int i = 0; i < str.length(); i++)
    if (int(str[i]) > 47 && int(str[i]) < 58)
      sum = (sum * 10) + int(str[i]) - 48;
    else
      break;

  return sum;
}

int main()
{
  string input;
  cin >> input;
  cout << value10(input) << endl;
  return 0;
}


[Quiet dogs know when to be silent]
Sheep & Tiojar's Anime Episodes

Re: Strings and C++
Link | by gendou on 2006-09-20 11:22:15
yea, i happen to like that solution better, myself.
manipulating ascii values is l33t!


Re: Strings and C++
Link | by psoplayer on 2006-09-24 10:28:21
The idea had crossed my mind, but I don't know ASCII off the top of my head and definitely didn't know how to get at the ASCII codes using C.


Re: Strings and C++
Link | by Crystal on 2006-10-17 20:22:51
It's looks like sheep is using int(str) to convert str to asc...
I wonder what is the function to convert to str...is it char(int)..???

Re: Strings and C++
Link | by on 2006-11-02 19:20:55
Have you tried using the built-in atoi function? I use the Turbo C++ compiler (DOS, I know... YUCK!), so I'm not quite sure if your compiler has atoi... but it is rather handy! :D


Back | Reverse | Quick Reply | Post Reply |

Copyright 2000-2024 Gendou | Terms of Use | Page loaded in 0.0034 seconds at 2024-05-07 06:40:11