Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 01-20-2005, 08:01 AM   #21 (permalink)
 
Newb Techie

Join Date: Jan 2005

Posts: 9

holy_devil_

Default

if I can define

string arr[5] = {"a","ab","abc","abcd","abcde"};

then how will the compiler know how long is the string
like first string is 1 char and the last is 5 char and allocate
a memory space accordingly.
EXample:

int x;
this statement reserves 2 bytes for an iteger type data pointed at by x.

how will this work for string data type.

if i declare
char arr[10][5];
then compiler will reserve total of 10*5=15 bytes one for each character and some of these characters will form a string(rows).
holy_devil_ is offline  
Old 01-20-2005, 10:02 AM   #22 (permalink)
 
True Techie

Join Date: Jan 2005

Posts: 113

RedRecon2004

Default

Quote:
Originally posted by holy_devil_
if I can define

string arr[5] = {"a","ab","abc","abcd","abcde"};

then how will the compiler know how long is the string
like first string is 1 char and the last is 5 char and allocate
a memory space accordingly.
EXample:

int x;
this statement reserves 2 bytes for an iteger type data pointed at by x.

how will this work for string data type.

if i declare
char arr[10][5];
then compiler will reserve total of 10*5=15 bytes one for each character and some of these characters will form a string(rows).
I believe that when the array is created, the compiler actually looks inside of those brackets, and just goes from there in terms of seeing what size the string objects are and how to reserve space for them.
RedRecon2004 is offline  
Old 01-20-2005, 10:43 AM   #23 (permalink)
 
True Techie

Join Date: Jan 2005

Posts: 113

RedRecon2004

Default

Oh, and sorry to double-post, but you guys probably need to learn about vectors, too..... use this as an example:

#include <iostream>
#include <vector> //Notice that vector is now a new thing to include
#include <string>
using namespace std;

int main()
{
vector<string> inventory; //vector<data_type> vector_name
inventory.push_back("sword"); //Puts "sword" string in vector
inventory.push_back("armor"); // Puts "armor" string in vector
inventory.push_back("shield"); // Puts "shield" string in vector

cout << You have " << inventory.size() << " items.\n";

cout << "\nYour items: \n"; //Shows items.
for(int i = 0; i < inventory.size(); ++i)
cout << inventory[i] << endl;

cout << "You trade your sword for a battle axe.";
inventory[0] = "battle axe"; //inventory[0] corresponds to "sword"
cout << "\nYour items: \n";
for (int i = 0; i < inventory.size(); ++i)
cout << inventory[i] << endl;

cout << "Your shield is detroyed in a fierce battle.";
inventory.pop_back(); //pop_back takes away the last thing assigned (in this case, shield)
cout << "\nYour items: \n";
for (int i = 0; i < inventory.size(); ++i)
cout << inventory[i] << endl;

cout << "You are robbed of all of your possessions!";
inventory.clear(); //Clear, well, clears your vector!
if (inventory.empty())
cout << "You have nothing.\n";
else
cout << "\nYou still have at least one item.\n";

cout << endl;

system("pause");
return 0;

}
__________________
I\'m not a vegetable!
RedRecon2004 is offline  
Old 01-22-2005, 09:22 PM   #24 (permalink)
 
Newb Techie

Join Date: Nov 2004

Posts: 28

Windwaker222

Default

alrite, i just started learning c++ today and i made a program like this except its not as advanced...

I just wanted to know what these two lines do

clrscr();

strcpy(item[i],"Hand Gun");
Windwaker222 is offline  
Old 01-23-2005, 12:15 AM   #25 (permalink)
 
True Techie

Join Date: Jan 2005

Posts: 113

RedRecon2004

Default

If you want me to be truthful with you, I've never seen such code as that used for beginning C++ ....

If you are really wanting to put "Hand gun" inside of an array, you could do it like this:

int numItems = 0;
string inventory[1];

inventory[numItems++] = "Handgun";

for (int i = 0; i < numItems; ++i)
cout << inventory[i] << endl;

If that's not what you are wanting to do, then I'm clueless.
__________________
I\'m not a vegetable!
RedRecon2004 is offline  
Old 01-23-2005, 04:33 AM   #26 (permalink)
 
Newb Techie

Join Date: Aug 2004

Posts: 22

vidyaputra

Default

Quote:
Originally posted by Windwaker222
alrite, i just started learning c++ today and i made a program like this except its not as advanced...

I just wanted to know what these two lines do

clrscr();

strcpy(item[i],"Hand Gun");
the clrscr is telling the compiler to clear the screen, but its gonna need #include <conio.h> as the header file.

and the strcpy() is a command to copy a string / char...
because u cant use "=" to assign a string or char^^, and for this one need #include <string.h>
...

-vip-
vidyaputra is offline  
Old 01-23-2005, 11:27 AM   #27 (permalink)
 
True Techie

Join Date: Jan 2005

Posts: 113

RedRecon2004

Default

Quote:
Originally posted by vidyaputra
the clrscr is telling the compiler to clear the screen, but its gonna need #include <conio.h> as the header file.

and the strcpy() is a command to copy a string / char...
because u cant use "=" to assign a string or char^^, and for this one need #include <string.h>
...

-vip-
Yeah, that code isn't really necessary.

You'd be better off by just using a string object or something instead..
RedRecon2004 is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On