Results 1 to 2 of 2

Thread: confusion city

  1. #1
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Default confusion city

    I created a driver for a widget and it runs perfectly, but I traslate it to Qt it does not work.
    Here is the driver program:
    Qt Code:
    1. void rule(int digit,int x, int place[])
    2. {
    3.  
    4. int n,flag=0;
    5. if(x % 2>0)
    6. n=floor(x/2);
    7. else
    8. n=x/2;
    9.  
    10. for(int i=0;i<n;++i)
    11. if (place[i]==place[n-i])
    12. flag++;
    13. if (flag==n)
    14. std::cout <<" "<<digit<<" is a palindrome";
    15. else
    16. std::cout<<" " <<digit<<" is not a palindrome";
    17.  
    18.  
    19.  
    20. }
    21.  
    22.  
    23. int pow(int a ,int b)
    24. {
    25. int i=1,power=1;
    26. while(i<b)
    27. {
    28. power *=a;
    29. ++i;
    30.  
    31. }
    32. return power;
    33.  
    34.  
    35. }
    36.  
    37.  
    38. int main()
    39. {
    40.  
    41. int rem[10],digit,n,x=1,place[10];
    42. std::cout << "\nEnter another number :";
    43. std::cin>>digit;
    44. n= sizeof digit;
    45. std::cout<<"\nn= "<< n;
    46. rem[0]= digit % pow(10,n);
    47. place[0]= (digit-rem[0])/pow(10,n);
    48. while( x<n)
    49. {
    50. rem[x]=rem[x-1] % pow(10,n-x);
    51. place[x]=(rem[x-1]-rem[x])/pow(10,n-x);
    52.  
    53. x++;
    54. }
    55. rule(digit,x,place);
    56.  
    57. system("Pause");
    58.  
    59. }
    To copy to clipboard, switch view to plain text mode 

    When run it Qt any number typed in states it is not a palindrome, but in the driver it worls
    Here is the Qt version

    Qt Code:
    1. #include "ui_palindrome.h"
    2. #include "palindrome.h"
    3. #include <QString>
    4. #include <QtGui>
    5. #include <QLineEdit>
    6. #include <QPushButton>
    7. #include <QSize>
    8.  
    9.  
    10. void rule(int x, int place[],QLineEdit *lineEdit)
    11. {
    12. QString msg=lineEdit->text()+" is a palindrome";
    13. QString msg2=lineEdit->text()+" is not a palindrome";
    14. QMessageBox msgbox;
    15. int n,flag=0;
    16.  
    17. if(x % 2>0)
    18. n=floor(x/2);
    19. else
    20. n=x/2;
    21.  
    22.  
    23. for(int i=0;i<n;++i)
    24. if (place[i]==place[n-i])
    25. flag++;
    26. if (flag==n)
    27.  
    28. msgbox.setText(msg);
    29. else
    30. msgbox.setText(msg2);
    31.  
    32. msgbox.exec();
    33.  
    34. }
    35.  
    36.  
    37.  
    38.  
    39.  
    40. int pow(int a ,int b)
    41. {
    42. int i=1,power=1;
    43. while(i<b)
    44. {
    45. power *=a;
    46. ++i;
    47. }
    48. return power;
    49.  
    50. }
    51.  
    52.  
    53.  
    54. palindrome::palindrome(QWidget *parent) :
    55. QDialog(parent)
    56.  
    57. {
    58. setupUi(this);
    59. connect(checkButton, SIGNAL(clicked(bool)), this, SLOT(palindrome_checker()));
    60. }
    61.  
    62.  
    63. palindrome::~palindrome()
    64. {
    65. delete this;
    66. }
    67.  
    68.  
    69.  
    70. void palindrome::palindrome_checker()
    71. {
    72. int remainder[10],place[10],digit,x=1,m=0;
    73.  
    74. digit=lineEdit->text().toInt();
    75. m=lineEdit->text().size();
    76. remainder[0]= digit % pow(10,m);
    77. place[0]= (digit-remainder[0])/pow(10,m);
    78. m=lineEdit->text().size();
    79. while(x<m)
    80. {
    81. remainder[x]=remainder[x-1] % pow(10,m-x);
    82. place[x]=(remainder[x-1]-remainder[x])/pow(10,m-x);
    83. ++x;
    84. }
    85.  
    86. rule(x, place,lineEdit );
    87.  
    88. }
    To copy to clipboard, switch view to plain text mode 

    I posted problems with this widge & I thought I was, as far interface goes it reacts when a number is put in.
    I think the problem might be
    Qt Code:
    1. m=lineEdit->text().size();
    To copy to clipboard, switch view to plain text mode 
    but according to the documentation size() returns the size() of lineEdit->text() as an int.
    Help !

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: confusion city

    The sizeof statement in your original program doesn't make sense. sizeof int is always 4 (depending on the system). I've tested your original program and it rules wrongly on 45654 and 4554. Revise your algorithm.

    On a side note, you don't have to do
    Qt Code:
    1. delete this;
    To copy to clipboard, switch view to plain text mode 
    in a destructor. It is already being deleted. Before you jump into using Qt you might really really no really want to learn a thing or two about C++ first.
    Last edited by franz; 3rd February 2010 at 15:36.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

Similar Threads

  1. Class Confusion
    By Petr_Kropotkin in forum Newbie
    Replies: 5
    Last Post: 23rd January 2010, 15:23
  2. QRegExp Confusion
    By redneon in forum Qt Programming
    Replies: 3
    Last Post: 21st April 2009, 11:33
  3. Templates Confusion
    By baray98 in forum General Programming
    Replies: 6
    Last Post: 23rd November 2008, 11:14
  4. QT debug confusion
    By swistak in forum Installation and Deployment
    Replies: 2
    Last Post: 24th September 2008, 19:25
  5. Plugins confusion
    By Paul Drummond in forum Qt Programming
    Replies: 11
    Last Post: 15th February 2006, 09:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.