converting a string to array

This is a discussion on converting a string to array within the C++ Programming forums, part of the General Programming Boards category; Hi all; So I have an assignment that needs me to take in a file that simply has a string ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    15

    converting a string to array

    Hi all;
    So I have an assignment that needs me to take in a file that simply has a string of characters
    (for example: aacgttatgtgtaaccagaactgatgt) and output a file that assigns each letter a value (for example, a =1, c=2, etc.) I've been thinking about how to go about doing this, and settled on somehow converting the string into a character array, which i can then use to call a character and assign it a value.
    Code:
    int main (int argc, char*argv[])
    {
    	
    	if (argc != 3) //if there are an incorrect number of arguments
    	{
    		cerr 	<< "\n\tIncorrect usage.\n"
    		<< "\tCorrect syntax: " << argv[0] << " inputFile outPutFile\n\n";
    		exit(1);
    	}
    	
    	ifstream fin;
    	ofstream fout;
    	
    	fin.open (argv[1]); //opens input file from command line
    	
    	if (fin.fail()) //if file cannot open
    	{
    		cerr << "Could not open " <<argv[1] << endl;
    		exit(1);
    	}
    	
    	fout.open (argv[2]);
    	
    	if (fout.fail()) //if file cannot open
    	{
    		cerr << "Could not open " <<argv[2] << endl;
    		exit(1);
    	}
    	string test;
    	int characters = test.size();
    	char inputArray[299]; //arbitrary size of array
    
    	
    	fin >> test;
    	for (int i =0; i <= characters; i++) 
    	{
    		
    		fout << inputArray[];
    	}
    	
    		fout << test<< endl;
    	
    	fin.close();
    	fout.close();
    	
    	return 0;
    	
    }
    so far, to just feel like I'm outputting something, all i have it doing is just assigning the string to "test", and printing out "test" into the output file. I set up some error messages, but those are unimportant for now. I'm particulary confused on how I would create and use the array. Am i going about this completely the wrong way? Thanks in advance.
    Last edited by jtieu; 03-28-2011 at 01:14 AM.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    Could you give a little bit more description to what you are trying to achieve please, like a sample input, and sample output.
    There are however a few problems with your snippet above,
    You saying you need to read from a file containing a string of char but here:
    fin >> test;
    you are actually trying to read the string to the file. From your description, I assume that the file must already contain the characters you need.

    for (int i =0; i <= characters; i++)
    {

    fout << inputArray[];
    }
    What is the block above suppose to achieve, since your inputArray[] is empty?

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    15
    Quote Originally Posted by Servalsoft View Post
    Could you give a little bit more description to what you are trying to achieve please, like a sample input, and sample output.
    There are however a few problems with your snippet above,
    You saying you need to read from a file containing a string of char but here:

    you are actually trying to read the string to the file. From your description, I assume that the file must already contain the characters you need.


    What is the block above suppose to achieve, since your inputArray[] is empty?
    The file contains a string of characters, for example, "actgtcat".
    Sorry for not being clear. I want to assign a value for each character For example, reading a string of "aatcgatatcg", and assigning the character 'a' to a value of 5, or assigning a value of 'c' to a value of 2, which I would then add up somehow, so a string value would eventually add up to 1838, or something. I would have to split the string into separate charcters right? Or am I speaking nonsense?
    I'm starting to get a sense of how to accomplish this, but I'll have to experiment some more with actually coding it.
    And sorry about that block of code, it was born out of frustration, and running out of ideas, I forgot to take it out. It's not part of anything, and doesn't really do anything. Actually, i think it gives an error message anyways.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,702
    Since the char 'a' already has a numerical value (97) why can't you use that number?

    Code:
    #include <iostream>
    
    int main()
    {
        char myChar = 'a';
        std::cout << myChar << " " << static_cast<int>(myChar) << std::endl;
        // OR
        int myInt = myChar
        std::cout << myChar << " " << myInt << std::endl;
    
    }
    Jim

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    15
    Right. Is there a way to assign it a value though, treating it as a variable?

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,661
    Sounds like you need something like this:
    Code:
    #include <map>
    
    std::map<char, int> codes;
    codes.insert( std::make_pair('a', 5) );
    codes.insert( std::make_pair('c', 2) );
    codes.insert( std::make_pair('g', 3) );
    codes.insert( std::make_pair('t', 7) );
    
    codes['a'] // == 5
    Or use a vector of pairs, or whatever you are comfy with. You should error check the insert function, though.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    Quote Originally Posted by jtieu View Post
    Right. Is there a way to assign it a value though, treating it as a variable?
    I suggest you use what Jim mentioned above, if you google "ascii" table you will see the numerical value for most ascii characters, and then you can just perform your addition as normal, as the characters will be automatically casted to their decimal values.
    I think this is the easiest way to go, unless the assignment is specific about assigning the values.
    I hope this helps.
    Yan

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    15
    Hmm, alright, maybe I can modify it after I find the numeric value for each char. I'm going to try screwing around with the code for a little while longer anyways.
    Thanks everyone for putting up with my incompetence. I'll post a solution as soon as I get one in case anyone else cares.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    Im sure some people do, I'd certainly like to see your final approach.
    Happy coding

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. Converting to expression to string of array elements
    By Sailors in forum C Programming
    Replies: 12
    Last Post: 07-26-2007, 03:01 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21