I want to print only the last value of the for statement in the code below.

Asked 2 years ago, Updated 2 years ago, 148 views

#pragma warning (disable:4996)
#include<stdio.h>
int main(void) {
    int cnt = 0, digt_cnt = 0, i, total = 0;
    char c;
    while (c = getchar()) {
        if (c == 'q')
            return 0;
        ++cnt;
        {if (c >= '0' && c <= 9) ++digt_cnt;
        i = 1;
        while (i++ <= c) {
            if (c == "B")
                break;
                total += 1;
            printf("input = %c and total = %d\n", c, total);
        }
        }
        return 0;
    }
}

A program in which ASCII code values are output when characters are entered. However, if you enter A when it is output, for example,

input = A and total = 1

input = A and total = 2

input = A and total = 3

..........

input = A and total = 65

That's what it says. I just want to print out the last value, what should I do?

c for

2022-09-22 20:09

1 Answers

while (i++ <= c) {
            if (c == "B")
                break;
                total += 1;
            printf("input = %c and total = %d\n", c, total);
        }

= >

while (i++ <= c) {
            if (c == "B")
                break;
                total += 1;
        }
    printf("input = %c and total = %d\n", c, total);

I think this will work.


2022-09-22 20:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.