EvilZone

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

Title: [C++]using String
Post by: gh0st on 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
Code: [Select]
// 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:

Code: [Select]
#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?
Title: Re: [C++]using String
Post by: p_2001 on 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
Title: Re: [C++]using String
Post by: jeyanthinath on 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 ...
Title: Re: [C++]using String
Post by: Frankenstinejoe on July 24, 2012, 12:26:37 PM
You should only go for pointers if you have full control over them otherwwise simpler is better .