|
-
Jun 3rd, 2004, 06:52 PM
#1
Thread Starter
Member
Strings
Ok, I started coding in VB... Now that im switching to C++ i cant figure out how to work strings... How do i store something in a string? how do i declare a string?
In the book im learning from it talks about comparing strings, but hasnt showed how to actauly make a string. Any help would be appriciated.
---Flac
Everything great once started with the words "That will never catch on, you shouldnt even bother" be great, go against the crowed, do something stupid, you might become famous.
-
Jun 3rd, 2004, 08:30 PM
#2
Hyperactive Member
Originally posted by Flac
Ok, I started coding in VB... Now that im switching to C++ i cant figure out how to work strings... How do i store something in a string? how do i declare a string?
In the book im learning from it talks about comparing strings, but hasnt showed how to actauly make a string. Any help would be appriciated.
---Flac
There are two general types of strings you can work with in C++: char arrays and strings from the standard library (which are very similar to strings in vb).
A char array is just an array of characters. They generally have to be a predefined size and you can't compare them the way you would compare two strings in VB. Here's some code that shows how you would create and initialize a char array:
Code:
//to declare a char array
char mycstring[4];
//to initialize a char array
mycstring = "bob";
Keep in mind that when you declare a char array, you must leave extra space at the end for the terminating character ('\0'). If you don't, you'll get errors.
Strings from the standard library are very similar to the strings your used to working with in vb. They can do most of the same things as the ones in vb and then some. Here's how you declare one:
Code:
#include <string> //need this to get access to std strings
string myString = "whatever";
Search the forum or read up on how these strings work at MS's MSDN library:
http://msdn.microsoft.com/library/default.asp
Hope that clears some things up. Good luck.
C.O.M.R.E.A.K.: Cybernetic Obedient Machine Responsible for Exploration and Accurate Killing
-
Jun 3rd, 2004, 10:31 PM
#3
Thread Starter
Member
dont mean to continue to pester.
but is there a way i can take input from a user into a string directly? or do i have to convert it somehow?
(is there a way i can do a std::cin >> string type statement to get a string, I prefer working with strings cause they are simpler to compare values to.)
Everything great once started with the words "That will never catch on, you shouldnt even bother" be great, go against the crowed, do something stupid, you might become famous.
-
Jun 3rd, 2004, 10:35 PM
#4
sure, the string class has a stream extraction operator;
Code:
#include <string>
#include <iostream>
using namespace std;
// ...
string sInput;
cin >> sInput; // read one word;
cout << "you wrote the word: " << sInput << endl;
getline(cin, sInput); // read a line
cout << "you wrote the line: " << sInput << endl;
// ...
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Jun 3rd, 2004, 10:47 PM
#5
Thread Starter
Member
Thanks a lot... But now i have the problem that the getline(cin, variable) code doesnt read the first word in the string... Any suggestions?
---Flac
Everything great once started with the words "That will never catch on, you shouldnt even bother" be great, go against the crowed, do something stupid, you might become famous.
-
Jun 4th, 2004, 03:21 AM
#6
Have you copied the example literally?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 4th, 2004, 10:12 AM
#7
Fanatic Member
Remember that the line is terminated when you hit <Enter>
"Can't" and "shouldn't" are two totally separate things.
All questions should be answered. All answers should be true. That is why I post.
-
Jun 4th, 2004, 09:28 PM
#8
Thread Starter
Member
If writin my own code using your example for basis, and copied example literaly(adding in all the stuff needed to make it compile cleanly.)
Cod entered(not including return, or includes)
Code:
int main()
{
std::string sInput;
std::cin >> sInput; // read one word;
std::cout << "you wrote the word: " << sInput << std::endl;
getline(std::cin, sInput); // read a line
std::cout << "you wrote the line: " << sInput << std::endl;
when a string is entered, for example : Hi there.
I get
You wrote the word: hi
You wrote the line: there
if i enter "hi there bob" i get
You wrote the word: hi
You wrote the line: there bob
Its cutting out the first word of the string. Any suggestions?
---Flac
Everything great once started with the words "That will never catch on, you shouldnt even bother" be great, go against the crowed, do something stupid, you might become famous.
-
Jun 5th, 2004, 01:19 AM
#9
That's because the first line:
reads the first word that you input and leaves the left in the stream to be read later.
try putting in just one word for the first case; then it will prompt you to enter more in the second case.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Jun 5th, 2004, 10:00 AM
#10
Thread Starter
Member
I understand the first part getting one word. But is there a way i can make
getline(std::cin, sInput); get the full inputed line, including the first word?
As is its taking everything but the first word, why is that?
---Flac
Everything great once started with the words "That will never catch on, you shouldnt even bother" be great, go against the crowed, do something stupid, you might become famous.
-
Jun 5th, 2004, 10:10 AM
#11
Because once the first part takes the first word, it's no longer there for the second part to take.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 7th, 2004, 11:27 AM
#12
Fanatic Member
If you comment out the line:
cin >> sInput;
it should work to your liking.
"Can't" and "shouldn't" are two totally separate things.
All questions should be answered. All answers should be true. That is why I post.
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
|