how do i make array of string in c++

October 22, 2010 at 06:48:18
Specs: Windows XP

becous it always make error with me
like this code
#include<iostream>
#include <string>
using namespade std;
int main()
{
string str1("Call home!");
return 0;
}

See More: how do i make array of string in c++

Report •

#1
October 22, 2010 at 07:05:27
becous it always make error with me
Probably because you misspelled "namespace". Compliers are a lot less forgiving than a human.

How To Ask Questions The Smart Way


Report •

#2
October 22, 2010 at 07:15:35
it accept this
string t("Call home!","iam c++",);
but this
string t("Call home!","iam c++","my head");
is not
Acceptable

Report •

#3
October 22, 2010 at 08:23:08
Neither of them work. The former probably resolves to template<class InputIterator> string (InputIterator begin, InputIterator end), so it should crash when you tried to run your application. (But I'm too lazy to check.)

You could use the following:

string t[] = {"Call home!","iam c++","my head"};
Or just use vector<string>, and live with its lack of nice initialization. (We'll get good vector<string> initialization with C++0x, whenever it happens.)

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
October 22, 2010 at 23:51:11
Hey,
u also can use this syntax :
char **t;
t[0]="Call home"; t[1]="I am C++";

hth


Report •

#5
October 23, 2010 at 13:20:26
thx so much
i did a code but my code doesnt work rigth i dont know why??
"" morse code""
its Not completed
*******************************************************
*******************************************************

#include<iostream>
#include <string>
using namespace std;

int main ()
{

char *arrayname[36];
arrayname[0] = ".- ";
arrayname[1] = "-...";
arrayname[2] = "-.-.";
arrayname[3] = " -..";
arrayname[4] = ".";
arrayname[5] = "..-.";
arrayname[6] = " --.";
arrayname[7] = "....";
arrayname[8] = "..";
arrayname[9] = ".---";
arrayname[10] = "-.-";
arrayname[11] = ".-..";
arrayname[12] = "--";
arrayname[13] = "-.";
arrayname[14] = " ---";
arrayname[15] = ".--.";
arrayname[16] = "--.-";
arrayname[17] = ".-.";
arrayname[18] = "...";
arrayname[19] = "-";
arrayname[20] = "..-";
arrayname[21] = "...-";
arrayname[22] = " .--";
arrayname[23] = "-..-";
arrayname[24] = "-.--";
arrayname[25] = " --..";
arrayname[26] = " -----";
arrayname[27] = ".----";
arrayname[28] = " ..---";
arrayname[29] = "...--";
arrayname[30] = "....-";
arrayname[31] = ".....";
arrayname[32] = "-....";
arrayname[33] = "--...";
arrayname[34] = "---..";
arrayname[35] = " ----.";

const int max=100;
char s[max];

char ch;
do{

cout<<"enter your"<<endl;
cin.get(s,max,'*');
for(int n=0;n<strlen(s);n++)
{
switch (s[n])
{
case'a': cout<<arrayname[0];break;
case'b': cout<<arrayname[1];break;
case'c': cout<<arrayname[2];break;
case'd': cout<<arrayname[3];break;
case'e': cout<<arrayname[4];break;
case'f': cout<<arrayname[5];break;
case'g': cout<<arrayname[6];break;
case'h': cout<<arrayname[7];break;
case'i': cout<<arrayname[8];break;
case'j': cout<<arrayname[9];break;
case'k': cout<<arrayname[10];break;
case'l': cout<<arrayname[11];break;
case'm': cout<<arrayname[12];break;
case'n': cout<<arrayname[13];break;
case'o': cout<<arrayname[14];break;
case'p': cout<<arrayname[15];break;
case'q': cout<<arrayname[16];break;
case'r': cout<<arrayname[17];break;
case's': cout<<arrayname[18];break;
case't': cout<<arrayname[19];break;
case'u': cout<<arrayname[20];break;
case'v': cout<<arrayname[21];break;
case'w': cout<<arrayname[22];break;
case'x': cout<<arrayname[23];break;
case'y': cout<<arrayname[24];break;
case'z': cout<<arrayname[25];break;
case'0': cout<<arrayname[26];break;
case'1': cout<<arrayname[27];break;
case'2': cout<<arrayname[28];break;
case'3': cout<<arrayname[29];break;
case'4': cout<<arrayname[30];break;
case'5': cout<<arrayname[31];break;
case'6': cout<<arrayname[32];break;
case'7': cout<<arrayname[33];break;
case'8': cout<<arrayname[34];break;
case'9': cout<<arrayname[35];break;
}
}

cout<<"do you want another operation (y/n)"<<endl;
cin>>ch;

}
while(ch!='n');


return 0;
}

(do&while) loop doesnt work,,


Report •

Ask Question