example : input : 23 output : 22333

I need help guys.

Program :

Input : 23
Output : 22333

Code :

‪#‎include‬ <iostream>
using namespace std;

int main (){

char myNumberChar [128];

cout << "Enter numbrers : ";
cin >> myNumberChar;
for (int y = 0; y<=strlen(myNumberChar); y++){
for (int i = 0; i<=myNumberChar[y]; i++)
while (myNumberChar!=NULL){
cout << myNumberChar[y];}
}
}

Question : Why this doesnot work ?

please if u have absolutely other code for this pls give me :) ty
Place [code\] and [/code\] (without the "\") around your code and explain your issue in more detail if you want help.
Last edited on
The address where myNumberChar is will never be NULL, so that while condition will always be true.
1
2
3
4
5
for(int i= 0; i < strlen(myNumberChar); i++) {
  for(int j=0; j < myNumberChar[i] - '0'; j++) {
    cout << myNumberChar[i];
  }
}


This should work.

Your mistakes:

First of all, you have to include <cstring> or <string.h> if you want to use strlen().

In the second loop you compare
 
i <= myNumberChar[y]

When char is converted to int, the returned value equals the position of the char in the ASCII-table. So 1 <= '2' is not 1 <= 2, but 1 <= 50. Safe char->int conversion (assuming char is supposed to represent a number) is done with 'c' - '0'.

And your while loop is unnecessary, and false. Not sure what you tried to accomplish with that.

Finally, you should probably consider to use an int array instead of a char array.
Topic archived. No new replies allowed.