Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easy Question pls Help

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have just started with C programming, but i am stuck, please help.

I want to seperate user input and feed it to a function ie.
User inputs 2756,(up to 8 letter long)
i would like the number to split up into "2" "7" "5" and "6"
and to be feed into function ie,

add(2); followed by 7,5,6
i want to aviod writting lots of Scanf and Add() statements.
Please Help
 
char szBuf[10];
scanf("%s",szBuf);
for (int i=0;i < strlen(szBuf);i++)
{
if (i >=8 ) break;
add(szBuf - '0');
}
 
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#define _maxsize_ 8
int exp( int a )
{
int result = 1;
for( int i = 0; i < a; i++ )
result = result * 10;
return result;
}
void main()
{
long number = 0;
int digit[_maxsize_];
char Number[_maxsize_];
int k = 0;
cout<<&quot;Enter a number: &quot;;
cin>>Number;
int size = strlen( Number );
if( size > _maxsize_ )
{
cout<<&quot;This number is too big !&quot;<<endl;
cout<<&quot;Only numbers with a maximum of 8 digits can be break by this program.&quot;<<endl;
exit(0);
}
number = atol( Number );
for( int j = size; j > 0; j-- )
{
digit[k] = number/exp( j - 1 );
number = number % exp( j - 1 );
k++;
}
for( int i = 0; i < size; i++ )
{
cout<<&quot;digit[&quot;<<i<<&quot;] = &quot;<<digit;
if( i != size - 1 )
cout<<&quot; ,&quot;;
}
cout<<endl;
}
 
By the way this code is for separating the user input.
Example,if the user input is: 2756,all of the digits of this number will be stored separately in a table.
So you would have: digit[0] = 2,digit[1] = 7,digit[2] = 5,digit[3] = 6.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top