problem:
the user will input an integer for example 12345 (5 digits) and the output should be:

12345
12345
12345
12345
12345
// depending of how much digit you input

or if the user will input lets say 123hi and the output should be

123hi
123hi
123hi
// the program will only acknowledge integer input digits

One way to do it (which is a sucky way really) is to compare each character of your string (string[0], string[1], etc) with numbers from 0 to 9 in a for loop.

Quickly..
(for i=0; "condition here"; i++)
if(string == '0' || string == '1'... )
counter++;

Really lame way to do it.. But that's more than you deserve for not even trying.

Yes, by comparison works good, but making the match beteween characters and integers. Codes for numbers 0 to 9 are ASCII 48 to 57. Other way could be making a switch(string with cases, case '1': cases '2': etc... We find end of string at the '\0' character.

By no comparison, you can use isdigit(int).

Be careful no writing more than 150 characters on this example or it will crash everything (C++ is a good bulldozer, but can become a gun).


#include <STDIO.H>>
#include <CONIO.H>

void main(void){
char string[150];  //here you put your own extension for string, now, no more than 150 characters
clrscr();
printf("Enter string:\n");
gets(string);
printf("\n");
int i=0,j,dig=0;
while(string[i]!='\0')
	{
	for(j=48;j<=57;j++)
		{
		if((int)string[i]==j) dig++;
		//printf("%i,%c\n",(int)string[i],(char)j); //ADD IF YOU WANT TO SEE HOW COMPARISON WORKS
		}
	i++;
	}
for(j=0;j<dig;j++)
printf("%s\n",string);
getch();
}

Edited 6 Years Ago by jorgempaz: n/a

Comments
We do NOT give answers to problems, especially with bad code and dangerous and inappropriate commands.

Wow, what's up with the solutions here..

#include <iostream>
using namespace std;

int main (int argc, char * const argv[]) {
	string inputx;
	cin >> inputx;
	
	
	int numCounter;
	
	for (int i=0; i<inputx.length(); i++) {
		
		if ( isdigit (inputx[i] )) {
			numCounter++;
		}
		
	}
	
	for (int i=0; i < numCounter; i++)
	{
		cout << inputx << endl;
	}
	
	
	return 0;
}
Comments
Solutions are inappropriate. We do NOT give answers to problems, we help them find their own solutions
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
	int count=0; 
	string str;
	cout<<"Enter string: ";	
	cin >> str;
	for(int y=0;y<str.length();y++)
	{
		if (isdigit(str[y])) /* or if((int)str[y]>47 && (int)str[y]<58) */
		{
			
			cout<<str<<endl;
		}
	}
		return 0;
}
Comments
Did you read the reputation comments on the two post above??
This article has been dead for over six months. Start a new discussion instead.