String manipulation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • The Midnighter
    New Member
    • Feb 2007
    • 15

    #1

    String manipulation

    Alright, so I'm trying to write a fraction calculator for the hell of it, here's my problem:

    I want the user to enter in two fractions, lets say 64/100 and 55/100
    I want then to enter one fraction at a time, in exactly that format. How can I manipulate that string, so I can get the numbers separated by the '/' ?

    I've been messing around with strings for a while now, but I can't seen to figure out how to separate it.. Thanks for the tips!

    Edit: Okay, I've got some code written to find the /, now I just need to find out how to grab the numbers on either side of it...

    Code:
    // Written by Michael C. Dillon
    #include <iostream>
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    using std::string;
    
    int main ()
    {
    	string first;
    	char second[50];
    	unsigned int value; 
    	size_t found;
    
    	while (true)
    	{
    	cout << "Enter your first fraction: ";
    		getline(cin,first,'\n');
    		string::size_type found = first.find("/", 0 );
    			if (found!=string::npos)
    				cout << "/ found at " << int(found) + 1 << endl;
    	}
    
    		system("PAUSE");
    		return EXIT_SUCCESS;
    }
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Check out string::substr.

    As a side note, why are you explicitly saying "using std::string", since you already say "using namespace std"?

    Comment

    • The Midnighter
      New Member
      • Feb 2007
      • 15

      #3
      I'd delete this if I could!

      Comment

      • The Midnighter
        New Member
        • Feb 2007
        • 15

        #4
        Originally posted by Ganon11
        Check out string::substr.

        As a side note, why are you explicitly saying "using std::string", since you already say "using namespace std"?

        I lied, it wasn't quite what I needed xD
        When I use that substr, its also copying the '/' into part of the variable, which is going to cause problems..
        [CODE=CPP]
        // Written by Michael C. Dillon
        #include <iostream>
        #include <string>
        #include <cstdlib>

        using namespace std;
        using std::string;

        int main ()
        {
        string first,str,str1;
        char second[50];
        unsigned int value;
        size_t found;
        size_t pos;

        while (true)
        {
        cout << "Enter your first fraction (0 to exit): ";
        getline(cin,fir st,'\n');
        if (first=="0")
        {
        break;
        }
        else
        {
        pos = first.find('/');
        str = first.substr(po s);
        string::size_ty pe found = first.find("/" );
        if (found!=string: :npos)
        cout << "/ found at " << int(found) + 1 << "str = " << str;
        }
        }

        system("PAUSE") ;
        return EXIT_SUCCESS;
        } [/CODE]


        //// Edit

        That was actually the easiest fix I've ever seen. I just added a +1 to (pos)
        Thank god!

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          You might consider using a stringstream:
          [code=cpp]
          string str("54/100");
          int numerator;
          int denominator;
          char trash;
          stringstream ss;
          ss << str;
          ss >> numerator >>trash >> denominator;
          cout << numerator << endl <<denominator << endl;
          [/code]

          Comment

          • The Midnighter
            New Member
            • Feb 2007
            • 15

            #6
            Originally posted by weaknessforcats
            You might consider using a stringstream:
            [code=cpp]
            string str("54/100");
            int numerator;
            int denominator;
            char trash;
            stringstream ss;
            ss << str;
            ss >> numerator >>trash >> denominator;
            cout << numerator << endl <<denominator << endl;
            [/code]

            ... Didn't understand your code at all mate. Also didn't compile.

            Comment

            • The Midnighter
              New Member
              • Feb 2007
              • 15

              #7
              Alright, so I've come to the conclusion that substr can find the variable for me after the /, but what about before the slash?

              I was thinking, if I could count how many spaces there are from the beginning of the line, until the '/' - then I could do a string::find to shove that into a variable..
              Here's my current code:
              [CODE=cpp]// Written by Michael C. Dillon
              #include <iostream>
              #include <string>
              #include <cstdlib>

              using namespace std;
              using std::string;

              int main ()
              {
              string first,str,str1;
              size_t pos;

              while (true)
              {
              cout << "Enter your first fraction (0 to exit): ";
              getline(cin,fir st,'\n');
              if (first=="0")
              {
              break;
              }
              else
              {
              pos = first.find('/');
              str = first.substr(po s+1);
              cout << "pos = " << pos+1 << " str = " << str << ".";
              //string::size_ty pe found = first.find("/" );
              // if (found!=string: :npos)
              // cout << "/ found at " << int(found) + 1 << "str = " << str;
              }
              }

              system("PAUSE") ;
              return EXIT_SUCCESS;
              } [/CODE]

              Ignore that commented out crap, I'm pretty sure its useless.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by TheMidNighter
                Didn't understand your code at all mate. Also didn't compile.
                It most certainly does because I compiled an ran it before posting.

                Probably you did not include the correct headers: <string> <sstream>.

                As yo your understanding, I assume you know cin >> and cout <<. Yes?

                Well, a stringstream is a variable that on the one hand acts like a stream so you can use the << and >> operators and on the other hand acts like a string.

                This is called in-core formatting. A little Google should clear this up.

                Hand-parsing a string is the hard way to go.

                Comment

                Working...