How do I convert my char array that reads 50 characters to a string array that reads an unlimited amount of charaters


I'm trying to change my char array to string so an unlimited amount of characters can be entered instead of it being a limit ex. (char DNA[50]) this means I can only enter 50 DNA characters but I want it to be unlimited. The idea i had is to change the char DNA[50] to string DNA; then declaring int DNA length and then DNAlength=DNA.Length(). well I enter that in my code and I have problems here's my original 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
48
49
50
51

#include<iostream>
#include<fstream>
#include <string>
using namespace std;
void KeyInput()
{

char DNA[50];
char RNA[50];

cout<<"Enter DNA Sequence"<<endl;
int len=0;
cin>>DNA;
for(int i=0; i<=sizeof(DNA); i++){

if (DNA[i] == 'A')
{cout<<"U";
RNA[i]='U';
len++;
}
if (DNA[i] == 'G')
{cout<<"C";
RNA[i]='C';
len++;
}
if (DNA[i] == 'C')
{cout<<"G";
RNA[i]='G';
len++;
}

if (DNA[i] == 'T')
{cout<<"A";
RNA[i]='A';
len++;
}

}
cout<<endl;
for(int i=0; i<len; i++){
cout<<RNA[i];
}
cout<<endl<<sizeof(DNA)<<endl;
cout<<len<<endl;
fstream file;
file.open("IOResults.txt",ios::out);
file<<DNA<<endl<<RNA<<endl;
file.close();

}

How do I change this to read and store an Unlimited amount of characters????
do I even have to change my char to string to do this?
Last edited on
google before you ask a question,
all you need to do is dynamically allocate the size
this way
1
2
3
int size=500;//any size you want
char * buffer;//pointer
buffer = new char [size];// 

Use string instead of the array:

http://www.cplusplus.com/reference/string/string/
The idea i had is to change the char DNA[50] to string DNA; then declaring int DNA length and then DNAlength=DNA.Length(). well I enter that in my code and I have problems here's my original code
http://www.cplusplus.com/forum/articles/40071/#msg216313
_ Don't post code which is different from your own but does not reproduce the problem ( or has other problems )
http://www.cplusplus.com/forum/articles/40071/#msg218019
_ Be specific about why it doesn't work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void KeyInput()
{
    std::string DNA ;
    std::cout << " Enter DNA Sequence: " ;
    std::cin >> DNA ;

    std::string RNA ;
    for( std::size_t i = 0 ; i < DNA.size() ; ++i )
    {
        switch( DNA[i] )
        {
            case 'A' : RNA += 'U' ; break ;
            case 'G' : RNA += 'C' ; break ;
            case 'C' : RNA += 'G' ; break ;
            case 'T' : RNA += 'A' ; break ;
            default : std::cerr << "error in input " << DNA[i] << '\n' ;
        }
    }

    std::ofstream file ( "IOResults.txt" ) ;
    file << DNA << '\n' << RNA << '\n';
}
An alternative 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
std::string::size_type KeyInput()
{
    const size_t N = 5;
    const DUMMY_CHAR = '?';
    const char DNA_CHAR[N] = "AGCT";
    const char RNA_CHAR[N] = "UCGA";
   
    std::string DNA ;
    std::cout << " Enter DNA Sequence: " ;
    std::cin >> DNA ;

    std::string RNA ;
    RNA.reserve( DNA.size() );

    for ( std::string::value_type c : DNA  )
    {
        char *p = std::strchr( DNA_CHAR, c );

        if ( p ) 
        {
            RNA += RNA_CHAR[p - DNA_CHAR];
        }
        else 
        {
            std::cerr << "error in input " << DNA[i] << '\n' ;
            RNA += DUMMY_CHAR;
        }
    }

    std::ofstream file ( "IOResults.txt" ) ;
    file << DNA << '\n' << RNA << '\n';

    return ( DNA.size() );
}
Last edited on
Thanks alot fellas
you guys make me realize I got a whole lot to learn
how long does it usually take to get to a pretty confident level of being a legit programmer?
6 months? 1 year?
whats the average time i should expect to become good at this?
there always room for improvement, but i think within a month or two you should be able to do most of the basic things in a good way :)
Topic archived. No new replies allowed.