-3

I have a string like ATGCCA... . This string will be transformed into an array of char as [ATG CCA ...]. I already know that ATG=1 and CCA=2, and I have defined these as double. How can I save the transformed matrix as a double? Here is my program now, but it doesn't work:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

using namespace std;

int main() {
double ATG=1, CCA=2;

fstream fp("sequence.txt",ios::in);
if(!fp)
{
    cerr<<"File can not open!"<<endl;
    exit(1);
}

char content,k,l,m;
char a[4]="";
double n;

while(fp>>k>>l>>m){
fp.read(a, sizeof(a) - 1); 
    n=atof(a);  
    cout<<a<<"  "<<n<<endl;
}
}

I expect to see this as output:

ATG 1
CCA 2

But what I see is:

ATG 0
CCA 0

Thank you for your help!

  • 1
    _"I already know that ATG=1 and CCA=2"_ Huh, what please? – πάντα ῥεῖ Jun 16 '17 at 10:35
  • `n=atof(a);`: It doesn't work this way. You have to use string comparison. The relevant functions can be found in `string.h` or better [`cstring`](http://en.cppreference.com/w/cpp/header/cstring). In general, I would suggest to use [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string) instead of `char*` or `char[]`. `std::string` provides methods to find or compare (sub-)strings. – Scheff's Cat Jun 16 '17 at 10:35
  • why should `n=atof(a)` give you 1 if a=`ATG`? – Walter Jun 16 '17 at 10:40
  • Could you provide a (not too long) sample input file? I tried the code of one of the answers in an online compiler but struggle to read correct data. (I'm afraid my sample input is wrong. Otherwise, the reading of input data is not correct implemented in your sample.) (Please, use the **edit** link to edit the question.) – Scheff's Cat Jun 16 '17 at 11:13

2 Answers2

4

The variables ATG and CCA have no relation to any chars you read in.

You probably want to associate strings to doubles, for which you will need an Associative Container, e.g. std::map<std::string, double>.

#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::map<std::string, double> lookup = { { "ATG", 1}, { "CCA", 2 } };

    std::fstream fp("sequence.txt",std::ios::in);
    if(!fp)
    {
        std::cerr<<"File can not open!"<<std::endl;
        exit(1);
    }

    char content,k,l,m;
    char a[4]="";
    double n;

    while(fp>>k>>l>>m){
    fp.read(a, sizeof(a) - 1); 
        n=lookup[a];  
        std::cout<<a<<"  "<<n<<std::endl;
    }
}
Caleth
  • 52,200
  • 2
  • 44
  • 75
1

It seems you are reading a string, which is "ATG", and you expect atof to use that as the name of a variable from which to extract its value. There are several chained erorrs in that reasoning.

You would need something like a map (code not tested):

#include <map>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

int main() {
    map<string, double> amino;
    amino["ATG"] = 1;
    amino["CCA"] = 2;
    // ... Complete with the other 62 codons

    fstream fp("sequence.txt",ios::in);
    if(!fp)
    {
        cerr<<"File can not open!"<<endl;
        exit(1);
    }

    char content, k, l, m;
    char a[4]="";
    double n;

    while(fp >> k >> l >> m) {
    fp.read(a, sizeof(a) - 1); 
        n = amino[a];  
        cout << a << "  " << n << endl;
    }

    return 0;
}

Note that you might want to use ints instead of doubles. And perhaps some checking to make sure that the sequences read are actually codons.

You might need/want to use array for the keys of the map pairs, see

unsigned char array as key in a map (STL - C++)

Character Array as a value in C++ map

Using char* as a key in std::map