错误:未使用计算值 [-Werror=unused-value]

发布于 2025-01-17 04:59:50 字数 844 浏览 0 评论 0原文

我编写了这段代码来尝试读取单词中是否有重复的字母,但我不断遇到此错误:

错误:未使用计算的值[-Werror = unused-value]

有问题的行是:

arr[(int)(str[i])++];

整个代码是:

#include<stdio.h>
#include<string.h>
int main()
{
    char str[30];
    printf("Enter your String:");
    scanf("%[^\n]",str);
    int i;
    int arr[256]={0};
    for(i=0;i<strlen(str);i++)
    {
        if(str[i]==' '){
            continue;
            arr[(int)(str[i])++];
        }
    }
    printf("Repeated character in a string are:\n");
    for(i=0;i<256;i++)
    {
        if(arr[i]>1)
        {
          printf("%c occurs %d times\n",(char)(i),arr[i]);
        }}
        return 0;
}

这是来自控制台的错误消息: https://i.sstatic.net/GpMOW.png

感谢任何帮助:)

Ive written this code to try and read if there are any duplicate letters in a word, but I keep coming across this error:

Error: value computed is not used [-Werror=unused-value]

The line in question is:

arr[(int)(str[i])++];

The whole code is:

#include<stdio.h>
#include<string.h>
int main()
{
    char str[30];
    printf("Enter your String:");
    scanf("%[^\n]",str);
    int i;
    int arr[256]={0};
    for(i=0;i<strlen(str);i++)
    {
        if(str[i]==' '){
            continue;
            arr[(int)(str[i])++];
        }
    }
    printf("Repeated character in a string are:\n");
    for(i=0;i<256;i++)
    {
        if(arr[i]>1)
        {
          printf("%c occurs %d times\n",(char)(i),arr[i]);
        }}
        return 0;
}

Here is the error message from the console:
https://i.sstatic.net/GpMOW.png

Any help is appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人九事 2025-01-24 04:59:50

我对代码做了一些更改,如评论中所示。

#include <stdio.h>
#include <string.h>
    
int main()
{
    char str[30];
    printf("Enter your String:");
    scanf("%29[^\n]", str);             // limit the input length
    int i;
    int arr[256] = { 0 };
    for(i = 0; i < strlen(str); i++)
    {
        if(str[i] == ' '){
            continue;
        }
        // this line was repositioned so that it can execute
        // char can be signed, so don't index by a negative value
        // the post-increment should apply to the `arr[]` array element
        arr[ (unsigned char)str[i] ]++;
    }
    printf("Repeated character in a string are:\n");
    for(i = 0; i < 256; i++)
    {
        if(arr[i] > 0)                  // inform *any* usage
        {
            // (char)i gets promoted to int anyway
            printf("%c occurs %d times\n", i, arr[i]);
        }
    }
    return 0;
}

会议:

Enter your String:one two
Repeated character in a string are:
e occurs 1 times
n occurs 1 times
o occurs 2 times
t occurs 1 times
w occurs 1 times

I've made some changes to the code, shown in comment.

#include <stdio.h>
#include <string.h>
    
int main()
{
    char str[30];
    printf("Enter your String:");
    scanf("%29[^\n]", str);             // limit the input length
    int i;
    int arr[256] = { 0 };
    for(i = 0; i < strlen(str); i++)
    {
        if(str[i] == ' '){
            continue;
        }
        // this line was repositioned so that it can execute
        // char can be signed, so don't index by a negative value
        // the post-increment should apply to the `arr[]` array element
        arr[ (unsigned char)str[i] ]++;
    }
    printf("Repeated character in a string are:\n");
    for(i = 0; i < 256; i++)
    {
        if(arr[i] > 0)                  // inform *any* usage
        {
            // (char)i gets promoted to int anyway
            printf("%c occurs %d times\n", i, arr[i]);
        }
    }
    return 0;
}

Session:

Enter your String:one two
Repeated character in a string are:
e occurs 1 times
n occurs 1 times
o occurs 2 times
t occurs 1 times
w occurs 1 times
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文