Question
// Sums all digits in a number #include #include // A program that takes in from the user a sequence of numbers separated // by
// Sums all digits in a number
#include
#include
// A program that takes in from the user a sequence of numbers separated
// by + or -, once their sum reaches below zero it prints out "Sum is below zero."
// You need to verify if the character entered is a number or not, and if the sign
// is '+'' or '-'. If not, the user is asked to re-enter till a valid input is taken.
int main(void) {
char userChar;
int sum = 0, sign = +1;
bool number = true;
printf("Enter sequence of character8s with numbers to add: ");
scanf(" %c", &userChar);
do {
if (number) {
while (userChar '9') {
printf("Invalid! Re-enter number. ");
scanf(" %c", &userChar);
}
number = false;
sum += sign * (userChar - '0');
} else {
while (userChar != '+' || userChar != '-') {
printf("Invalid! Re-enter sign. ");
scanf(" %c", &userChar);
}
number = true;
if (userChar == '+') {
sign = +1;
} else {
sign = -1;
}
}
} while (sum
printf("Sum fell below zero. ");
return 0;
}
Part 3 - Debugging NOTE: Please use the debugger to find out the bugs. Kevin, Samantha and Aditya were developing a program that takes in from the user a sequence of numbers separated by + or . Their program would add the numbers depending on the arithmetic operations entered by the user. Once the sum of the numbers fell below zero, the program prints out "Sum fell below zero." They verify if the character entered is a number or not, and they check if the sign entered is ' + ' or ' ' '. If not, the user is asked to re-enter till a valid input is taken. The numbers and signs are separated by . All numbers and signs are entered as characters. Unfortunately, their code does not work, and they hire you to fix their code. You are given the following code
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started