3 Replies - 5145 Views - Last Post: 07 April 2010 - 03:02 PM Rate Topic: -----

#1 Guest_Joe*


Reputation:

Convert char array to int array

Posted 07 April 2010 - 01:44 PM

Hi, I am new to the site and am having trouble with converting a char array in a function and returning it back to main. The program specs call for checking to make sure it is a digit in the function, and then converting each part of array element by element. I have included my code so far. I am not looking for the answer but just for one piece of this so that I can finish the other parts of the program.

//This program will prompt a user for a series of numbers and
//will total them
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <conio.h>
using namespace std;

int converttoInt(char *, int);

int main()
{
const int INPUT_SIZE = 81; // Size of input array
char input[INPUT_SIZE]; //Array to hold numbers
int total=0; //Variable to hold numbers added

cout << "Please input a series of numbers without spaces and I will total them? "<<endl;
cin.getline(input, INPUT_SIZE);
total = converttoInt(input, INPUT_SIZE);

cout << "The total of the inputted numbers are " << total << endl;
_getch();
return 0;
}
int converttoInt(char *strPtr, int size)
{
int total =0;
if (isdigit(strPtr[size]))
{
while (*strPtr != '\0')
{
for (int count = 0; count < size; count++)
total += strPtr[size];
return total;
}
}
else
return 0;
}




Thanks for any help or pointers.

Joe

Is This A Good Question/Topic? 0

Replies To: Convert char array to int array

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Convert char array to int array

Posted 07 April 2010 - 02:09 PM

convert a char array to an int, or convert a char array to an int array because each one is different. If your just trying to convert to an in take a look at stringstream or atoi()
Was This Post Helpful? 0
  • +
  • -

#3 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Convert char array to int array

Posted 07 April 2010 - 02:15 PM

i guess size is the length of the array.
if so, this code is an error:
if (isdigit(strPtr[size]))

remember that in an array of size values, valid indexes are int values between 0 and size - 1.
so the last value in your array shuld be
strPtr[size-1]

now, i think that the way you wrote the function is not right.
it should be arranged that way:
iterate the char array
for every char, check id it is a digit.
if yes, add it to the new int array.
else, continue to the next value.

also, to convert a char to an int, you have to remove the ASCII code, by taking out the value of '0'.
as:
int num = strPtr[i] - '0';

Was This Post Helpful? 0
  • +
  • -

#4 Guest_Joe*


Reputation:

Re: Convert char array to int array

Posted 07 April 2010 - 03:02 PM

Thank you for your help. To answer Imasexy, I am trying to get the input into the char array "Input". Then I am supposed to pass it to the converttoInt function for checking if it is digit and totaling. Then return that back to main.

Thank you very much japanir. I am using your logic in the function and build/testing it to see outcome. That is what I needed was some guidance.

Joe
Was This Post Helpful? 0

Page 1 of 1