Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Unexpected output qt c++ function
Forum Updated to NodeBB v4.3 + New Features

Unexpected output qt c++ function

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt 6.0
11 Posts 5 Posters 927 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zeyad Khaled
    wrote on last edited by Chris Kawa
    #1

    iam trying to embed a c++ function into my qt desktop application , the function works well on Visual studio and the output is the desired one ,But when i try to embed it in my app it works but the output is not what i exactly was looking for ,Here is my Function:

    #include "Consistency.h"
    #include<iostream>
    #include<string>
    #include<stack>
    #include<vector>
    #include<iterator>
    
    using namespace std;
    
    string Check_XML_Consistency(string xml_file)
    {
        string xml_edit = xml_file;
        static int pre_index;
        int begin_index;
        int true_index;
        char temp;
        int flag = 0;
        int flag_check = 0;
        int diffrence = 0;
    
        stack <string>xml_check_close_outer;
        stack <string>xml_check_close_outer_temp;
        stack <string>xml_check_close_inner;
    
        string xml_check_close_string;
        string check_top;
        string correct;
    
    
        for (static int i = 0; i < xml_edit.length(); i++)
        {
            if (xml_edit[i] == '<')
            {
                i++;
    
                if (xml_edit[i] != '/')
                {
                    begin_index = i;
                    pre_index = i - 1;
                    while (xml_edit[i] != '>') i++;
                    true_index = i;
                    diffrence = true_index - begin_index;
    
                    if (!xml_check_close_inner.empty())
                    {
                        xml_edit.insert(pre_index, ">");
                        xml_edit.insert(pre_index, xml_check_close_inner.top());
                        xml_edit.insert(pre_index, "</");
                        correct = xml_check_close_inner.top();
                        xml_check_close_inner.pop();
    
                        begin_index = pre_index + (int)correct.size() + 4;
                        true_index = begin_index + diffrence;
                        i = true_index;
                    }
                    i++;
                    while (xml_edit[i] != '<')
                    {
                        temp = xml_edit[i];
    
                        if (temp == ' ')
                        {
                            i++;
                            continue;
                        }
                        else
                        {
                            flag = 1;
                            break;
                        }
                    }
                    i = true_index;
                    if (flag == 1)
                    {
                        xml_check_close_inner.push(xml_edit.substr(begin_index, i - begin_index));
                        flag = 0;
                    }
                    else
                    {
                        xml_check_close_outer.push(xml_edit.substr(begin_index, i - begin_index));
                    }
                }
                else if (xml_edit[i] == '/')
                {
                    i++;
                    begin_index = i;
                    pre_index = i - 2;
    
                    while (xml_edit[i] != '>') i++;
    
                    xml_check_close_string = xml_edit.substr(begin_index, i - begin_index);
    
                    if (!xml_check_close_inner.empty())
                    {
                        xml_edit.insert(pre_index, ">");
                        xml_edit.insert(pre_index, xml_check_close_inner.top());
                        xml_edit.insert(pre_index, "</");
    
                        correct = xml_check_close_inner.top();
                        xml_check_close_inner.pop();
    
                        begin_index = pre_index + (int)correct.size() + 5;
                        i = begin_index + (int)xml_check_close_string.size();
                        xml_edit.erase(begin_index - 2, i - begin_index + 3);
                        i = begin_index - 3;
                    }
                    else if(!xml_check_close_outer.empty())
                    {
                        if ((xml_check_close_outer.top() == xml_check_close_string))
                        {
                            xml_check_close_outer.pop();
                        }
    
                        else
                        {
                            xml_edit.insert(pre_index, ">");
                            xml_edit.insert(pre_index, xml_check_close_outer.top());
                            xml_edit.insert(pre_index, "</");
    
                            correct = xml_check_close_outer.top();
                            xml_check_close_outer.pop();
    
                            begin_index = pre_index + (int)correct.size() + 5;
                            i = begin_index + (int)xml_check_close_string.size();
                            pre_index = begin_index - 2;
    
                            while (!xml_check_close_outer.empty())
                            {
                                if (xml_check_close_outer.top() != xml_check_close_string)
                                {
                                    xml_check_close_outer_temp.push(xml_check_close_outer.top());
                                    xml_check_close_outer.pop();
                                }
                                else
                                {
                                    while (!xml_check_close_outer_temp.empty())
                                    {
                                        xml_check_close_outer.push(xml_check_close_outer_temp.top());
                                        xml_check_close_outer_temp.pop();
                                    }
                                    flag_check = 1;
                                    break;
                                }
                            }
    
                            if (flag_check == 1)
                            {
                                while (xml_check_close_outer.top() != xml_check_close_string)
                                {
                                    xml_edit.insert(pre_index, ">");
                                    xml_edit.insert(pre_index, xml_check_close_outer.top());
                                    xml_edit.insert(pre_index, "</");
    
                                    correct = xml_check_close_outer.top();
                                    xml_check_close_outer.pop();
    
                                    begin_index = pre_index + (int)correct.size() + 5;
                                    i = begin_index + (int)xml_check_close_string.size();
                                    pre_index = begin_index - 2;
                                }
                                xml_edit.insert(pre_index, ">");
                                xml_edit.insert(pre_index, xml_check_close_outer.top());
                                xml_edit.insert(pre_index, "</");
    
                                correct = xml_check_close_outer.top();
                                xml_check_close_outer.pop();
    
                                begin_index = pre_index + (int)correct.size() + 5;
                                i = begin_index + (int)xml_check_close_string.size();
                                pre_index = begin_index - 2;
    
                                flag_check = 0;
                            }
                            else
                            {
                                while (!xml_check_close_outer_temp.empty())
                                {
                                    xml_check_close_outer.push(xml_check_close_outer_temp.top());
                                    xml_check_close_outer_temp.pop();
                                }
                                xml_edit.erase(begin_index - 2, i - begin_index + 3);
                                i = begin_index - 3;
                            }
                        }
    
                    }
                }
            }
    
        }
        if (!xml_check_close_inner.empty())
        {
            xml_edit.append("</"); xml_edit.append(xml_check_close_inner.top()); xml_edit.append(">");
            xml_check_close_inner.pop();
        }
        while (!xml_check_close_outer.empty())
        {
            xml_edit.append("</"); xml_edit.append(xml_check_close_outer.top()); xml_edit.append(">");
            xml_check_close_outer.pop();
        }
        return xml_edit;
    }
    

    And here is my button code that fires that function :

    void MainWindow::on_pushButton_4_clicked()
    {
        QString text =ui->plainTextEdit->toPlainText();
        string x=text.toStdString();
        string Result=Check_XML_Consistency(x);
        QString str = QString::fromStdString(Result);
        ui->plainTextEdit->setPlainText(str);
    
    }
    

    My input:
    input.png

    Vscode output(Expected one):
    VsOutput.png

    Qt Output:
    Qtouutput.png

    i tried debugging using qt to know what's happening but it was quite hard ,,Thanks in advance

    1 Reply Last reply
    0
    • AxelViennaA Offline
      AxelViennaA Offline
      AxelVienna
      wrote on last edited by
      #2

      A bit hard to guess since your code is pretty nested. You should definitively review your function as two issues stand out:

      • you define pre_index as static. Why? It will only be initialized on the first function call. Its value upon return will be used for all other calls. Does not seem like what you want and may trigger unexpected behavior. Also: Why do you use static in your first for loop?
      • your function will trigger an exception if you call it with the string “<“.

      Why don’t you set a break point inside on_pushButton_4_clicked() and debug your way through your function?

      C++ and Python walk into a bar. C++ reuses the first glass.

      Z 1 Reply Last reply
      2
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Folow-up from here: https://forum.qt.io/topic/132702/unresolved-external-symbol-class-std-basic-_string/
        As said before - learn to use a debugger.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • AxelViennaA AxelVienna

          A bit hard to guess since your code is pretty nested. You should definitively review your function as two issues stand out:

          • you define pre_index as static. Why? It will only be initialized on the first function call. Its value upon return will be used for all other calls. Does not seem like what you want and may trigger unexpected behavior. Also: Why do you use static in your first for loop?
          • your function will trigger an exception if you call it with the string “<“.

          Why don’t you set a break point inside on_pushButton_4_clicked() and debug your way through your function?

          Z Offline
          Z Offline
          Zeyad Khaled
          wrote on last edited by
          #4

          @AxelVienna i was not static but i got a warning from qt ro make it static so i just tried doing that , but it does'nt work either ways, thanks for helping

          JonBJ 1 Reply Last reply
          0
          • Z Zeyad Khaled

            @AxelVienna i was not static but i got a warning from qt ro make it static so i just tried doing that , but it does'nt work either ways, thanks for helping

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @Zeyad-Khaled
            Apart from what others have said about debug your code and/or put qDebug() statements into it if you want to know how it is behaving.

            What does your code actually do, and why in the world do you do it the way you do it? The code is opaque. Writing chunks of code to output bits like </, > and doing pops and checks and so on seems complex and error-prone. Why don't you use something like QDomDocument, or at least some node-structural-hierarchy of your own, to generate output from, instead of outputting piecemeal?

            Z 1 Reply Last reply
            0
            • JonBJ JonB

              @Zeyad-Khaled
              Apart from what others have said about debug your code and/or put qDebug() statements into it if you want to know how it is behaving.

              What does your code actually do, and why in the world do you do it the way you do it? The code is opaque. Writing chunks of code to output bits like </, > and doing pops and checks and so on seems complex and error-prone. Why don't you use something like QDomDocument, or at least some node-structural-hierarchy of your own, to generate output from, instead of outputting piecemeal?

              Z Offline
              Z Offline
              Zeyad Khaled
              wrote on last edited by
              #6

              @JonB it's A College Project and we are Required to write Code Using native c++ and i didn't write those functions my teammembers wrote it , so iam just trying to collect everything together and to check errors to make it just work , thank for helpinng

              JonBJ 1 Reply Last reply
              0
              • Z Zeyad Khaled

                @JonB it's A College Project and we are Required to write Code Using native c++ and i didn't write those functions my teammembers wrote it , so iam just trying to collect everything together and to check errors to make it just work , thank for helpinng

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #7

                @Zeyad-Khaled
                Well it sure is an opaque piece of code they have written, goodness knows why it works in one environment but not another.

                I don't know what

                the function works well on Visual studio and the output is the desired one ,But when i try to embed it in my app it works but the output is not what i exactly was looking for

                means. vscode is just an IDE for writing/running code. Code does not behave one way when run within vscode and another way outside of it. One does not get "different output" under vscode compared to (what you call) "Qt Output". When you say that, what do you mean, do you mean you use a different compiler in Qt Creator versus in vscode?

                What does it actually do anyway? It seems to take some (potentially) malformed XML input and then make some of its own "guesses" about how to "correct" the input to produce some (well-formed?) XML output from it, is that it? I have no idea how it works/is supposed to work, as there is no one way to "guess"/"correct" any ill-formed XML input, there are a non-ending number of ways one might choose for what the malformed input might intend and so what to do to change it.

                Z 1 Reply Last reply
                0
                • JonBJ JonB

                  @Zeyad-Khaled
                  Well it sure is an opaque piece of code they have written, goodness knows why it works in one environment but not another.

                  I don't know what

                  the function works well on Visual studio and the output is the desired one ,But when i try to embed it in my app it works but the output is not what i exactly was looking for

                  means. vscode is just an IDE for writing/running code. Code does not behave one way when run within vscode and another way outside of it. One does not get "different output" under vscode compared to (what you call) "Qt Output". When you say that, what do you mean, do you mean you use a different compiler in Qt Creator versus in vscode?

                  What does it actually do anyway? It seems to take some (potentially) malformed XML input and then make some of its own "guesses" about how to "correct" the input to produce some (well-formed?) XML output from it, is that it? I have no idea how it works/is supposed to work, as there is no one way to "guess"/"correct" any ill-formed XML input, there are a non-ending number of ways one might choose for what the malformed input might intend and so what to do to change it.

                  Z Offline
                  Z Offline
                  Zeyad Khaled
                  wrote on last edited by
                  #8

                  @JonB i agree with in that point , i tried putting the input in a different way that he expects and it ofcourse pops an error and the program terminates , the code should take xml file nad preetify it but it doesn't work in alot of cases except the only case he has assumed , Shall i get a help about the structure to use or a way to write this in a good Structure

                  1 Reply Last reply
                  0
                  • AxelViennaA Offline
                    AxelViennaA Offline
                    AxelVienna
                    wrote on last edited by
                    #9

                    You may want to test your code in Qt Creator with the same code as in vscode, i.e. with a hard coded string variable? Make the same call twice with the same input string, to rule out misbehavior due to the static variable. BTW, show us your compiler warnings that you eliminate by making i static in your for loop.

                    C++ and Python walk into a bar. C++ reuses the first glass.

                    1 Reply Last reply
                    0
                    • Kent-DorfmanK Offline
                      Kent-DorfmanK Offline
                      Kent-Dorfman
                      wrote on last edited by
                      #10

                      That static loop variable made me raise an eyebrow too. at function scope it would set the initial value on the first iteration and then all future reference would be based on whatever the last value it it was. OP doesn't state whether the function in which it is called is used once or called multiple times....but yeah, a static loop control var doesn't really make sense unless it servers as a global accumulator, in which case for() isn't really appropriate.

                      1 Reply Last reply
                      1
                      • JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by
                        #11

                        Making the for counter static will definitely make the behaviour go wrong if the function is called more than once. If it is the case that there are "compiler warnings that you eliminate by making i static in your for loop" I too would like to see the original warnings.

                        Separately there are incorrect assumptions about what you can afford to do to i in the loop. For example, in a loop which limits it to i < xml_edit.length() one cannot afford to go:

                            i++;
                            if (xml_edit[i] != '/')
                        

                        and other similar cases.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved