Annoying Problem with Char Array to Int conversion

---------UPDATED---------
Thanks guys. I just solved the problem. I clarify here again, the string actually prints out, but the integer of it does not. But I still used atoi () function after I initialize the last element of the array to null value- ‘\0’, then the string is complete. And because every time it goes through the loop, it reads the value, so every time there will be a char which is 3 would be added into the string, and I manually convert it to blank space, so when the string is converted into a integer, the space would be ignored.

Thanks for everyone’s help.


Hi Guys, now I’m writing a code which could read input from Serial Monitor and convert the input into an integer. EXP: enter:“1”,“2”,“3”, hit return, then then there would be an integer which is 123.

I read from the Serial.read, and then put every single char into a char array, then I wanna convert the char array into an integer.

But every time the integer printed out as 0. This is where problem at. I think I failed to convert the array into an integer.

Can you guys help me? :stuck_out_tongue:

boolean done=false;
int bsInt=0;
char bigStr[30];
int inputCents;
void setup(){
  Serial.begin(9600);
  welcome();
  done=false;
  bsInt=0;

}
void loop(){
  yifunc(readSerial());//take the return value of input to store it into a string

}

//Functions

//continually checks on serial-in for characters, printing them if there are something
char readSerial() {
 if (Serial.available() > 0) {    //>>>>>> UPDATED: while was replaced by if
    char inputChar;
    inputChar = Serial.read();
    Serial.println("RAW");
    Serial.println(inputChar);
    return inputChar;
  }
  Serial.println('x');
}

// takes a char argument and adds it to a global string array of size [10] until the array is full or \n is hit

void yifunc(char x){
  Serial.println('y');
  char inputC=x;
  if(!done&&bsInt<30){

    if (inputC=='\n'){
      done=true;
      Serial.println(bigStr);
      Serial.println("RETURN HIT");
      return;
    } 
    if(inputC=='c'){ 
      setup();
      Serial.println("C HIT");
      return;
    }      
    bigStr[bsInt]=inputC; 
    bsInt++;
    bigStr[bsInt]='\0';
    Serial.println('z');
    Serial.println(bigStr);
    Serial.println("cents");
inputCents=atoi(bigStr);
    Serial.println(inputCents);
    delay(2000);
  }
}

You may benefit from reading Serial input basics and everyone would benefit if you used code tags instead of putting your code in quotes. See read this before posting a programming question

Also read about Serial.readBytesUntil() and atoi().

  while (Serial.available() > 0) {
    char inputChar;
    inputChar = Serial.read();
    Serial.println("RAW");
    Serial.println(inputChar);
    return inputChar;

This is nonsense. There is no excuse for bastardizing a while statement like this. This while loop will iterate 0 or 1 times. NEVER any more than that.

char readSerial() {
  while (Serial.available() > 0) {
    char inputChar;
    inputChar = Serial.read();
    Serial.println("RAW");
    Serial.println(inputChar);
    return inputChar;
  }
  Serial.println('x');
}

What does this function return if there is no data to read? NOTHING. And that is WRONG!!!

At present moment I am receiving 0 nonstop…

At present moment I am receiving 0 nonstop..

No. You are receiving nothing BECAUSE that function is flawed. The function says that it returns a char. That means that EVERY path through the function MUST encounter a return statement. 99.9% of your calls to the function do NOT encounter a return statement, so the return value is garbage.

  char inputC=x;

This is silly.

    if(inputC=='c'){ 
      setup();

And this is way beyond silly. You should NEVER call setup().

    bigStr[bsInt]=inputC; 
    bsInt++;
    bigStr[bsInt]='\0';
    Serial.println('z');
    Serial.println(bigStr);
    Serial.println("cents");
inputCents=atoi(bigStr);

At most, your string will contain one character. How is that useful?

You're making this way too hard. See if this helps:

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

void loop() {
  int number;
  int charsRead;
  char temp[6];
  
  if (Serial.available() > 0) {
    charsRead = Serial.readBytesUntil('\n', temp, 5);
    temp[charsRead] = '\0';                                         // Make it a string
    number = atoi(temp);
    Serial.println(number);
  }
}

Paul, I didn't quite get you. The string actually respond to what I'm adding to it. So the function actually returns char every time it runs. Can you clarify on that? And I called setup() because I want to reset the whole program when c is entered.

Paul

PaulS: No. You are receiving nothing BECAUSE that function is flawed. The function says that it returns a char. That means that EVERY path through the function MUST encounter a return statement. 99.9% of your calls to the function do NOT encounter a return statement, so the return value is garbage.

  char inputC=x;

This is silly.

    if(inputC=='c'){ 
      setup();

And this is way beyond silly. You should NEVER call setup().

    bigStr[bsInt]=inputC; 
    bsInt++;
    bigStr[bsInt]='\0';
    Serial.println('z');
    Serial.println(bigStr);
    Serial.println("cents");
inputCents=atoi(bigStr);

At most, your string will contain one character. How is that useful?