What can I do so that when the input is a number 1-9 the result will be that input * 100? Currently only an input of 1 will print 100, but I would like to include the range of integers 1-9.
It's my first time coding, so apologies if the question isn't clear.
The problem is in a book I'm reading, but it has no solution in the book. It merely states that I can use an if/else if statement in combination withe the regular AND Boolean operator.
This is the code
//***************************************************************************
// Lab 6.1
//***************************************************************************
char UserInput; // Declare UserInput as a variable of type char
int number = 100; // Declare and initialize number to a value of 100
void setup() // This function executes only once
{
Serial.begin(9600); // Initialize serial communication at a baud rate of 9600
}
void loop() // This function executes repeatedly
{
int result; // Variable to store the output
Serial.println("Enter a number 1-9 in the top box");
while (Serial.available() == 0) // Wait for the user to enter a character
{
; // Do nothing
}
UserInput = Serial.parseInt();
if (UserInput == 1) // Check if the user entered 1
{
result = number * 1; // Compute result
Serial.println(result); // Print the result to the user
}
}
// Copyright 2022 by Musa K. Jouaneh
// All rights reserved. No part of this work, except the program code, may be reproduced, transmitted,
// stored, or used in any form or by any means graphic, electronic, or mechanical, including but not
// limited to photocopying, recording, scanning, digitizing, taping, web distribution, information networks,
// or information storage and retrieval systems without the prior written consent of Musa K. Jouaneh.
// The program code can be used, stored, and executed by the reader for strictly personal use and learning,
// but not for distribution, reproduction, transmission, or selling in any form or by any means.
Sorry, but the code you included does not even do that! How much time have you spent on your problem? Keep at it and you will figure it out! Serial.print() will help you by showing the variable values you are working with.
The following is the only part of your code that checks the user input and takes an action accordingly:
if (UserInput == 1) // Check if the user entered 1
{
result = number * 1; // Compute result
Serial.println(result); // Print the result to the user
}
All this does is to check whether the user entered "1", and if so, to print the result of the calculation 1×100. So the microprocessor has no instructions for what to do if the user enters something other than "1".
Since you're just starting to learn, my suggestion would be that you should experiment and check what happens if you change both occurrences of the numeral 1 to 2, instead:
if (UserInput == 2) // Check if the user entered 1
{
result = number * 2; // Compute result
Serial.println(result); // Print the result to the user
}
What happens now (test inputting all integers 1–9)? How is the behavior different from the original code? By examining the code, why do you think that is?
Next, try using two copies of the if block, one using the numeral 1, and the other using the numeral 2:
if (UserInput == 1) // Check if the user entered 1
{
result = number * 1; // Compute result
Serial.println(result); // Print the result to the user
}
if (UserInput == 2) // Check if the user entered 1
{
result = number * 2; // Compute result
Serial.println(result); // Print the result to the user
}
What happens now (test inputting all integers 1–9)?
Below is the modified code that should theoretically work for any number including 1-9
Basically I converted the char to an int then just directly multiplied the input by the number.
I'm not sure if this is what you were looking for but it should work.
Please let me know if this works for you or not.
//***************************************************************************
// Lab 6.1
//***************************************************************************
char UserInput; // Declare UserInput as a variable of type char
int number = 100; // Declare and initialize number to a value of 100
void setup() // This function executes only once
{
Serial.begin(9600); // Initialize serial communication at a baud rate of 9600
}
void loop() // This function executes repeatedly
{
int result; // Variable to store the output
Serial.println("Enter a number 1-9 in the top box");
while (Serial.available() == 0) // Wait for the user to enter a character
{
; // Do nothing
}
UserInput = Serial.parseInt();
int userIn = toInt(UserInput);
result = number * userIn; // Compute result
Serial.println(result); // Print the result to the user
}
// Copyright 2022 by Musa K. Jouaneh
// All rights reserved. No part of this work, except the program code, may be reproduced, transmitted,
// stored, or used in any form or by any means graphic, electronic, or mechanical, including but not
// limited to photocopying, recording, scanning, digitizing, taping, web distribution, information networks,
// or information storage and retrieval systems without the prior written consent of Musa K. Jouaneh.
// The program code can be used, stored, and executed by the reader for strictly personal use and learning,
// but not for distribution, reproduction, transmission, or selling in any form or by any means.
I know I can repeat that same if statement with each individual digit from 1-9, but the book claims there's a more efficient way of doing it. I tried using the constrain() function but it would always include the lower end of the range with the input.
It sounded like you had no idea how to make this work.
What the hint is the book is alluding to is probably to use a Boolean operation to check if the input is in the range 1–9:
if (UserInput >= 1 && UserInput <= 9) *
if (UserInput >= '1' && UserInput <= '9')
Not sure why they'd suggest to use else if, though (unless you want to print two separate error messages for inputs that are too low and inputs that are too high).
*Edit: As pointed out by @cyber_hacker_134below, the variable UserInput was declared as char, so the code suggestion above had to be corrected.
I can't test right now so I'm trying to do this out of memory and I'm a little rusty.
I though atoi was for char strings so I thought it would work for a normal char but I guess I'm wrong.
"It sounded like you had no idea how to make this work."
That's pretty much the case. I don't know why it's so difficult for me to understand, perhaps I'm just to slow. Any tips on having a better understanding of the material and coding in general would be greatly appreciated.
The book does ask us to check if it is in the proper range, and then says to use an if statement with and And Boolean operator and it looks like you did just that.
void loop() // This function executes repeatedly
{
int result; // Variable to store the output
Serial.println("Enter a number 1-9 in the top box");
while( Serial.available() == 0 ) // Wait for the user to enter a character
{
; // Do nothing
}
UserInput = Serial.read();
if( isdigit(UserInput) && UserInput > '0' )
{
int userIn = UserInput - '0';
result = number * userIn; // Compute result
Serial.println(result); // Print the result to the user
}
}
You're welcome, glad I could help! Feel free to use the below my answer to mark it as the solution.
See my original response:
Experiment — make modifications to the code, and check what happens (even better: try to predict what effect the change will have, and then check if your prediction was correct). Search the internet for solutions to common coding problems, and ask questions if you don't understand how they work. Read programming language references and tutorials for C and C++.