My program does not stop on condition

前端 未结 4 1076
别跟我提以往
别跟我提以往 2021-01-26 01:03

So- my program does not stop on condition (str2[o] != \'+\') So if anyone knows why and how to fix it it will help me ( :.

this is My code -

#include <         


        
相关标签:
4条回答
  • 2021-01-26 01:22

    At the moment + will be copied to str2, o index will point to the next char, which most probably won't be +, so you condition will never be true. This could be possible solution :

        do
        {
            str2[o] = str3[w];
            o++;
            w++;
        }
        while(str2[o-1] != '+' );
    
    0 讨论(0)
  • 2021-01-26 01:23
    while(str2[o] != '+') 
        {
            str2[o] = str3[w]; 
            o++;
            w++;
        }
    

    Assume o == x. You're assigning value to str2[x]. In while, you are seeing if str2[x+1] == '+', but str2[x+1] is empty.

    0 讨论(0)
  • 2021-01-26 01:29

    you need to check the last updated value in str2, but you are checking the next value which is not initialized with a character so the loop will keep on repeating itself. you can do this:

    while(str2[o-1] != '+'){
    
         str2[o] = str3[w]; 
        o++;
        w++;
    }
    
    0 讨论(0)
  • 2021-01-26 01:34

    This line

    while(str2[o] != '+')
    

    provokes undefined behaviour as str2's elements had not been properly initialised before being read.

    To fix this do

    char str2[9] = "":
    
    0 讨论(0)
提交回复
热议问题