[SOLVED] Char append ?

Trying to build up a string of ##### depending on a number input
However the result is always one # and it overwrites the initial :..

    char tmpChar = 58;
    for(int i=1; i <= keyValue; i++)
    {
      tmpChar =+ 35;
    }

so I'm trying to , for each keyValue add another # to the line

Do i have to turn it into an array to make it work ?

like this ? but I want to use the entire thing afterwards as a line.

    char tmpChar[8]; tmpChar[0] = 58;
    for(int i=0; i <= keyValue; i++)
    {
      tmpChar[i] =+ 35;
    }
    display.println(tmpChar[SHOW ALL THE VALUES!!!!!]);

Yes.

C strings that you should use in small memory environments are char arrays where the text is ASCII coded characters ending in a terminating 0 (ASCII NULL)

Please avoid C++ String objects with Arduino. They behave badly. You can get away with using them but it's a bad habit to learn. People who program PCs with loads of RAM use them and bring those habits along but that doesn't make it a good idea on Arduinos, not at all!

You could do something like this.

    const byte buflen = 10;
    char buf[ buflen ];

// ...............

    if ( keyValue < buflen - 1 )
    {
        for (byte i = 0; i <= keyValue; i++)
        {
            buf[ i ] = '#'; // '#' is ASCII 35 in easy to read form
        }
        buf[ i ] = 0;
    }

Note that with C strings you have to check limits or make sure that your code never exceeds them. That's only a minus if you don't think that hardware awareness is a good thing.

With C strings, the data stays where I put it and only uses the space I give it.

BTW, what do you need the string of #'s for? If it's for serial/print output then skip the buffering.

        for (byte i = 0; i <= keyValue; i++)
        {
            Serial.write( '#' );
        }

I'd suggest the key to this riddle is what is being held in keyValue. Whatever it is, it's likely different to what you expect. I also see no evidence of you attempting to terminate the string before printing it.

http://snippets-r-us.com/

KenF:
I'd suggest the key to this riddle is what is being held in keyValue. Whatever it is, it's likely different to what you expect.

well the keyValue is just a number from 0-9, it's just the ammount of #'s i want in my ... ermm thing.

GoForSmoke:
BTW, what do you need the string of #'s for? If it's for serial/print output then skip the buffering.

It's a learning process, for home use.

I got one of those keypads, hooked it up with a bunch of stuff, see it here : Imgur: The magic of the Internet

Then i have a login screen, my A,B,C,D is menu selectors, and on the login screen,
Im displaying # to hide the actual numbers, it means nothing, but it's feels right.

so first time user hits the loop, by pressing a number, it's :#, second time :##, and so on.

I was previously using a

switch(keyValue)
 case 1:
 display.println(" #");
 break;
 case 2: 
 display.println(" ##");
 break;
//........ and so on up to 9

and I was trying to change this switch into a more fluent while/for loop
The switch works fine, but changing to the for actually saved me some space.

I have allready switched some things to hardware to save additionally,
like the mosfet, is also hooked up to a pnp transistor to indicate if the motor is running,
while the mosfet is doing the motor controls.

You can create plugins for the stream/print class easily.

Here is a different method for printing a character multiple times:

struct Repeater : Printable{
    Repeater( char el, char count ) : el(el), count(count) {}

    size_t printTo(Print& p) const{    //<< this is a function inherited from 'Printable'
      for( char idx = 0 ; idx < count ; ++idx ) p.write( el );
    }
    char el;
    char count;
};

And you use it just like a function call inside the print/println:

display.print( Repeater( '#', 7 ) );

This will print '#######'.

As it works with the Print library, you can use it with many different libraries ( Serial, I2C, Ethernet/WiFi, LiquidCrystal, SD, ... )

Serial.print( Repeater( '!', 4 ) );
client.print( Repeater( '~', 27 ) );

It also will allows some creative ideas :sunglasses: :

void setup() {
  Serial.begin( 9600 );

  for( char v = 0 ; v < 18 ; ++v  ){
    switch( v ){
      case 13 ... 14:
        Serial.print( Repeater( ' ', 11 ) );
        Serial.println( Repeater( '|', 3 ) );
        break;    
      case 15 ... 17:
        Serial.print( Repeater( ' ', 10 ) );
        Serial.println( Repeater( '@', 5 ) );
        break;
      default:
        Serial.print( Repeater( ' ', 12 - v ) );
        Serial.println( Repeater( v % 3 ? '#' : '*', 2 * v + 1 ) );      
    }
  }
}

void loop() {}

pYro_65:
You can create plugins for the stream/print class easily.

struct Repeater : Printable{

Repeater( char el, char count ) : el(el), count(count) {}

size_t printTo(Print& p) const{    //<< this is a function inherited from 'Printable'
      for( char idx = 0 ; idx < count ; ++idx ) p.write( el );
    }
    char el;
    char count;
};




And you use it just like a function call inside the print/println:

*display.print( Repeater( '#', 7 ) );*

This will print '*#######*'.

You sir, are a life saver!

Thank you, sending some karma your way.
Was about to order a mega 2560 just for the extra memory :smiley: