need help with string creation

i am trying to make a palindrome finder using strings
and my program just stops running at the test case input number, so i am guessing that my declaration is wrong.
this is just the even section of the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 #include<iostream>
 #include<string>

using namespace std;

int main()
{
    int t;
     cin>>t;
    string number[t];

      for(int i=t; i<t; i++)
        {
            cin>>number[t]; /*i used cin instead of getline because i expect a single stream of numbers with no spaces. getline creats a large gap between my last test case input and my string input. */
            int c = number[t].size()/2;
            int l = number[t].size();


            if(l%2 == 0) //Even
                {
                    if(number[t].at(c) - number[t].at(c-1) > 1)
                    {
                        number[t].at(c-1)++;
                        number[t].at(c)=number[t].at(c-1);
                    }
                    else if(number[t].at(c-1) > number[t].at(c))
                    {
                        number[t].at(c)=number[t].at(c-1);
                    }

                    for(int x=0 , y=l ; x<=c-1 && y>=c+1 ; x++ , y--)
                        {
                            if(number[t].at(x)=number[t].at(y))
                            {
                                continue;
                            }
                            else {number[t].at(y)=number[t].at(x);}
                        }

                }
        }

            for(int i=0 ; i<t ; i++)
            {
                cout<<number[t];
            }
}
you declare a string like this

string str = "test";

if you use the subscript operator [], you are accessing a char in the sting


cout << str[0]; //output: t

Do you want an array of strings?

string str[] = {"test","test0","test1"};

Then if you use the subscript you get the word:

1
2
string str[] = {"test","test0","test1"};
cout << str[0]; //output: test 


to loop thru a string to each char:

1
2
for(int i = 0; i != str.size();++i)
     cout << str[i] << endl;


If you read to an int, then try to copy the int to the string, your going to get bad data, you want to read the numbers to a string.
Last edited on
i need to declare an array of strings whose size and contents are user dependent
sorta like this

first the user inputs the number of inputs he wants.
so if he wants to put in 3 number he will just enter 3

then i want 3 empty strings to be created and accessed sequentially.

so for example
here is the users input
3 ---> no. of numbers to be inputed
457 ---> placed in string0 in the array
*my algo does its work and changes string0's contents to 464
7884---> placed in string1
*changed by algo, becomes 7887 in same string1
45945--->place in string 2
*changed by algo, becomes 45954

and then the 3 strings to be outputted at once.

....kinda convoluted.
You want to use a vector then:

1
2
3
4
5
6
7
8
9
vector<string> str;

int num;
string empty = " ";
cout << "Enter number of string elements" << endl;
cin >> num;

for(int i = 0; i != num; ++i)
     str.push_back(empty);


Now the vector has whatever number of blank strings the user inputed, a vector is like a array but doesnt have a fixed size.

http://www.cplusplus.com/reference/vector/vector/

You can then assign elements like and array

str[0] = "blah blah";
Last edited on
here
1
2
for(int i = 0; i != num; ++i)
     str.push_back(empty);


i am confused here, doesn't push_back refer to the last string in the vector str?

how can it do that when the total number of strings in the vector is still undefined?

i am sorry if i am coming off as an idiot here but this is sort of a crash course in vectors for me.
Last edited on
oh .. wait not i geddit, push back adds a string..so looping creates x number of strings the user needs... cool
thanks man.. really appreciate it ^^
Topic archived. No new replies allowed.