-
May 7th, 2009, 04:11 PM
#1
New to C++ but familiar with Programming
Ok im trying to figure out how to hold a string variable, how do i declare a variable as the string type.
like this example in PHP:
$myString = "This is my string";
regards thank you
-
May 7th, 2009, 04:20 PM
#2
Re: New to C++ but familiar with Programming
Code:
#include <string>
int main()
{
std::string myString = "This is my string";
}
std::string info.
Last edited by STLDude; May 7th, 2009 at 04:22 PM.
Reason: Added link
-
May 7th, 2009, 06:34 PM
#3
Re: New to C++ but familiar with Programming
Just "plain" C++ doesn't have a string type. String management is the same as in C, using arrays of char's (or widechars for unicode).
The Standard Template Library which comes with most recent C++ compilers does have one, as STLDude mentioned.
Most other libraries offer their own version of string classes. MFC/ATL has CString.
At the low level. A string in most C++ libraries ends up being a pointer to a character array and ending with a zero terminator. The memory this pointer points to is usually allocated and disposed of via new[]/delete[].
-
May 7th, 2009, 07:44 PM
#4
Re: New to C++ but familiar with Programming
"plain" C++ doesn't have a string type.
It depends on what you mean by "plain" C++. If by "plain", you mean just C++ syntax, then there is no "string" type.
However, the language library of C++ (and C) are considered parts of the language. "Plain" C++ could mean what is defined by ANSI, which includes syntax and libraries. If this is what it means to be "plain", then C++ does have a string type (std::string).
Regards,
Paul McKenzie
Last edited by Paul McKenzie; May 7th, 2009 at 07:50 PM.
-
May 8th, 2009, 05:23 PM
#5
Re: New to C++ but familiar with Programming
thank you guys for the help.
im using visual C++ from Microsoft's site. and im making a form app. isnt the "std::string" only used in console apps?
thankyou
-
May 8th, 2009, 06:05 PM
#6
Re: New to C++ but familiar with Programming
isnt the "std::string" only used in console apps?
No.
So, what would you use in GUI/Form app if you needed to deal with strings? (20 points for answering).
-
May 8th, 2009, 08:00 PM
#7
Re: New to C++ but familiar with Programming
say i wanted to hold the path to a file in the string. i want to use the string in a few places and not have to rewrite it.
but simply for usability and ease.
-
May 8th, 2009, 08:12 PM
#8
Re: New to C++ but familiar with Programming
Originally Posted by sacarias40
say i wanted to hold the path to a file in the string. i want to use the string in a few places and not have to rewrite it.
but simply for usability and ease.
Let's change some words:
"say I wanted to hold the student average in the double, I want to use the double in a few places and not have to rewrite it."
Now what is the difference between the above, and what you stated? What difference does it make if the variable type is an int, double, char, or string? None. A string is just another data type. Except for a couple of cases, it doesn't need to be treated differently than any other built-in type.
Regards,
Paul McKenzie
-
May 8th, 2009, 08:19 PM
#9
Re: New to C++ but familiar with Programming
Well im not exactly sure on this but i suppose you cannot do math with a string only with an int or double or something of that sort.
if im right, dont doubles and ints only hold numbers. please correct me if im wrong.
-
May 8th, 2009, 08:39 PM
#10
Re: New to C++ but familiar with Programming
Originally Posted by sacarias40
Well im not exactly sure on this but i suppose you cannot do math with a string only with an int or double or something of that sort.
if im right, dont doubles and ints only hold numbers. please correct me if im wrong.
You're missing the point.
A string is nothing but another data type -- a type that can hold "strings". Most languages have string types, numeric types, boolean types, etc. C++ is no different than those languages.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; May 8th, 2009 at 08:43 PM.
-
May 8th, 2009, 09:01 PM
#11
Re: New to C++ but familiar with Programming
i understand this but i just need to have the equivalent to this:
var filePath = "C:\users\name\desktop";
C++ style
or this vb style:
dim filePath as string = "C:\users\name\desktop"
-
May 8th, 2009, 09:33 PM
#12
Re: New to C++ but familiar with Programming
Did you bother to read any of the example code in the reference link above?
-
May 8th, 2009, 09:37 PM
#13
Re: New to C++ but familiar with Programming
yes, and the above is confusing. they are telling me two things. std::string and char str[]
but i dont know what will work for me. just some help is all im looking for. lol
-
May 8th, 2009, 11:54 PM
#14
Re: New to C++ but familiar with Programming
So, is this example (listed in my previous reply also) was not enough to show how to initialize a string?
Code:
#include <string>
int main()
{
std::string myString = "This is my string";
}
-
May 9th, 2009, 05:22 AM
#15
Re: New to C++ but familiar with Programming
Go play around with this.
Code:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string Path = "C:\\users\\name\\desktop";
cout << Path << endl;
cin.get();
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|