I have to input whole numbers and the program has to stop if I put in a negative number. It has to sum up every whole number I've inputed that has a "7" in it, but for some reason, it counts "70" as "7". I'm quite new with programming and I'm stuck on this.
#include <stdio.h>
int main() {
int number = 1, temp_var = 0, sum = 0;
while (number >= 0) {
printf("Put in a number: ");
scanf_s("%d", &number);
while (number >= 1) {
temp_var = number % 10;
if (temp_var == 7)
{
sum = sum number;
break;
}
number = number / 10;
}
}
printf("%d", sum);
return 0;
}
CodePudding user response:
Your code is incorrect: you modify number
in order to test if it has 7
among the digits of its base 10 representation so you do not add the original value if the test succeeds. You should use a separate variable to test the digit values.
The termination test if number
is negative works, but is not straightforward for the casual reader.
Here is a modified version:
#include <stdio.h>
int main() {
int number, temp_number, sum = 0;
for (;;) {
printf("Put in a number: ");
if (scanf("%d", &number) != 1 || number < 0)
break;
for (temp_number = number; temp_number; temp_number /= 10) {
if (temp_number % 10 == 7) {
sum = sum number;
break;
}
}
}
printf("%d\n", sum);
return 0;
}
CodePudding user response:
You are modifying number
then adding that modified value when the least significant digit becomes zero (so 70 adds 7, while 770 or 771 for example would both add 77).
You should retain the original input value and add that whilst modifying a copy of the original to detect the digit 7.
Alternatively accept a string input, scan it for '7' characters and if so convert to an integer and add it. For example:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
int main()
{
char input[16] = "" ;
int sum = 0;
bool got_input = false ;
do
{
got_input = scanf_s( "%s", input, sizeof(input) ) != 0 ;
for( const char* scan = input;
got_input && *scan != 0 && isdigit((int)(*scan));
scan )
{
if( *scan == '7' )
{
sum = atoi( input ) ;
break ;
}
}
}
while( got_input && isdigit((int)(*input)) ) ;
printf("Sum: %d", sum );
return 0;
}
If the input starts with any non-digit/non-whitespace character the input loop terminates. Trailing non-digit characters in input are ignored.
CodePudding user response:
There are some issues:
- You are looping on
number
and it eventually becomes zero. - When you try to add to the
sum
, you want the original value ofnumber
but you're using the partially modified value. - You want to preserve the original value and use a temp copy in the loop.
- The
while (number >= 0)
in the outer loop won't stop immediately if a negative number is executed (i.e. an extra loop iteration is performed). We want to use anif
after thescanf_s
. - Because
number
gets trashed by the inner loop, the outer loop will only execute once.
Here's the refactored code. It is annotated:
#include <stdio.h>
int
main(void)
{
int number = 1,
temp_var = 0,
sum = 0;
// NOTE/BUG: this will loop once too often
#if 0
while (number >= 0) {
#else
while (1) {
#endif
printf("Put in a number: ");
scanf("%d", &number);
// NOTE/FIX: correct way to stop on negative number
#if 1
if (number < 0)
break;
#endif
// NOTE/FIX: to prevent "number" from being trashed, we should use a temp
// value in the loop
int temp_number = number;
while (temp_number >= 1) {
temp_var = temp_number % 10;
if (temp_var == 7) {
sum = sum number;
break;
}
temp_number = temp_number / 10;
}
}
printf("%d\n", sum);
return 0;
}
In the code above, I used cpp
conditionals to denote old vs. new code:
#if 0
// old code
#else
// new code
#endif
#if 1
// new code
#endif
Here's a slightly cleaned up version that puts the digit match code in a function:
#include <stdio.h>
// hasdigit -- decide if a given number contains a given decimal digit
int
hasdigit(int number,int dig)
{
int temp_var;
int match = 0;
while (number != 0) {
temp_var = number % 10;
match = (temp_var == dig);
if (match)
break;
number = number / 10;
}
return match;
}
int
main(void)
{
int number;
int sum = 0;
while (1) {
printf("Put in a number: ");
scanf("%d", &number);
if (number < 0)
break;
if (hasdigit(number,7))
sum = number;
}
printf("%d\n", sum);
return 0;
}
CodePudding user response:
Here is a slightly different take. Typically testing for a 7 in number would be done by a separate function. Since it appears that you have not yet covered functions in your studies all of the code is included in the main function.
/* Question: input numbers; stop on a negative #; sum numbers with a "7" */
#include <stdio.h>
int main (void)
{
printf("\n");
printf("enter a number (negative number to exit): ");
int number;
scanf_s("%d", &number);
int sum = 0;
while (number >= 0) {
int power = 1; // mult of 10 matching left-most digit (also a loop counter)
while (number / power > 9) {
power *= 10;
}
int temp = number; // save current value of number
do {
// if find a 7 then add number to sum and exit this do loop
if (temp / power == 7) {
sum = number;
break;
}
temp %= power; // drop left-most digit
power /= 10; // adjust multiple of 10
} while (power > 0);
printf("enter a number (negative number to exit): ");
scanf_s("%d", &number);
}
printf("sum = %d\n", sum);
return 0;
}