0

Hello Everyone so this is my first post, and i have been studying programming in C from like 6 months enrolled in a university.From last three days i am trying to learn windows.h and upto much extent i have got basics from youtube . But now i am stuck in this code where runtime error occurs when user press 'Ok' button. Please help me out in this. Thanks in advance

            double res,x,t1,t2,t3,temp;
            char num[10],t4[20],disp[50],c;
            GetWindowText(box2,num,11);
            ifstream inFile;
            temp=atoi(num);
            inFile.open("product.txt");
            while(!inFile.eof())
            {
                inFile>>x;
                inFile>>t4;
                inFile>>t3;
                inFile>>t2;
                inFile>>t1; 
                cout<<x;
                if(x==temp)
                {
                sprintf(disp,"\tId: %.0lf\t\n\tName: %s\t\n\tSale Price: %.0lf\t\n\tDiscount %.2lf\t\n\tProduct Added to Cart",x,t4,t2,t1);                     
                MessageBox(hwnd2,disp, "Product Info", MB_OK);
                break;
                }
            }
            inFile.close();
Hammad97
  • 27
  • 6
  • `GetWindowText` you pass in an array of 10 chars and tell the API it's got room for 11. – Richard Critten May 23 '16 at 15:28
  • 2
    I suggest you get a `c++` book and stay away from online tutorials. You are are writing a bad mix of `c` and `c++`. – drescherjm May 23 '16 at 15:28
  • 2
    See also [why eof is bad](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – Thomas Matthews May 23 '16 at 15:31
  • IMO, you should declare one variable per line, spaces between operators. This makes code easier to read. Spaces are ignored by the compiler and don't take up any significant time during compilation. – Thomas Matthews May 23 '16 at 15:32
  • Out of curiosity, what happens if the text box doesn't contain a valid number? – Thomas Matthews May 23 '16 at 15:33
  • 1
    What is the runtime error message? What is the expected behavior? What is the actual behavior? What happened when you used the debugger? – Thomas Matthews May 23 '16 at 15:35
  • #Richard I need to do it since it is skipping char as an array of 10 is actually 11 because it have a '\0' character too. – Hammad97 May 23 '16 at 15:52
  • #Thomas. I'll try to do that from next time. – Hammad97 May 23 '16 at 15:53
  • #drescherjm I just used sprintf in order to get that whole double and other stuff in a string so i could print it in my message box. I couldn't find any other way, if you have one please let me know. – Hammad97 May 23 '16 at 15:58
  • #Thomas the error it gives that .exe has stopped working and it must just do like just close that message box only no error should be given as a loop break is enough. – Hammad97 May 23 '16 at 16:00
  • That probably is caused by the first comment. I mean "you pass in an array of 10 chars and tell the API it's got room for 11" – drescherjm May 23 '16 at 16:50
  • Also `disp` is not sized large enough. Although you would not have that issue if you were using `std::string`. – drescherjm May 23 '16 at 16:52
  • Okay. Thanks everyone that issue is fixed and #drescherjm I tried using string but sprintf was accepting char* only but it got fixed when i made it num[6] from num[10]. As my product id was of max. 6 numbers. – Hammad97 May 23 '16 at 20:18
  • The `c++` way is to not use sprintf and avoid other `c-string` functions. http://stackoverflow.com/questions/4983092/c-equivalent-of-sprintf – drescherjm May 23 '16 at 20:30

0 Answers0