EvilZone

Programming and Scripting => C - C++ => : gh0st April 11, 2011, 12:35:15 AM

: [C++]using String
: gh0st April 11, 2011, 12:35:15 AM
well the problem was:
2. Rewrite Listing 4.4, using the C++ string class instead of char arrays.

Listing 4.4
:
// instr2.cpp -- reading more than one word with getline
#include <iostream>
int main()
{
using namespace std;
const int ArSize = 20;
char name[ArSize];
char dessert[ArSize];
cout << “Enter your name:\n”;
cin.getline(name, ArSize); // reads through newline
cout << “Enter your favorite dessert:\n”;
cin.getline(dessert, ArSize);
cout << “I have some delicious “ << dessert;
cout << “ for you, “ << name << “.\n”;
return 0;
}

so solving this would be:

:
#include <string>
#include <iostream>

int main()
{
using namespace std;
string name,dessert;
cout << "Enter your name:\n";
cin >> name;
cout << "Enter your favorite dessert:\n";
cin >> dessert;
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
system("pause");
return 0;
}

finally I learn what is a string I think that using string is better than using any types of variables right? cause why would be it created?
: Re: [C++]using String
: p_2001 May 11, 2012, 03:01:23 PM
somethings to add!

using pointers instead of direct String.h helps...... you get a better understanding of allocations and working with pointers...

you can manipulate strings with your own functions such as reversing, searching etc.

for a beginner one must learn how to do it himself and later open the string.h and study it....

always use what you have written yourself as long as you can
: Re: [C++]using String
: jeyanthinath July 04, 2012, 04:09:24 PM
somethings to add!

using pointers instead of direct String.h helps...... you get a better understanding of allocations and working with pointers...

you can manipulate strings with your own functions such as reversing, searching etc.

for a beginner one must learn how to do it himself and later open the string.h and study it....

always use what you have written yourself as long as you can


the Better idea is to go with him because the Pointer are the most effective thing that is available with C/C++ (The Powerful things)

So go more with more examples on "Arrays and Pointers"

It would be little more better than using inbuilt function...

In C there is not String datatype to hang-on ...
: Re: [C++]using String
: Frankenstinejoe July 24, 2012, 12:26:37 PM
You should only go for pointers if you have full control over them otherwwise simpler is better .