Convert string of characters to integers

This is a discussion on Convert string of characters to integers within the C++ Programming forums, part of the General Programming Boards category; Hello, for the program below i need to write a function (int str_to_int) which will convert the string of characters ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    Unhappy Convert string of characters to integers

    Hello, for the program below i need to write a function (int str_to_int) which will convert the string of characters to integers and than another one (int calculate_sum) which will calculate sum of them.
    This program is supposed to read from .txt file (which contains 5 numbers:
    2341
    23451
    121
    887
    2301
    5551
    Can anybody help me, I have no idea where are to start.
    Code:
    #include <iostream>
    using namespace std;
    
    int const	STR_SIZE = 30;
    int const ARR_SIZE = 100;
    char const DELIMITER = '\n';
    
    int readLine(char str[], int max, char delimiter);
    /* This function reads several lines of characters from standard input stream
     * Stops reading when it reads a delimiter characte (for example '\n'),
     * or when reaches the end of file, or...when the number of charaters read, 
     * reaches to the limit (max number of charaters)
     * Adds a backslash zero as a string terminator to the end of string.
     * Returns -1 if reads the end of file otherwise returns 1.
     * Terminates the program if any error happens when reading characters.
     */
    
    int str_to_int(const char digits[]);
    /* str represents an array of characters that contains digits terminated with
     * a '\0'. For example: it may contain digits[0] == '7', digits[1] == '5', 
     * digits[2]== '3', and digit[3] == '\0'. 
     * ‘\0’ is also know as c_string terminator. I is used in indicate the end of
     * the string.  
     * This function should return an equivalent integer value for digits. 
     * For the above example, it should return integer: 753.
     */
    
    int calculate_sum(int int_number, int n);
    
    
    
    int readLine(char str[], int max, char delimiter)
    {
      char c;
    	int i =0;
      do {
        cin.get(c); // a standard input function that reads characters
    		
    		if(!cin.eof() && cin.fail()){
    			cout << "\nReading from input stream failed...";
    		  exit(1);
    		}
    		
    		if(cin.eof()) return -1;
    		
    		if(i == max-1 || c == '\n' )
    			break;
    		else
    			str[i] = c;
    		
    		i++;
      } while(1);
    	
    	str[i] = '\0';  // put a backslash zero to the end of str.
    	return 1;
    }
    
    
    int main(void)
    {
    	char str[STR_SIZE];
    	int int_numbers [ARR_SIZE];
    	cout << "\nThis program reads numbers from a text file, using redirection ";
    	cout << "\n input operator \'<\'on the command line." << endl;
      
    	cout << "\nReading and echoing input ...." << endl;
    	
    	int i=0;
    	// an infinite loop that will be terminated with a break statement
    	while(1){
    		// read one line from the input stream
    		if(readLine(str, STR_SIZE, DELIMITER) == -1)
    			break;
    		
    		// display the string of characters on the screen
    		cout << endl << str;
    		
    		//UNCOMMENT THE FOLLOWING LINE TO TEST THEIR FUNCTION;
    		// int_number[i] = str_to_int(str);
    		i++;
    	}
    		
    	// UNCOMMENT THE FOLLOWING LINES TO TEST THEIR FUNCTION;
    	// int total = calculate_sum(int_numbers, i );
    	// cout << "\n\nThe total of the numbers in the given file is: " << total;
    	
    	cout << "\nProgram Terminated. Bye..." << endl;	
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    I assume you can't use existing functionality to do the conversion.

    I'd start by figuring how to do it with math, assuming you can only see each digit. You can convert a digit (e.g. '7') to a number (e.g 7) by subtracting the zero character:
    Code:
    char digitchar = '7';
    int digitval = digitchar - '0';
    // digitval is now 7.
    Once you take that knowledge, you can convert digits and use math to put them in the correct place. For example, if the '7' was the first of three digits, you can convert the character '7' to the number 7 and then multiply by 100 to get 700, then look at the next digit.

    See if you can figure out the entire process, then put it into code.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    Thumbs up

    Quote Originally Posted by Daved View Post
    I assume you can't use existing functionality to do the conversion.

    I'd start by figuring how to do it with math, assuming you can only see each digit. You can convert a digit (e.g. '7') to a number (e.g 7) by subtracting the zero character:
    Code:
    char digitchar = '7';
    int digitval = digitchar - '0';
    // digitval is now 7.
    Once you take that knowledge, you can convert digits and use math to put them in the correct place. For example, if the '7' was the first of three digits, you can convert the character '7' to the number 7 and then multiply by 100 to get 700, then look at the next digit.

    See if you can figure out the entire process, then put it into code.
    Thank you very much your advice was very helpfull

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Reading characters from a string?
    By ladysniper in forum C++ Programming
    Replies: 6
    Last Post: 04-08-2006, 11:45 PM
  4. Replies: 10
    Last Post: 08-17-2005, 11:17 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

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