Sum Digits & Exit with 'Q': A Program Solution

  • Thread starter Thread starter DiamondV
  • Start date Start date
  • Tags Tags
    Program Sum
AI Thread Summary
The program reads characters from the keyboard, sums only digit characters, and exits when 'Q' or 'q' is entered. The code correctly uses a while loop with an AND condition to continue processing input until either 'Q' or 'q' is detected. The logic behind this is that if the input is neither 'Q' nor 'q', the program should continue to sum digits. Using an OR condition would lead to an immediate exit since any character would satisfy one of the conditions. The explanation clarifies that the logical expression effectively checks for the absence of both exit conditions to keep the loop running.
DiamondV
Messages
103
Reaction score
0

Homework Statement


Write a program that reads in any characters from the keyboard, sums up only characters corresponding to a digit and prints the result on the screen. The program will exit when the character 'Q' is entered (upper or lowercase).

Example:
Input = 9 8 q
Output = The total is 17.

Homework Equations

The Attempt at a Solution


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(){
char input;
int total = 0;
scanf("%c", &input);
while(input!='q' && input!='Q'){
if(input>'9' || input<'0'){ //Input is not a valid integer
printf("Input is not a digit\n");
}
else{//Input is a valid integer
//ASCII value for 0 is 48 .
total = total + (input-48); // Takes previous total and adds new valid integer onto it
}
scanf(" %c", &input);//Scans for new Input
}
printf("The total is %d\n", total);



return 0;
}This is my code that works perfectly, I don't understand one thing though. At the line that is bold I have said that while the input is not q AND not Q. Initially I used a || there, so it was while the input is not Q or q, that didnt work. Its not working as an OR(||) but its working as AND(&&). Why is that? Surely the input can't be Q and q at the same time? I mean only 1 would need to appear to stop the code, so why is it && and not ||.
 
Physics news on Phys.org
If input is not 'Q' AND it's not 'q' then it must be something you want to process further.

If you used OR then you might as well have no test because your Boolean will always be TRUE. Every character you nominate will test true on one side or the other in each "not something" condition, meaning both expressions can never be simultaneously false.
 
Last edited:
Your logic is such that
Code:
if (input == 'q' || input == 'Q)
your program should exit. In your loop, if the logical expression above is false, you want the program to proceed with another iteration of the loop.
In other words, if the expression ~(input == 'q' || input == 'Q') is false. Due to one of de Morgan's laws, ~ (P || Q) is equivalent to (~P && ~Q), where P and Q represent the logical expressions that make up your test. In your case, ~(input == 'q' || input == 'Q') is equivalent to (input != 'q' && input != 'Q').
 

Similar threads

Back
Top