...

View Full Version : C++ Strings



Active X
08-21-2007, 09:52 AM
Hi guys ,

I have this test code :

#include <iostream>
#include <string>

int main()
{
char* data = "";
std::cin >> data;
std::cout << strlen(data);
}

Please test it on your compiler , What's problem of this code ?
I just want a code to get a string and show length of that string .

And oh ! , Need a good web site as C++ reference

Will help instead very much .

sage45
08-21-2007, 04:44 PM
#include <iostream>
#include <string>

int main()
{
char* data = "";
std::cin >> data;
std::cout << strlen(data);
}I'm not in front of a compiler right now, but it would be easier to do this:
// string::length
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string strLngth="";
cin >> strLngth;
cout << "The length of strLngth is " << strLngth.length() << " characters.\n";
return 0;
}One good reference is: http://www.cplusplus.com/

-saige-

TheShaner
08-21-2007, 05:15 PM
There is a stickied thread with program language references. Here's the one for C++: http://www.codingforums.com/showthread.php?p=218467#post218467

oracleguy
08-21-2007, 05:30 PM
Hi guys ,

I have this test code :

#include <iostream>
#include <string>

int main()
{
char* data = "";
std::cin >> data;
std::cout << strlen(data);
}

Please test it on your compiler , What's problem of this code ?


You can't cin into a pointer like that. That is the problem, you'd have to allocate some memory first and have the pointer pointing to it. However unless you really need to use a pointer, just use the string class like Sage demonstrated.

Active X
08-23-2007, 12:44 PM
Hi guys ,

Thank you very very much
You really helped .

Active X
08-24-2007, 11:34 AM
Oh ! another problem guys ,

I have this code :

#include <iostream>
#include <string>
using namespace std;
int main()
{
string myStr = "This is some text I write";
cin >> myStr;
cout << myStr.length();
cin >> "";
return 0;
}

but when I insert a space in the string , program is automatically closed .

Need your emergency help
Thank you very very much

sage45
08-24-2007, 05:09 PM
Oh ! another problem guys ,

I have this code :

#include <iostream>
#include <string>
using namespace std;
int main()
{
string myStr = "This is some text I write";
cin >> myStr;
cout << myStr.length();
cin >> "";
return 0;
}

but when I insert a space in the string , program is automatically closed .Try this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string myStr = "This is some text I write";
cin >> myStr;
cout << myStr.length();
cout << "Press any key to continue...";
cin.get();
return 0;
}HTH,

-saige-

Active X
08-25-2007, 10:48 AM
Thank you sage , but it didn't work too .

Test THIS code on your compiler :

// Main file of this project
#include <iostream>
#include <string>
using namespace std;
int main()
{
string myStr;
cin >> myStr;
cout << myStr.length();
cin >> "";
return 0;
}

and when you want to give it the string variable , Insert a space in your string and see what happens .
When I do that , IDE terminates the program .

And the cin >> "" is just to not let the program be closed after run

Thank you for helping

oracleguy
08-25-2007, 07:50 PM
Thank you sage , but it didn't work too .

Test THIS code on your compiler :

// Main file of this project
#include <iostream>
#include <string>
using namespace std;
int main()
{
string myStr;
cin >> myStr;
cout << myStr.length();
cin >> "";
return 0;
}

and when you want to give it the string variable , Insert a space in your string and see what happens .
When I do that , IDE terminates the program .

And the cin >> "" is just to not let the program be closed after run

Thank you for helping

You can't cin into a constant, which "" is.

You can use system("pause") if it is only going to be ran on Windows. Otherwise the more platform independent version would be:


cout << "Press enter to continue..." << endl;

cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.get();

Gox
08-25-2007, 10:11 PM
I don't have the means to test, but my guess is it's the way that cin reads the buffer.

If cin reads until it finds white-space, i.e. a space then it will stop there, thus leaving the rest of the string in the buffer. If this is true then when your program hits the cin >> "" line it's probably reading the remainder of the string that is still in the buffer rather than waiting for input. Given your symptoms this would make some sense.

Can someone verify this is how cin works?

This page suggests at the very least that cin may leave the terminating character in the buffer (i.e. <enter> or newline). Their suggestion is to follow cin with a call to cin.ignore, to ignore the terminating character.
http://www.cprogramming.com/tips/showTip.php?tip=42&count=30&page=0

It's a good idea to follow cin with cin.get() or cin.ignore() because cin can leave a terminating character in the stream, which could case small problems with your code.

Again, this sounds to me like the issue. Of course there is the matter of whether you can cin to a constant which was pointed out in the post above mine.

Active X
08-26-2007, 01:17 PM
Hi guys ,

I'm really confused . Could you write me such a program please :

Read a string , Get it's length , Just this . But it must also be able to read spaces in the input string .

Thank you very very much

oracleguy
08-26-2007, 05:22 PM
cin >> myStr;

Then instead of doing that, use cin.getline() to take in text including spaces. If you have a C++ book look the function up or you can google it to find the parameters it takes.

sage45
08-27-2007, 03:57 PM
Hi guys ,

I'm really confused . Could you write me such a program please :

Read a string , Get it's length , Just this . But it must also be able to read spaces in the input string .

Thank you very very muchActive X. The code that has been provided will do everything that you want. The only problem you are having is getting it to stay at the command prompt when ran from the debugger. Either you can add-in the code to accomplish this or you can simply drop to a command prompt, go to the location of the executable and run it from the command line.

Since this is a console application, once it has executed the return 0 line in the code, any time you attempt to run from the debugger or from within a windows explorer screen, you will get the same result, i.e. - you input a string, the application returns the specified value and then closes the window. If however, you run it from a command line interface (If Windows XP: Start --> Run --> CMD) the window for the command line interface remains because this is the environment that the application was designed to run in and does not provide a call to exit the environment.

HTH,

-saige-

Active X
08-27-2007, 04:29 PM
Thank you very very much



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum