Hi, so I'm having issues with storing the 6 integers into the array and then the multiplying/summation of them at the end. The code is meant to Prompt the user to enter 6 signed integers. Scold if anything other than an integer and re-prompt until the user has entered six. Allow users to enter "x" to start again. After receiving 6 integers query the user for "sum" or "product". Begin the program again after printing the result. And the code is:
// integer read from user
int userInt;
int i = 0;
// character array read from user
char userArray[] = {0, 0, 0, 0, 0, 0};
void setup() {
// Set up the serial port, baud rate argument can be changed
// to others supported by serial monitor
Serial.begin(9600);
}
void loop () {
// prompt the user
int intCount = 0;
while (intCount < 6) {
Serial.println("Enter six signed but non-zero integers, or 'x' to quit"); //change to x eventually
while (Serial.available() == 0) {
delay(20); //Seems to work better when delaying a tiny bit between reads
}
if (Serial.peek() == 'x' || Serial.peek() == 'X') {
// Now, read and ignore all characters from the serial monitor
// Allows user to type qzw134ks5 and still quit without leaving zw134ks5
// in the buffer for the next read
while (Serial.available() > 0) {//had this at start of og code and it just deletes available char
Serial.read(); //Ignoring the returned value just empties the queue of characters
}
Serial.println("OK, quitting");
intCount = 6;// exit this loop
} else {
userInt = Serial.parseInt(); // if not 'q' read code
intCount++; //counter +1
if (userInt == 0) {
Serial.println("You did not enter a valid integer or entered zero.");
intCount--;// so that you dont have to start over return counter as this input was invalid
} else {
Serial.print("Integer number "); Serial.print(intCount); Serial.print(" is ");
Serial.println(userInt);// print each number as it appears
userInt = userArray[intCount];
}
}
}
for (i = 0; i < 5; i++) {
Serial.println(userArray[i]);
}
Serial.println("Sum or Product?");
Serial.println("For Sum type 's' or 'S'");
Serial.println("For Product type 'p' or 'P'");
while (Serial.available() == 0) {
delay(20); //Seems to work better when delaying a tiny bit between reads
}
while (Serial.available() > 0) {
if (Serial.peek() == 'x' || Serial.peek() == 'X') {
// Now, read and ignore all characters from the serial monitor
// Allows user to type qzw134ks5 and still quit without leaving zw134ks5
// in the buffer for the next read
while (Serial.available() > 0) {
Serial.read(); // Ignoring the returned value just empties the queue of characters
}
Serial.println("OK, quitting");
intCount = 6;
}
if (Serial.readString() == 'p' || Serial.readString() == 'P') { //if user chooses product
int p = 1;
//set p to 1
for (i = 0; i < 6; i++) {
p *= userArray[i];
//(1)*int1*int2...
}
}
if (Serial.readString() == 's' || Serial.readString() == 'S') { // if user chooses sum
int s = 0;
//set s to 0
for (i = 0; i < 6; i++) {
s += userArray[i];
//(0) + each number in array for all 6 spots ( 0 + int1 +int2...)
}
}
else {
Serial.println("invalid");
}
}
}
please help and please don't be rude I am new to Arduino.
I see several issues with this code:
-
Serial.parseInt() returns a long so I would suggest making all arrays and temporary variables of type long
-
The following assignment is backwards:
userInt = userArray[intCount];
- You are incrementing intCount too early causing userArray[0] not to be populated.
userInt = Serial.parseInt(); // if not 'q' read code
intCount++; //counter +1
if (userInt == 0) {
Serial.println("You did not enter a valid integer or entered zero.");
intCount--;// so that you dont have to start over return counter as this input was invalid
} else {
Serial.print("Integer number "); Serial.print(intCount); Serial.print(" is ");
Serial.println(userInt);// print each number as it appears
userInt = userArray[intCount];
}
- The following code has several issues:
if (Serial.readString() == 'p' || Serial.readString() == 'P') { //if user chooses product
int p = 1;
//set p to 1
for (i = 0; i < 6; i++) {
p *= userArray[i];
//(1)*int1*int2...
}
}
if (Serial.readString() == 's' || Serial.readString() == 'S') { // if user chooses sum
int s = 0;
//set s to 0
for (i = 0; i < 6; i++) {
s += userArray[i];
//(0) + each number in array for all 6 spots ( 0 + int1 +int2...)
}
}
else {
Serial.println("invalid");
}
}
- You are comparing String against char
- You need to do a Serial.readString() into a temporary String variable and then do all your compares.
- You should restructure this into if/elseif
- This should be i<6 not i<5
for (i = 0; i < 5; i++) {
Serial.println(userArray[i]);
}
Here is a version with the above corrections. There are still issues handling 'x' (quit) but I'm not sure exactly how you want that to work.
// integer read from user
long userInt;
int i = 0;
// integer array read from user
long userArray[] = {0, 0, 0, 0, 0, 0};
void setup() {
// Set up the serial port, baud rate argument can be changed
// to others supported by serial monitor
Serial.begin(9600);
}
void loop () {
// prompt the user
int intCount = 0;
while (intCount < 6) {
Serial.println("Enter six signed but non-zero integers, or 'x' to quit"); //change to x eventually
while (Serial.available() == 0) {
delay(20); //Seems to work better when delaying a tiny bit between reads
}
if (Serial.peek() == 'x' || Serial.peek() == 'X') {
// Now, read and ignore all characters from the serial monitor
// Allows user to type qzw134ks5 and still quit without leaving zw134ks5
// in the buffer for the next read
while (Serial.available() > 0) {//had this at start of og code and it just deletes available char
Serial.read(); //Ignoring the returned value just empties the queue of characters
}
Serial.println("OK, quitting");
intCount = 6;// exit this loop
} else {
userInt = Serial.parseInt(); // if not 'q' read code
if (userInt == 0) {
Serial.println("You did not enter a valid integer or entered zero.");
} else {
Serial.print("Integer number "); Serial.print(intCount); Serial.print(" is ");
Serial.println(userInt);// print each number as it appears
userArray[intCount++] = userInt;
}
}
}
for (i = 0; i < 6; i++) {
Serial.println(userArray[i]);
}
Serial.println("Sum or Product?");
Serial.println("For Sum type 's' or 'S'");
Serial.println("For Product type 'p' or 'P'");
while (Serial.available() == 0) {
delay(20); //Seems to work better when delaying a tiny bit between reads
}
while (Serial.available() > 0) {
if (Serial.peek() == 'x' || Serial.peek() == 'X') {
// Now, read and ignore all characters from the serial monitor
// Allows user to type qzw134ks5 and still quit without leaving zw134ks5
// in the buffer for the next read
while (Serial.available() > 0) {
Serial.read(); // Ignoring the returned value just empties the queue of characters
}
Serial.println("OK, quitting");
intCount = 6;
}
String oper = Serial.readString();
if (oper == "p" || oper == "P") { //if user chooses product
long p = 1;
//set p to 1
for (i = 0; i < 6; i++) {
p *= userArray[i];
//(1)*int1*int2...
}
Serial.print("Product = ");
Serial.println(p);
} else if (oper == "s" || oper == "S") { // if user chooses sum
long s = 0;
//set s to 0
for (i = 0; i < 6; i++) {
s += userArray[i];
//(0) + each number in array for all 6 spots ( 0 + int1 +int2...)
}
Serial.print("Sum = ");
Serial.println(s);
}
else {
Serial.println("invalid");
}
}
}
Thank you this is very informative and helpful. However, now when running the program it is never-ending and doesn't tell what number you are on i.e. 1-6. I am thinking this may have something to do with the userArray[intCount++] = userInt?
The 'x' is meant to be a way to restart the program if the user wants to quit and enter different values for integers 1-6 in the userArray.
This is the output I got when I ran my version:
Enter six signed but non-zero integers, or 'x' to quit
Integer number 0 is 1
Enter six signed but non-zero integers, or 'x' to quit
Integer number 1 is 1
Enter six signed but non-zero integers, or 'x' to quit
Integer number 2 is 1
Enter six signed but non-zero integers, or 'x' to quit
Integer number 3 is 1
Enter six signed but non-zero integers, or 'x' to quit
Integer number 4 is 1
Enter six signed but non-zero integers, or 'x' to quit
Integer number 5 is 1
1
1
1
1
1
1
Sum or Product?
For Sum type 's' or 'S'
For Product type 'p' or 'P'
Sum = 6
I realized that I did not delete a line before, sorry. but now when I run the program it still does not wait for input and I cannot do either function at the end. It reads:
Enter six signed but non-zero integers, or 'x' to quit
You did not enter a valid integer or entered zero
integer number 0 is 1
Enter six signed but non-zero integers, or 'x' to quit
You did not enter a valid integer or entered zero
integer number 1 is 1
and so on until the end where it reads them back correctly but does not wait to choose sum or product and decides it is invalid before restarting.
It works for me. Check your line ending in your serial monitor.
Yep, that was it! Thank you so much