Character Arrays

This is a discussion on Character Arrays within the C++ Programming forums, part of the General Programming Boards category; I've got write a program that reads a data file which contains a paragraph. I need to take each letter ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    16

    Character Arrays

    I've got write a program that reads a data file which contains a paragraph. I need to take each letter and convert it to two letters higher. So a-->c d-->f and y-->{. The spaces must not be changed. It needs to print the output into another data file. I must use arrays and have it in int main().

    I would appreciate it if someone could point me in the write direction or show me a program that is somewhat like what I need.
    Thanks

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    What, in the above problem, do you not know how to do?

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    I know how to open the file and I know how to output a data file but I'm not sure how to read from the file. I know I need to use get() I think but I'm not sure how to get char array set up to read the paragraph.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by JHaney
    I know how to open the file and I know how to output a data file but I'm not sure how to read from the file. I know I need to use get() I think but I'm not sure how to get char array set up to read the paragraph.
    Don't use a char array to read the file. Instead, read the data into a char variable. Then, use a char array to store the converted characters. A char array works just like a numerical array, and it can be used to hold values just like a numerical array can--the only difference is that the values the char array holds are char's rather than numbers. Here is an example:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
    	char input = 'a';
    	char converted[100] = {0};
    
    	converted[0] = input;
    
    	input = 'b';
    	converted[1] = input;
    
    	input = 'c';
    	converted[2] = input;
    	
    	cout<<converted<<endl;
    	
    	return 0;
    }
    Last edited by 7stud; 10-27-2005 at 12:24 AM.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    well I don't know how to read a file from c++, but you can save the caracters in an int array, so when you read a character just put something like:

    intArray[i] = input + 2;

    assuming that input is a char and have the value of the character read from the file...

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    Thanks for the replies. Now I'm wondering how to get one letter at a time from the data file and how to store it. I can use the stuff yall have posted so thanks for that.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    char ch;
    while( inputFile.get(ch) )
    {
         //do something with ch
    }
    When reading from a file, you should use the read statement as your while conditional. That way if there is an error while reading from the file, things will terminate correctly. Otherwise, you could get stuck in an infinite loop. That version of get() will return an object that will evaluate to false if there is an error while reading from the file.

    You should be aware that at the end of a line in a file, there is an invisible '\n' character, which was placed there when you hit return to start a new line. get() will read in the '\n' character, so not only do you have to leave spaces alone, you also have to check for a '\n'.
    Last edited by 7stud; 10-27-2005 at 03:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  4. Comparing Character Arrays problem...
    By newy100 in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2003, 07:54 PM
  5. Spaces in Character Arrays
    By ADLOTS in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2002, 04:24 AM

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