| | |
VB's Left, Right, Mid Functions in C++?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2003
Posts: 6
Reputation:
Solved Threads: 0
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
#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
•
•
Join Date: Sep 2003
Posts: 22
Reputation:
Solved Threads: 0
Left could be done by going:
Right could be done like this:
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:
Does this help?
C++ Syntax (Toggle Plain Text)
char left(char *string) { return string[0]; }
Right could be done like this:
C++ Syntax (Toggle Plain Text)
char right(char *string) { return string[strlen(string)]; }
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)
char mid(char *string) { return string[strlen(string)>>1); }
Does this help?
•
•
Join Date: Sep 2003
Posts: 81
Reputation:
Solved Threads: 0
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
IIya
•
•
•
•
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'
•
•
Join Date: Sep 2003
Posts: 22
Reputation:
Solved Threads: 0
•
•
•
•
none of the 2 overloads can convert parameter 1 from type 'std::string'
To get it to work, you have to do something like this:
C++ Syntax (Toggle Plain Text)
char *string = "Hello. I am a string"; cout << right(string);
If there actually is a 'right()' function in there somewhere, I believe this will work:
::right(string);
•
•
Join Date: Sep 2003
Posts: 81
Reputation:
Solved Threads: 0
I think he was using strings all along..
•
•
•
•
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)
char *string = "Hello. I am a string"; cout << right(string);
If there actually is a 'right()' function in there somewhere, I believe this will work:
::right(string);
•
•
Join Date: Dec 2003
Posts: 1
Reputation:
Solved Threads: 0
u lot talking about left and right ok BUT i can't get my head around this,
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..
can anyway explain this plz?
C++ Syntax (Toggle Plain Text)
1 For x = 1 to Len(txtNI.Text) 2 If Asc(Mid(txtNi,x,1) < 65 or _ 3 Asc(Mid(txtNi,x,1) > 90 Then .....
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)
... (Mid(txtNi,x,1) < 65 ...
can anyway explain this plz?
•
•
Join Date: Dec 2003
Posts: 55
Reputation:
Solved Threads: 6
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <stdlib.h> #include <string> #define ONE " via c++ string at() (worx only on 1 char): " #define MORE " via c++ string substr() (worx on more chars): " using namespace std; int main(void) { string test; do { cout << "Enter the teststring (6 chars minimum): "; cin >> test; } while (test.size() < 6); /* u could actually use more chars with substr() method than just one * for example u could replace the test.substr(0, 1) with test.substr(0, 2) * to get 2 chars */ cout << "\n\tleft()" << MORE << test.substr(0, 1) << "\n"; cout << "\tleft()" << ONE << test.at(0) << "\n\n"; cout << "\tright()" << MORE << test.substr(test.size() - 1, 1) << "\n"; cout << "\tright()" << ONE << test.at(test.size() - 1) << "\n\n"; /* here i extracted the 5th char, remember counting in c / c++ begins at 0, not at 1 */ cout << "\tmid()" << MORE << test.substr(4, 1) << "\n"; cout << "\tmid()" << ONE << test.at(4) << "\n\n"; system("PAUSE"); return 0; }
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:
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: 1 cin statement/multiple values to a vector
- Next Thread: circular linked lists
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets




