7 Replies - 322 Views - Last Post: 13 May 2013 - 09:04 AM Rate Topic: -----

#1 Criel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 11-March 11

Cut a string in VC++

Posted 13 May 2013 - 08:08 AM

Hi All,

I'm extremely new to visual c++ and I have a calculation assigned to a data type long that I copied to a string in hopes of cutting the first index if the value gets more than 6 digits long.

The max the value can get is 7 digits long so if that ever occurs, I would like to cut the first number of the string
Example: 1532939 -> 532939

I've been looking all around and I cannot seem to cut a string. Is there any way to cut a string based on the index like in vb?

(_ltoa(total,total2, 10));
	
	if (strlen(total2) > 6)
	{
		
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Cut a string in VC++

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9277
  • View blog
  • Posts: 34,788
  • Joined: 12-June 08

Re: Cut a string in VC++

Posted 13 May 2013 - 08:22 AM

So.. like substring?
Was This Post Helpful? 0
  • +
  • -

#3 Zereo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 108
  • Joined: 08-November 12

Re: Cut a string in VC++

Posted 13 May 2013 - 08:32 AM

Here is a example of substr like modi suggested.
#include <string>
#include <iostream>

using namespace std;

int main()
{
    string name;

    cout << "Enter your name: ";
    cin >> name;

    // If the word is 7 character or more cut the word in half
    // and keep the first half of the word
    if (name.length() >= 7)
        name = name.substr(0, name.length() / 2);

    cout << name << endl;
}


Hopefully that helps give you some ideas on how you can use .substr to complete your problem. If you have any other questions let us know and we would be more then happy to help out.

This post has been edited by Zereo: 13 May 2013 - 08:32 AM

Was This Post Helpful? 0
  • +
  • -

#4 Criel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 11-March 11

Re: Cut a string in VC++

Posted 13 May 2013 - 08:33 AM

Hi Modi,

I've been experiencing a similar error when I try to do any substr and I don't quite understand it. What exactly does this mean?

error C2228: left of '.substr' must have class/struct/union
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,682
  • Joined: 25-December 09

Re: Cut a string in VC++

Posted 13 May 2013 - 08:37 AM

Are you trying to write a C or C++ program?

Please show a small complete program that illustrates your problem.


Jim
Was This Post Helpful? 0
  • +
  • -

#6 Zereo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 108
  • Joined: 08-November 12

Re: Cut a string in VC++

Posted 13 May 2013 - 08:39 AM

That means you must have a string object to use substr().

For example

string word;

// Place the correct arguements that you want
// instead of 0 and 2
word.substr(0, 2);


Notice how I didn't call the function just by itself like a normal function. I called it from my string object. This is because the substr() function is a member function of the string class.

We are saying that we want to create a substring of the variable word. So

string newWord = word.substr(0, 2);

will create the string variable newWord from a substring of the variable word.

This post has been edited by Zereo: 13 May 2013 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

#7 Criel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 11-March 11

Re: Cut a string in VC++

Posted 13 May 2013 - 08:59 AM

Apologies for not understanding but isn't there only the data type of char/signed char/unsigned char in visual c++? I didn't think there was a string data type.

The data type for total2 is char

This post has been edited by Criel: 13 May 2013 - 09:01 AM

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,682
  • Joined: 25-December 09

Re: Cut a string in VC++

Posted 13 May 2013 - 09:04 AM

Are you trying to write a C or a C++ program? A C++ program would have the std::string class available, provided you're not using some ancient version of your Microsoft compiler. C doesn't have the std::string class.

So please show a small complete program that illustrates your problem, then maybe we can stop guessing and possibly help answer your question.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1