Author Topic: [C++]using String  (Read 2139 times)

0 Members and 1 Guest are viewing this topic.

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
[C++]using String
« 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?
« Last Edit: April 13, 2011, 04:42:18 pm by gh0st »

Offline p_2001

  • Royal Highness
  • ****
  • Posts: 684
  • Cookies: -64
    • View Profile
Re: [C++]using String
« Reply #1 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
"Always have a plan"

Offline jeyanthinath

  • /dev/null
  • *
  • Posts: 17
  • Cookies: -2
  • Do what you think !!!
    • View Profile
Re: [C++]using String
« Reply #2 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 ...
JMR

Offline Frankenstinejoe

  • NULL
  • Posts: 3
  • Cookies: 0
  • GoOoing BLaCk ... !!!
    • View Profile
Re: [C++]using String
« Reply #3 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 .
Asta Lavista Baby ...!!!