Serial Monitor Entry Issues

Hey guys! I’m trying to create a program where I multiply a certain amount of numbers together (decided by the user in Serial Port) and then the user enters in the numbers to be multiplied. Right now all I can do is enter the number of numbers I want to multiply, and it displays that number in 0’s. Could I get some help?

I’ve seen some posts already and, yes, I have the code attached.

MultiplyingNumbersArduino.ino (2.29 KB)

You should read now the instructions at the top of the page that you should have read before posting a question. Especially, how to post code with code tags:
Read this before posting a programming question ...

Your code is short. Post it with code tags so it looks like this:

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
}
float num1 = 0;                //assigns num 1 a starting value and data type
float num2 = 0;                //assigns num 2 a starting value and data type
int flag1 = 0;                 //assigns flag1 a starting value and data type

int numOfNums;
String inputString = "";                    // a string to hold incoming data
boolean stringComplete = false;             // whether the string is complete


void setup() {                             //Sets up the program to be looped
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
Serial.println("How many values do you want to multiply?");    //Prompts user to print out first  
Serial.println(inputString);                                                               //number
}

void loop() {
numOfNums = inputString.toInt();
inputString = "";  

//print the string when a newline arrives:
if (stringComplete) {                     //If the string is entered into the serial prompt
  if (num1 == 0) {
    Serial.println("Enter the first number: ");
    Serial.println(inputString);
    num1 = inputString.toFloat();
    inputString = "";
  }
  for (int i=0; i < numOfNums; i++) {
    Serial.println("Enter the next number: ");
    num2 = inputString.toFloat();
    num1 = num1 * num2;
    inputString = "";
    Serial.println(num1);
  }
  
  //clear the string:                                  
  stringComplete = false;             //Changes stringComplete value to false
}
}



/*
 * SerialEvent occurs whenever a new data comes in the hardware serial RX.
 * This routine is run between each time loop() runs, so using delay inside
 * loop can delay response. Multiple bytes of data may be available.
 */
 void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    //add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag so the main loop 
    //can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
 }

I'm allergic to Strings. User input is char array. Want to stay with that, then I help....

void loop() {
numOfNums = inputString.toInt();
inputString = "";

When loop first gets called, inputString is "". What value are you expecting in numOfNums?

// reserve 200 bytes for the inputString:
inputString.reserve(200);

If you KNOW the upper limit for the embedded string, and there is room for that embedded string, then you are an idiot for using the String class to wrap the string.

Ditch the asynchronous serialEvent() method. Deal with serial data, as it arrives, in loop(), like a real programmer.

The user of your program should NOT have to define how many numbers to multiply. The user should be able to type "14.6 * 27.3 * -14.76564 =" and get a result. YOU should collect all the data in a char array, adding a NULL terminator each time you add a letter to the array, stopping when you encounter the = (also known as an end-of-packet marker).

When you do encounter the end of packet marker, call a function to compute the result.

Your program won't then be limited to just multiplication. You could easily handle addition, subtraction, and division. With a tiny bit of thought, the compute function could be made to handle parentheses, factorial, modulo, exponentiation, etc.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

And there is a simple user input example in Planning and Implementing a Program

...R

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

And there is a simple user input example in Planning and Implementing a Program

...R

Oh, yeah. I meant to include that link.