• Advertisement

Archived

This topic is now archived and is closed to further replies.

brain dead here on strings

This topic is 5066 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I would like to know how to store the entie string in a pointer in c++. here is the code I have, useing char only stores one letter, and I cant get the string prefix to work. #include <fstream.h> #include <string.h> #include <iostream.h> //using namespace std; (this will not work with visual studio c++7) int main() { // this is only storeing 1 char, what would I replace char with to get // the entire string char str1; //this just stops the program from ending till a key is pressed char i; char *str= &str1; //imput string cin >> str1; //print string to screen cout << str1; // just used to stop the app from ending cin >> i; return 0; the code is simple, but I have always had bad problems with strings. Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
I think your getting mixed up with a pointer that points to a object/instance with a pointer that points to a particular element of an array instance.

Also your using deprecated headers not recommended, for standard C++ libs remove the ".h" and for standard C headers remove the ".h" and append a ''c'' on the front like:
"#include <cmath>"

Anyways there are two ways to store strings, C style using static/dynamic character arrays, or the C++ way using the standard library string type which is what you should be using instead.

C style:

#include <cstddef>
#include <iostream>

int main() {

//static way

static const ::size_t size = 50;
char static_buf[size];

std::cout << "Enter up to some stuff:\n";

std::cin.get(static_buf, size);

std::cout << "50 chars you entered where:" << static_buf << ''\n'';

//dynamic way

::size_t size2 = 0;
std::cout << "Please enter length of the string: ";
std::cin >> size2;

std::cout << "\nNow enter your string:\n";

char* dynamic_buf = new char[size2];
std::cin.get(dynamic_buf, size2);

std::cout << "\nYou entered: " << dynamic_buf << std::endl;

if(dynamic_buf != 0)
delete[] dynamic_buf;

return 0;
}


C++ way

#include <string>
#include <iostream>
int main() {

std::string my_string;

std::cout << "Enter a string of any length:\n";
std::cin >> my_string;

std::cout << "U entered: " << my_string;

return 0;
}

Share this post


Link to post
Share on other sites
thanks that explains alot. I think its the header file [.h] were is the fault. Thanks for your help

Share this post


Link to post
Share on other sites
Famine3d, try this:

#include <string>
#include <iostream>

using namespace std;

int main() {
string str1;
cin >> str1;
cout << str1;
system("pause");
return 0;
}

Edit: oops, looks like i was a bit late

[edited by - VStrider on June 8, 2004 7:32:24 AM]

Share this post


Link to post
Share on other sites

  • Advertisement