[RESOLVED] only works with cout?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: [RESOLVED] only works with cout?

  1. #1
    Join Date
    Apr 2013
    Posts
    34

    [RESOLVED] only works with cout?

    hey, I ran into this, what I'm calling "quantum problem" because it's like quantum mechanics, when I'm looking at
    what my program is doing, it works fine, but if I want to hide the data, it doesn't work.

    I can't show you the original code since it's for a business, but I've managed to replicate it with this simple program:

    Code:
    #include <Windows.h>
    #include <iostream>
    
    using namespace std;
    
    long big[999999];
    int x = 1;
    
    int main()
    {
    	while(true)
    	{
    		big[x] = x;
    		x = x+1;
    
    		if (GetAsyncKeyState(0x01)) break; // just included this as a simple way to exit the program.
    
    		cout << x << '\r' ; // only works when this line is included.
    
    	}
    
    	return 0;
    }
    this works just dandy, but if you remove the cout/comment it out. it'll get an error after a few seconds.

    I'm running windows 8, using VS 2012 desktop edition.

    my guess is that without the display, it calculates the data to fast, filling up the array, causing an error when it can't hold any more data.. but I can't seem to replicate that problem with the cout active.

    any Idea what's causing this?

    thank you.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,294

    Re: only works with cout?

    Quote Originally Posted by peteandperry
    my guess is that without the display, it calculates the data to fast, filling up the array, causing an error when it can't hold any more data.. but I can't seem to replicate that problem with the cout active.
    My guess is that you are correct: due to the infinite loop, your program has undefined behaviour when the array is written to out of bounds. A simple fix is to change that while loop to a for loop that ensures that the array is not written to out of bounds.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,431

    Re: only works with cout?

    Quote Originally Posted by peteandperry View Post
    hey, I ran into this, what I'm calling "quantum problem" because it's like quantum mechanics, when I'm looking at what my program is doing, it works fine, but if I want to hide the data, it doesn't work.
    The C++ language works like this:

    If you make an error such as accessing an array out of bounds, anything can happen. The program may work, may crash, may work today and crash tomorrow, may work for years and then crash when a simple, unrelated coding change is made. It may work on your computer but fail on another computer, may work on 10,000 computers but fail on computer 10,001, etc.

    Unlike other computer languages, C++ mistakes such as this do not automatically pop up an "error window" or "stack trace" or whatever nice things other languages would offer. Instead, you are left in the dark, and especially if your program seems to run "ok". The only time you will know you have an error is if (luckily) your error is detected on your machine at run time, or if you can spot the error during program development. However, many times it's that unlucky customer that experiences the error, or worse, it's at the demo of your program where the problems start to occur.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Apr 2013
    Posts
    34

    Re: only works with cout?

    laserlight: thank you, though a for loop won't work for my actual program, I'll set some more parameters to prevent that from happening, and I'll probably also need to add some "Sleep's" so it doesn't run as fast, or to only record for a more specific set of parameters..

    any way's, thank you for your suggestion and insight

    Paul: thank you for pointing that out to me, as you can probably tell, I'm not an expert programmer, I'm just helping a friend out, and luckily this software won't be given to other customers, just 8(or so) computers here in the office, but still, it's for his business, and the less headache's I can help my friend avoid, the better. and I'm glad I've stumbled across this problem now rather then later so I can fix it before bad things happen

    thank you for your insight , it's a good reminder to me that not all programming problems are syntax or visible error's. thank you again

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,310

    Re: only works with cout?

    If the real program is polling key board states and is a console app, you might be better off starting with a windows app (and/or a key board hook).

  6. #6
    Join Date
    May 2013
    Location
    // Brazil
    Posts
    51

    Re: only works with cout?

    instead loop while use loop for and youŽll not need to put break to stop your program. to hide your data you can make like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h> //to use the comand system
    
    #include<conio.h> // for the function getch
    
    main ()
    {
    
    char string[5];
    printf ("\n Type the password with 5 digits: ");
    string[0] = getch();
    printf ("*");
    string[1] = getch();
    printf ("*");
    string[2] = getch();
    printf ("*");
    string[3] = getch();
    printf ("*");
    string[4] = getch();
    printf ("*");
    
    if((string[0] == 'a') && (string[1] == 'd') && (string[2] == 'r') && (string[3] == 'i') && (string[4] == 'a'))
    printf ("\nAlowed Access!\nwhat you wish to do now?\n");
    else
    printf ("\nAccess denyed!\n you not permission to enter!\n");
    
    system("pause"); //to see result
    }

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,108

    Re: only works with cout?

    Quote Originally Posted by crisdeveloper View Post
    instead loop while use loop for and youŽll not need to put break to stop your program. to hide your data you can make like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h> //to use the comand system
    
    #include<conio.h> // for the function getch
    
    main ()
    {
    
    char string[5];
    printf ("\n Type the password with 5 digits: ");
    string[0] = getch();
    printf ("*");
    string[1] = getch();
    printf ("*");
    string[2] = getch();
    printf ("*");
    string[3] = getch();
    printf ("*");
    string[4] = getch();
    printf ("*");
    
    if((string[0] == 'a') && (string[1] == 'd') && (string[2] == 'r') && (string[3] == 'i') && (string[4] == 'a'))
    printf ("\nAlowed Access!\nwhat you wish to do now?\n");
    else
    printf ("\nAccess denyed!\n you not permission to enter!\n");
    
    system("pause"); //to see result
    }
    What does your code have to do with a for loop, and what does either your code or your advice have to do with the original problem, why are you checking the value of a char array by checking one char at a time and what happens if the user types in more than five characters?

  8. #8
    Join Date
    May 2013
    Location
    // Brazil
    Posts
    51

    Re: only works with cout?

    this is a simple program to show how you can hide a password. the number of characters you can define.
    thatŽs a little out of the mainly problem but i hope that can help a little.

  9. #9
    Join Date
    May 2013
    Location
    // Brazil
    Posts
    51

    Re: only works with cout?

    if you are a good programmer, you can retire something from anycode and implement in your app.

  10. #10
    Join Date
    May 2013
    Location
    // Brazil
    Posts
    51

    Re: only works with cout?

    maybe this can trade the break line for a boolean type

    Code:
    #include <Windows.h>
    #include <iostream>
    
    using namespace std;
    
    long big[999999];
    int x = 1;
    bool key_pressed = true;
    
    int main()
    {
    	while(key_pressed == true)
    	{
    		big[x] = x;
    		x = x+1;
    
    		if (GetAsyncKeyState(0x01)) 
    
                    key_pressed = false;
    	}
    
    	return 0;
    }

  11. #11
    Join Date
    May 2013
    Location
    // Brazil
    Posts
    51

    Re: only works with cout?


  12. #12
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,421

    Re: only works with cout?

    Quote Originally Posted by crisdeveloper View Post
    maybe this can trade the break line for a boolean type

    Code:
    #include <Windows.h>
    #include <iostream>
    
    using namespace std;
    
    long big[999999];
    int x = 1;
    bool key_pressed = true;
    
    int main()
    {
    	while(key_pressed == true)
    	{
    		big[x] = x;
    		x = x+1;
    
    		if (GetAsyncKeyState(0x01)) 
                                key_pressed = false;
    	}
    
    	return 0;
    }
    First, please, use proper code indentation!
    Second, What will happen if x will become 999999?
    Last edited by VictorN; May 10th, 2013 at 11:54 AM.
    Victor Nijegorodov

  13. #13
    Join Date
    May 2013
    Location
    // Brazil
    Posts
    51

    Re: only works with cout?

    this problem is out my knowledge a little

    i make a program that works fine
    not with vectors but for me is something good

    Code:
    // function GetAsyncKeyState
    
    #include "stdafx.h"
    
    using namespace std;
    
    int main(void)
    {
        bool b_esc = false;
        while(!b_esc)
        {
    		
            if(GetAsyncKeyState(0x41) == -32768)//key 'A'
                std::cout<<"the key \'a\' has been pressed ...\n";
            if(GetAsyncKeyState(0x42) == -32768)//key B
                std::cout<<"the key \'b\' has been pressed ...\n";
    
    		//to close program
            if(GetAsyncKeyState(VK_ESCAPE))//key 'Esc'
                b_esc = true;
    
            _sleep(50);
        }
        return 0;
    }
    Last edited by crisdeveloper; May 10th, 2013 at 11:54 AM.

  14. #14
    Join Date
    May 2013
    Location
    // Brazil
    Posts
    51

    Re: only works with cout?

    thanks for tell me about code indentation, Victor!

  15. #15
    Join Date
    Apr 2013
    Posts
    34

    Re: only works with cout?

    WOW, that's a lot of reply's..

    Arjay: thanks though, although I do pull the occasional key-state from the user, its really only to start/stop certain aspects of my program, it's primarily a number cruncher, so no need for me to spend the extra time on a nice GUI. (and I don't want to hide it completely either for debugging purposes), but thank you for your input


    hanhphucthanhcong04: jeeze, if your gonna advertise/spam, don't do it in Vietnamese, at least do it in English. plus, you've spammed that line all over the internet.


    crisdeveloper: I see what you are trying to do, and I appreciate the effort of putting up the code, though that's more if I want to hide data, but still show that there is data there (like a password), but there's nothing secret with the data I'm hiding, it's just a number cruncher, so there is no need to show all the numbers that it's processing. but I am very thankful for you putting in the time and effort to try and help me out


    GCDEF: I think he simply misunderstood my question which is ok, I'll try to make my questions more clear next time, thank you for your contribution to the thread ^.^


    crisdeveloper: yes, yes it is I can probably use it in a different program, or if i want to give limited access in this program. thanks!


    crisdeveloper: yes I could (though I don't consider myself that good of a programmer, lol mediocre at best )

    crisdeveloper: lol I only put the key-press there as an easy way to exit the program.
    what I ended up using is an " if (x >= 999998) break; " (because adding it to the while loop list would make it extremely cluttered since in my original program I already have several conditions)

    chrisdeveloper: thanks for the link! it's an interesting read.

    VictorN: and that brings us back to our original question (that is solved), it turns out, if there's no "cout" or "sleep" or anthing
    else like that to slow it down, it'll do the entire loop INSTANTLY, as shown here;

    Code:
    // this is NOT the way to do it, since it does the loop instantly
    // (and I'm only showing what's in the "Main" function to save space)
    
    while(true) //infinite loop
    {
         big[x] = x;
         x = x + 1;
         
         if(x >= 99998)
         {
              cout << "ERROR: array is out of bounds" << endl; // this will pop up as soon as you run the program
              break;
         }
    }
    Sleep(3000);
    - interesting note; in this case, if the if-statement is waiting for a number larger then you initialized, it'll get an error, however,
    when used with a "Sleep(1);" to slow it down, you can exceed the initialized number, with no error at all! and that's what made me
    confused originally.

    Code:
    // this is the way i fixed it (adding an x <= 99998 to the while loop conditions would work too in most situations)
    // (and I'm only showing what's in the "Main" function to save space)
    
    while(true) //infinite loop
    {
         big[x] = x;
         x = x + 1;
         
         if(x >= 99998)
         {
              cout << "ERROR: array is out of bounds" << endl; // this will pop up as soon as you run the program
              break;
         }
    }
    Sleep(3000);

    chrisdeveloper: that's an excellent key-state grabber


    Thank you all for your valuable input!

    Edited: because of typo in the code (thanks 2Kaud)
    Last edited by peteandperry; May 10th, 2013 at 03:50 PM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center