943,312 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 58516
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 15th, 2003
0

VB's Left, Right, Mid Functions in C++?

Expand Post »
I apologize up front; I'm a VB Programmer trying to pick up C++. I have a very simple program that takes a string input from the user. How can I look at a specific character in that string? VB has the Left(), Right(), and Mid() Functions. Is there something similar for C++? A sample of my code is below. In this example, how could I have the program return the "T" in "Test String" or the "g" at the end?

#include "stdafx.h"
#include <iostream>
#include <string>

#using <mscorlib.dll>

using namespace System;
using namespace std;

int _tmain()
{
string strTest;
strTest = "Test String";
cout << strTest;

return 0;
}

Joe Cody
Allied Tube & Conduit
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Daywalker46410 is offline Offline
6 posts
since Sep 2003
Sep 15th, 2003
0

Re: VB's Left, Right, Mid Functions in C++?

Left could be done by going:

C++ Syntax (Toggle Plain Text)
  1.  
  2. char left(char *string) {
  3. return string[0];
  4. }

Right could be done like this:
C++ Syntax (Toggle Plain Text)
  1. char right(char *string) {
  2. return string[strlen(string)];
  3. }

I'm no VB programmer, since it's my personal opinion that VB is for idiots, so I don't know what mid() does. If it gets the middle character:

C++ Syntax (Toggle Plain Text)
  1. char mid(char *string) {
  2. return string[strlen(string)>>1);
  3. }

Does this help?
Reputation Points: 12
Solved Threads: 0
Newbie Poster
Mike29936 is offline Offline
22 posts
since Sep 2003
Sep 16th, 2003
0

Re: Re: VB's Left, Right, Mid Functions in C++?

Quote originally posted by Mike29936 ...
I'm no VB programmer, since it's my personal opinion that VB is for idiots, so I don't know what mid() does.
Well this isn't very nice, is it? (although.. i prefer C/C++ myself)
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
Valmian is offline Offline
82 posts
since Sep 2003
Sep 16th, 2003
0
Re: VB's Left, Right, Mid Functions in C++?
I'm assuming I create these as functions, but I get an error when calling them.

error C2665: 'std::right' : none of the 2 overloads can convert parameter 1 from type 'std::string'
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Daywalker46410 is offline Offline
6 posts
since Sep 2003
Sep 16th, 2003
0

Re: VB's Left, Right, Mid Functions in C++?

Did you declear the functions? May be there are these functions in reality since std::right would seem as if it thinks it is one of the library functions.. rename them to something like rightc() or smthn..
IIya
Quote originally posted by Daywalker46410 ...
I'm assuming I create these as functions, but I get an error when calling them.

error C2665: 'std::right' : none of the 2 overloads can convert parameter 1 from type 'std::string'
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
Valmian is offline Offline
82 posts
since Sep 2003
Sep 16th, 2003
0

Re: VB's Left, Right, Mid Functions in C++?

Quote ...
none of the 2 overloads can convert parameter 1 from type 'std::string'
It does strings, as in the character arrays. It doesn't do objects.

To get it to work, you have to do something like this:

C++ Syntax (Toggle Plain Text)
  1. char *string = "Hello. I am a string";
  2. cout << right(string);

If there actually is a 'right()' function in there somewhere, I believe this will work:

::right(string);
Reputation Points: 12
Solved Threads: 0
Newbie Poster
Mike29936 is offline Offline
22 posts
since Sep 2003
Sep 16th, 2003
0

Re: Re: VB's Left, Right, Mid Functions in C++?

I think he was using strings all along..
Quote originally posted by Mike29936 ...
It does strings, as in the character arrays. It doesn't do objects.

To get it to work, you have to do something like this:

C++ Syntax (Toggle Plain Text)
  1. char *string = "Hello. I am a string";
  2. cout << right(string);

If there actually is a 'right()' function in there somewhere, I believe this will work:

::right(string);
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
Valmian is offline Offline
82 posts
since Sep 2003
Dec 1st, 2003
0

Re: VB's Left, Right, Mid Functions in C++?

u lot talking about left and right ok BUT i can't get my head around this,

C++ Syntax (Toggle Plain Text)
  1. 1 For x = 1 to Len(txtNI.Text)
  2. 2 If Asc(Mid(txtNi,x,1) < 65 or _
  3. 3 Asc(Mid(txtNi,x,1) > 90 Then
  4. .....

I know this, on the first line of the code, Len function is looping and looking at each character in a string that has been entered in txtNi. Then on the second line Asc checks for Ascii characters all fine till here, i don't understand this part..

C++ Syntax (Toggle Plain Text)
  1. ...
  2. (Mid(txtNi,x,1) < 65
  3. ...

can anyway explain this plz?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
simprix is offline Offline
1 posts
since Dec 2003
Dec 4th, 2003
0

Re: VB's Left, Right, Mid Functions in C++?

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string>
  4.  
  5. #define ONE " via c++ string at() (worx only on 1 char): "
  6. #define MORE " via c++ string substr() (worx on more chars): "
  7.  
  8. using namespace std;
  9.  
  10. int main(void)
  11. {
  12. string test;
  13.  
  14. do
  15. {
  16. cout << "Enter the teststring (6 chars minimum): ";
  17. cin >> test;
  18. }
  19. while (test.size() < 6);
  20.  
  21. /* u could actually use more chars with substr() method than just one
  22.   * for example u could replace the test.substr(0, 1) with test.substr(0, 2)
  23.   * to get 2 chars
  24.   */
  25. cout << "\n\tleft()" << MORE << test.substr(0, 1) << "\n";
  26. cout << "\tleft()" << ONE << test.at(0) << "\n\n";
  27.  
  28. cout << "\tright()" << MORE << test.substr(test.size() - 1, 1) << "\n";
  29. cout << "\tright()" << ONE << test.at(test.size() - 1) << "\n\n";
  30.  
  31. /* here i extracted the 5th char, remember counting in c / c++ begins at 0, not at 1 */
  32. cout << "\tmid()" << MORE << test.substr(4, 1) << "\n";
  33. cout << "\tmid()" << ONE << test.at(4) << "\n\n";
  34.  
  35. system("PAUSE");
  36. return 0;
  37. }

this is actually some source who should illustrate you the use of the c++ string class (which is very handy) in order to simulate the vb functions u asked for, so u dont need your own functions for it, since all is already in c++ :cheesy:
Reputation Points: 13
Solved Threads: 6
Junior Poster in Training
r0ckbaer is offline Offline
55 posts
since Dec 2003
May 1st, 2005
0

Re: VB's Left, Right, Mid Functions in C++?

memcpy(out,in,len); is alot faster and more efficient seeing as you're using pointers anyway
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dotslashsatan is offline Offline
2 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: 1 cin statement/multiple values to a vector
Next Thread in C++ Forum Timeline: Cube Root Calculator





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC