代码显示左值错误

时间:2015-03-04 17:50:27

标签: c++

我的代码有错误

  

prog.cpp

   比较两个给定两个相同长度的字符串和
  例如,如果str1大于另一个字符串,则向用户提供所需的输出。

      #include<iostream>
      #include<cstring>
      using namespace std;

   int main()

     {    
       int i=0;


         char  str1[10],str2[10];

          cin>>str1>>str2;

          if(strlen(str1)!=strlen(str2)) return 0;

         while(str1[i]!='\n'){

        if(str1[i]>='A'&&str1[i]<='Z') str1[i]=str1[i]+32;
       if(str2[i]>='A'&&str2[i]<='Z') str2[i]=str2[i]+32;
         str1++;   //is there any over here??
          }

        int result=strcmp(str1,str2);
         if(result==0) cout<<"0";
         else if(result>0) cout<<"1";
         else cout<<"-1";
            // printing values..
     return 0;

         }

3 个答案:

答案 0 :(得分:0)

    str1++;   //is there any over here??

您可能需要++i;

str1是一个数组,本身无法增加。

答案 1 :(得分:0)

str是一个数组,数组不能是可赋值的左值

str1++;

相同
str1 = str1 + 1;

所以有一个错误。

PS:阵列不可分配。

答案 2 :(得分:0)

 str1++;   //is there any over here??

不对。这应该是编译器错误。

您需要的是:

while(str1[i]!='\n')
{
   if(str1[i]>='A'&&str1[i]<='Z') str1[i]=str1[i]+32;
   if(str2[i]>='A'&&str2[i]<='Z') str2[i]=str2[i]+32;

   // Just increment the array index.
   ++i;
}
相关问题