Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// Sums all digits in a number #include #include // A program that takes in from the user a sequence of numbers separated // by

image text in transcribed

// 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security

Authors: Alfred Basta, Melissa Zgola

1st Edition

1435453905, 978-1435453906

More Books

Students also viewed these Databases questions