与未指定行为的字符串字面结果进行比较? [英] Comparison with string literal results in unspecified behaviour?

查看:1059
本文介绍了与未指定行为的字符串字面结果进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试编写程式时遇到问题。它只是一个Windows控制台程序,我是非常新的C ++。这只是我的第四个程序。



我遇到的问题是,当我运行我的程序,我没有错误,但很多警告说,与字符串字面值结果



当程序运行,而不是添加数字,我想要它只是给我一个随机的大数字



以下是代码:

  #include< iostream> 

using namespace std;

int main()
{
int hold;
int i;
int n;
i = 6;
int result;
int * price;
char items [100] [100];

if(items == 0)
cout<< 没有项目可以存储;
else
{
for(n = 0; n {
cout< Item#<< n<< :;
cin>>项目[n];
}
cout<< \\\
You Entered:\\\
;
for(n = 0; n< i; n ++)
cout<项目[n] ,;

}
for(n = 0; n< i; n ++)
{
if(items [n] ==ab){
price [n] = 2650;
}

else if(items [n] ==ae){
price [n] = 1925;
}

else if(items [n] ==ie){
price [n] = 3850;
}

else if(items [n] ==bt){
price [n] = 3000;
}

else if(items [n] ==pd){
price [n] = 2850;
}

else if(items [n] ==ga){
price [n] = 2600;
}

}

for(n = 0; n {
result = result + price [n ];
}

cout<< \\\
Total gold for this build:<<结果;
cin>>>保持;
return 0;
}

任何帮助。有可能是大的,我做错了。 if语句中的名称都是当前的占位符,我将添加更多的if语句,当我可以得到它与裸6,这是它需要工作。

== 仅在基本类型内部实现,数组不是原始类型,因此比较 char [100] 和字符串文字只会将它们作为2 char * 或更好地称为2个指针,指针不能等于 items [n] ==ae永远不能为true,而不是这样,你应该使用 std :: string 以保存字符串为:

  std :: string items [100] 
//初始化项目
if(items [n] ==ae)...


b $ b

或者你应该使用 strcmp 来比较字符串,但是remeber strcmp 因此您的代码将为:

  char items [100] [100]; 
//初始化项目
if(strcmp(items [n],ae)== 0)...

另外一个注意事项是 if(items == 0)没有用,因为 items 在堆栈上分配,而不是在堆中!


I am having a problem with the program I am trying to code. It's just a Windows console program and I am very new to C++. It's only my 4th program.

The problem I am having is that when I run my program I have no errors but a lot of warnings that say "comparison with string literal results in unspecified behaviour" in the lines that I will highlight below.

When the program runs instead of adding the numbers I want it to it just gives me a random huge number no matter what I put in for my inputs.

Here is the code:

#include <iostream>

using namespace std;

int main()
{
     int hold;
     int i;
     int n;
     i = 6;
     int result;
     int * price;
     char items[100][100];

     if (items == 0)
        cout << "No items can be stored";
    else
    {
        for (n=0; n<i; n++)
        {
            cout << "Item#" << n << ": ";
            cin >> items[n];
        }
        cout <<  "\nYou Entered: \n";
        for (n=0; n<i; n++)
            cout << items[n] << ", ";

    }
    for (n=0; n<i; n++)
    {
        if (items[n] == "ab"){
        price[n] = 2650;
        }

        else if (items[n] == "ae"){
        price[n] = 1925;
        }

        else if (items[n] == "ie"){
        price[n] = 3850;
        }

        else if (items[n] == "bt"){
        price[n] = 3000;
        }

        else if (items[n] == "pd"){
        price[n] = 2850;
        }

        else if (items[n] == "ga"){
        price[n] = 2600;
        }

    }

    for (n=0; n<i; n++)
    {
    result = result + price[n];
    }

    cout << "\nTotal gold for this build: " << result;
    cin >> hold;
    return 0;
}

Any help is appreciated. There is probably something big that I've done wrong. The names in the if statements are all currently placeholders and I'll be adding a lot more if statements when I can get it to work with the bare 6 which is what it needs to work.

解决方案

In C++ == only implemented internally for primitive types and array is not a primitive type, so comparing char[100] and string literal will only compare them as 2 char* or better to say as 2 pointers and since this 2 pointers can't be equal then items[n] == "ae" can never be true, instead of this you should either use std::string to hold string as:

std::string items[100];
// initialize items
if( items[n] == "ae" ) ...

or you should use strcmp to compare strings, but remeber strcmp return 0 for equal strings, so your code will be as:

char items[100][100];
// initialize items
if( strcmp(items[n], "ae") == 0 ) ...

And one extra note is if (items == 0) is useless, since items allocated on stack and not in the heap!

这篇关于与未指定行为的字符串字面结果进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆