Annoying Message Box Problem

This is a discussion on Annoying Message Box Problem within the C++ Programming forums, part of the General Programming Boards category; Hi again. Another quick question: I'm using a basic cosole app, and halfway through the program, a message box appears. ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    Question Annoying Message Box Problem

    Hi again.
    Another quick question:

    I'm using a basic cosole app, and halfway through the program, a message box appears. My problem is that the message box is created behind the window. I know this isn't exactly vital to the execution of a program, but it's really annoying. Is there a simple, quick way to stop this happening?

    Thanks in advance

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Show some code. What message box are you using from what library? Sometimes, there will be arguments which specify if the message box pops up on top or in the back.

    Another possibility, I suppose, is that your message box is popping up in front and getting pushed to the back because something else happens in the console which is then pushed up front.
    Last edited by SlyMaelstrom; 07-01-2006 at 04:59 AM.
    Sent from my iPad®

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    Code

    Dead simple, incomplete file editor.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        int mb_result;
        string edit;
        string content;
        string location;
        
        cout<<"\n    TXT READER\n\n\n    NOTE: This program is used for reading\n          txt documents only.\n\n\n";
        
        cout<<"    Text file you wish to read:\n\n    ";
        getline (cin, location);
        
        ifstream a_file (location.c_str());
        a_file>> content;
        
        if ( a_file.fail() )
        {
            mb_result = MessageBox(NULL, "                             Failed to locate/read a file.\n                              Do you wish to try again ?                                                  ", "   Text.exe", MB_YESNO | MB_ICONEXCLAMATION);
            
            while ( mb_result == IDYES && a_file.fail() )
            {
                a_file.close();    // Close existing stream before opening again. (Bottom of while loop.)
                a_file.clear();    // Clear the stream status.
            
                system("cls");
                
                cout<<"\n    TXT READER\n\n\n    NOTE: This program is used for reading\n          txt documents only.\n\n\n";
        
                cout<<"    Text file you wish to read:\n\n    ";
                getline (cin, location);
        
                a_file.open(location.c_str());
                a_file>> content; 
            }
        }
        
        if ( a_file.is_open() )
        {
        
            cout<<"\n\n\n    This is the content of the file:\n\n    "<<content<<"";
        
            cin.get();
        
        }    
            
    }
    I understand that if I put the window handle instead of NULL when creating the message box, it will be 'part of' that window, but as far as I'm concerned, my window has no handle.

    Thanks

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    Another Question

    Sorry, this has absolutely nothing to do with my messagebox question, but how can I convert a string to an int?

    Ta

    Or even better, what is a function similar to this, except it is used for ints : intname.erase(intname.size()-1);
    Last edited by Necrofear; 07-01-2006 at 08:47 AM.

  5. #5
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    308
    Hi:

    About your question of the console, since console apps (in Win32) are like meant to be run MS-DOS (and MS-DOS don't have Windows handles ), you might want to see some API for this.

    As for string -> int see atoi
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You just want to get rid of the last digit?

    Code:
    int x = 12345;
    x /= 10;

    And you should stick with one all the way: console or API. Intermixing stuff may produce undefined behaviour.


    Code:
            cout<<"\n\n\n    This is the content of the file:\n\n    "<<content<<"";
    Why the extra "" at the end?
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    I'm gonna take your advice, an get rid of the message box. But about getting rid of the last digit, on that example, what exactly would be taken off that int ? Would it become: 1234? If so, why? Should that not take off 10 digits? And what does /= mean?

    Cheers for the replies

    Oh, and about the extra "" on the end, it didn't work without it. That was wierd to me too, but at least it would compile.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    Wtf

    Okay, I did a little experiment using the 'age program' from the tutorials, and I know realize that it does infact take off one digit only. However, something beyond my understanding has occured. Here's my code:

    Code:
    #include <iostream>
    #include <conio.h>
    #include <windows.h>
    
    using namespace std;
    
    void mainfunction();
    
    void mainfunction()
    {
        char age;
        int size = 0;
        int agefull;
        
        system("cls");
        
        cout<<"Please insert your age:    ";
        
        while ( age != '\n' && age != EOF && age != '\r' )
        {
            age = getch();
            
            if ( age == '\b' && size > 0 )
            {
                cout<<"\b \b";
                size--;
                agefull /= 10;
            }
            
            else
            {
                if ( isdigit(age) && size < 3 )
                {
                    cout<<age;
                    agefull += age;
                    size++;
                }
            }
        }
        
        cout<<"\n\n\n";
        
        if ( agefull < 100 )
        {
            cout<<"You are pretty young!";
        }
        
        if ( agefull == 100 )
        {
            cout<<"You are old.";
        }
        
        if ( agefull > 100 )
        {
            cout<<"You are really old.";
        }    
    }     
    
    int main()
    {
        char input;
        
        cout<<"Do you wish to run this program ?\n\nY = Yes\nN = No\n\n";
        input = getch();
        
        while (input != 'y' && input != 'Y' && input != 'n' && input != 'N')
        {
            input = '\0';
            input = getch();
        }
        
        cout<<input<<"";
        Sleep(1000);
        
        switch (input)
        {
            case 'y':
                
                mainfunction();
                
            break;
                
            case 'n':
                
                return 0;
                
            break;
            
            case 'Y':
                
                mainfunction();
                
            break;
            
            case 'N':
                
                return 0;
                
            break;
            
            default:
                
                cout<<"\n\nError. Execution Terminated.\nPress any key to exit the program.";
                input = getch();
                return 0;
                
            break;
            
        }
        
        cout<<"\n\n\nPress any key to exit the program.";
        input = getch();
    }
    The program kept saying you are really old, so I told it to print 'agefull' after a key was pressed or if backspace was pressed, and it came out with stupid numbers. Compile it and see for yourself. What the hell is going on ?

    Please help me, this is so frustrating.

    P.s. A few notes:

    1. I know that on the switch statement, I don't really need default, but I put it there just as a precaution.

    2. I don't really know what EOF, and \r are in the main function while loop. Would any one be so kind as to explain them to me. I found them on the FAQ about getch(), and I thought I'd better use them, because they worked fine in my other programs.

    Thanks.
    Last edited by Necrofear; 07-02-2006 at 07:07 AM.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,660
    1. You could at least make the response case-insensitive. There are cleaner ways to do this I guess, but you could fall-through
    Code:
    case 'Y':
    case 'y':
        mainfunction();
        break;
    case 'N':
    case 'n':
        cout << "Error with input. Closing...\n";
        break;
    Use of default is good.
    I never understood things like this though, if people want to run the program, they run it. No need to ask.

    2. getch is a bit of a stretch for something like this, you could do the same thing with standard input devices.
    Basically we check all those characters and EOF because we never want to get stuck in a loop.
    Supposing that you're only doing this to learn how getch works, to truly understand it, you have to wrap your head around the concept of keyboard buffering (thanks quzah! It might be a bit fishy why I have to defer the real explanation to someone else. heheh... ) Enter is one of those weird functional keys, where both a carriage return \r and a newline \n appear in the console. It's important to check for both of these characters when using getch because getch only grabs the first part of the keystroke.
    But that is not the only way the user can signal the end of input. If he's really clever, he could simulate EOF (end of file) and stop recording. You need to check for that also.

    As a parting gift:
    Code:
    #include <iostream>
    #include <limits>
    int main( ) {
            int age;
            std::cout << "Enter your age please: ";
            while(!(std::cin >> age)) {
                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                std::cout << "Enter your age: ";
            }
    
            if(age < 100)
                std::cout << "You're really young.\n";
            else if(age > 100)
                std::cout << "You're ancient!\n";
            else
                std::cout << "You're old.\n";
            return 0;
    }
    
    $ ./age
    Enter your age please: jfirijgkagf  
    Enter your age: 21
    You're really young.
    
    Owner@PAVILION ~
    $
    Last edited by whiteflags; 07-02-2006 at 10:19 AM. Reason: fixed the link

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Thanks Citizen! Oh and also thanks to quzah for your detailed explaination on keyboard buffering. I understand Getch() alot more though now.

    Cheers again.
    Necrofear.

    EDIT: Oh, hold on. Eventhough I understand getch() (well, a little more, anyway) I would still like to know what was wrong with the code so I don't make the same mistake again. Why does them wierd numbers come up? Any ideas?

    Ta
    Last edited by Necrofear; 07-02-2006 at 10:36 AM.

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,660
    It's because you're reading a character from the user, and not converting it to an integer type. You can learn how to do that in the FAQ, but since I'm feeling nice today, atoi is one way to do it:
    Code:
    if(isdigit(age) && size < 3)
    {
        cout << age;
        agefull += atoi(age);
    }
    Now go debug...

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    What do you mean 'No go debug' ?

    Oh, and I was just testing that atoi thing you showed me, but it says invalid conversion from 'char' to 'const char*' . It doesn't matter, I was just wondering how it could be done.

    EDIT: Oh right, you changed it. Cheers
    Last edited by Necrofear; 07-02-2006 at 11:09 AM.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    How come people keep deleting messages ?

    Oh, I understand now
    Last edited by Necrofear; 07-02-2006 at 11:16 AM.

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Because you keep editting yours.
    Sent from my iPad®

  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,660
    > invalid conversion from 'char' to 'const char*'
    Ah, I was wondering when this would happen. Alright, so you read a character at a time. But, atoi converts a string.
    - you need a character variable for the current one you read from the user.
    char age;
    - you need a string to put it in (I'm just gonna use a dumb c-string)
    char agefull[4]; // remember the terminating \0
    - you need an integer
    int the_age;

    Now you have everything you need. Append valid number characters to the agefull string, and then convert the whole string into an int. To make sure you don't overflow the string's memory and cause problems, you need to constantly monitor the size, and handle backspacing correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP with windows message loops problem
    By cloudy in forum C++ Programming
    Replies: 3
    Last Post: 01-21-2006, 12:09 PM
  2. No data showing in combo box
    By PJYelton in forum Windows Programming
    Replies: 6
    Last Post: 04-29-2005, 07:20 PM
  3. problem with UDP WSAAsyncSelect socket, no callback message received???
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-29-2004, 10:59 AM
  4. the open box problem
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 02-28-2003, 04:53 AM
  5. problem with the open dialog box
    By stallion in forum Windows Programming
    Replies: 13
    Last Post: 02-19-2003, 07:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21