Thread: bizarre anomaly in input loop with getche()

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    bizarre anomaly in input loop with getche()

    This isn't the full program but I rewrote the functional part that is causing the problem. The program i'm working on is going to be used to calculate subnets and vlsm's. All I'm asking of c++ and getche() is that It will prevent the user from starting the ip address (or octet of ip address) with a dot, just as a form a validation. anyway they problem is with a seemingly unrelated piece of code, specifically dot_count++, whenever this line is in my code for some reason after 48 attempts or so a dot is forcibly echoed to the screen!? why? I do not know. here's the code, its just a nested do while loop..thanks for any suggestions:
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <windows.h>
    #include "conio.h"
    #include <iomanip.h>
    #include <string>
    using namespace std;
    char test[2][2];
    void locate(int x, int y);
    int main()
    {
        int chrs=0;
        int y=0;
        int bounds=0;
        int dots[3];
        int dot_count=0;
        do {
          int x=0;
          do{
             chrs=chrs+x;
             test[y][x]=getche();
    
             if (test[y][x] == '.')
            {dots[dot_count]=x;
            dot_count++;} //move this line and you can never 
                          //enter a dot as the first character!
    
             if (test[y][0]=='.'){
              test[y][x]= ' ';
              locate(chrs,0);
              cout << ' ';
              locate(chrs,0);}
             else
              x++;
             if (x > 0)
                 bounds=x-1;
                 else
                 bounds=0;
          }while ((x<=2) && (test[y][x-1] != '.'));
    
            chrs=chrs+x-1;
            y++;
            cout << y;
    }while (y<=2);      system("PAUSE");
          return 0;
    }
    
    void locate(int x, int y)
    {
    	COORD point;
    	point.X = x; point.Y = y;
    	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), point);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Wow, what a mess of header files.

    Code:
    #include <iostream.h>  // old-style C++
    #include <stdlib.h>    // C
    #include <stdio.h>     // C
    #include <windows.h>   // Win32-specific
    #include "conio.h"     // DOS specific
    #include <iomanip.h>   // old-style C++
    #include <string>      // new-style C++
    It's only the last one which has anything to do with the std namespace.

    > whenever this line is in my code for some reason after 48 attempts or so
    Considering it's indexing an array with only 3 elements, I'm surprised it lasts so long.

    > char test[2][2];
    How does this represent a dotted IP address?

    Also, your other cryptically named variables like chrs do nothing to aid understanding what your overall strategy is.

    Personally, I would go with
    - read a line using cin.getline()
    - write a function called validateIPAddress, which is passed a buffer read by getline.

    Trying to mix input and validation all in the same code is always more trouble than it's worth.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    sorry 'bout that

    the actual program is clear and has alot more code, Basically it stores each octet of a ip address in a character array to be checked if its out of range or a valid net address etc. You cannot enter a dot to start and octet with, only after 1 or 2 numbers are entered for the octet then it goes to input the next one, also if you type 3 numbers it automatically puts a dot for you and goes to the next octet. Sort of like the tcp/ip host address in network settings in windows. My aim is to have a program to help beginners understand ip addressing and subnetting so I wanted it to be a dumb-proof as possible and help the user out.

    Also I'm totally lost with headers at the minute, I used to be a qbasic programmer back in the day and and library header was sort of rare, and was much simpler. But in C/C++ I am lost at the minute as to which ones have which functions, or even how they actually work.

    Thanks for the help though. I think the problem was actually it would increment dots[dot_count] ( used as a pointer to convert the characters to ints and check for validity ) beyond its declared limit and affected my varibles in memory in some way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop while waiting for input?
    By Terran in forum C++ Programming
    Replies: 6
    Last Post: 05-22-2008, 08:32 PM
  2. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  3. Input in timed loop
    By glo in forum Game Programming
    Replies: 12
    Last Post: 05-20-2006, 06:04 AM
  4. Infinite Loop when entering invalid input
    By acwheat in forum C Programming
    Replies: 5
    Last Post: 04-18-2006, 04:17 PM
  5. Replies: 2
    Last Post: 10-09-2002, 11:22 PM