Is there any way to convert String s = "1234" to int s[4] = {1,2,3,4}?
and how can i check if it does not contain any non-numeric characters?

thanks=)

Recommended Answers

All 9 Replies

To test the string for non-numerics - loop through the string and call standard library function isdigit() on each character.

To convert to the array - loop through the (numeric) string, for each character subtract '0' and put the result in the corresponding element of the array.

Hi kz07,

MrSpigot explained it very well, but I thought I would add some code, as I would do it..

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

	string s = "0123456789017893469178234691";
	int* numbers = new int[s.length()];
	char currentChar;

Now we have the variables we need.
First we chesk if the string holds anything other than numbers, if not then add the currentChar to the numberlist.

for(int i = 0;i<s.length();i++)
	{
		currentChar = s.at(i);

		if(currentChar<(char)'0' || currentChar>(char)'9')
		{	cout << "error in input string" << endl;
			return -1;
		}
		else
		{
			numbers[i]= (int)currentChar-'0';
		}

	}

Now you should have a int array containing all the numbers.

for(int i = 0;i<s.length();i++)
	{
		cout << "index " << i << " = " << numbers[i] << endl;
	}

	delete numbers;
	return 0;
}

Hope this added to the explanation by MrSpigot.

Have FUN++,

thanks!! i get it now.. =)

wait..
this is my code:

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


using namespace std;

void listTitles();
void displayItem(int []);
void addNewItem(int []);
void save(int [], string, string);

int main(){
    
    
    string name = "NAME ";
    char ans = ' ';    
    int num;
    int pin[4] = {1,2,3,4} ;
    char currentChar;
    int i;
    
    
    cout << "Engineering Journal - " << name << endl << endl;
    
    for(i = 0;i < num.length; i++){
          cout << "Enter your four digit pin: ";
          cin >> num;
          currentChar = num.at(i);

            if(currentChar<(char)'0' || currentChar>(char)'9'){
                     cout << "Invalid Pin" << endl;
             }
    }
//....

im still lost.. =(

" num.at(i) "

num is an integer and not an array. This does not work.
I think what you are trying to do is something like this :

int digits[4] = {0};
int num;
cin >> num;
int i  = 0;
while(num)
{
     digits[i] = num%10; //get the last digits
     num /=10; //remove the last digit
}

Although not tested, I think that will extract the digits from num into
an int array. Then you can convert it into char if you want.

ok.. ill try if it works.
coz, what im trying to do, is to store the pin(which is 1234) in a string first.. then get the user input and check if it doesnt contain any non numeric characters.. after that, ill store that string into an integer array..

I guess you mean all characters other than 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 when you say: any non-numeric character.

Why not just get your number like this:

int num;
cout << "Enter a number: ";

if( !(cin >> num) )
{
    // Reading a number failed, most likely due the fact the user
    // didn't enter a valid number.
    
    /*
         Put your code to handle such a situation here
    */
}

or use a stringstream:

stringstream ss;
char cnum[] = "1234";
int inum

ss << cnum;

if( !(ss >> inum) )
{
    // Again: extracting the number failed
    // Most likely due the fact that you're trying to read
    // something else than a number.
}

You need to include the sstream header in order to make the above snippet compile, you can do this by adding the following include directive to your program: #include <sstream> .

if(currentChar<(char)'0' || currentChar>(char)'9'){
   cout << "Invalid Pin" << endl;
}

Explicitly casting to a char isn't even needed here, the following code is equivalent:

if(currentChar<'0' || currentChar>'9'){
   cout << "Invalid Pin" << endl;
}

thanks again! =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.