Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

small bug I cannot figure out with using getche() and array

Status
Not open for further replies.

Beholder2

Technical User
Sep 27, 2004
31
US
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);
}
 
I'm not quite sure what your code is trying to accomplish? You defined "test" (a very bad name for the IP Address) as an array or 2x2 chars, but IP Addresses are 3x4 without the terminating NULLs or 4x4 with the NULLs.

If you want to control whether the character is printed to the screen, you should be using getch() & putch() instead of getche().

getch() & getche() return an int, not a char.
 
Like I said I just bashed out the code from the proper program to test what wasn't working right, thats not the real one. I was trying to illustrate how in the input loop you cannot enter a dot to start an octet so it asks for the input again clearing the value aswell as repositioning the cursor. I was puzzled when after holding down the '.' for a few seconds it would force a . to the screen. The only mistake I made was incrementing dots(dot_count) (which is a place holder to tell me where each dot was placed, allowing me to read each octet individually) beyond its declared limit. I don't know why my IDE (dev c++) won't recognize this but apparently its changing something in memory. I don't know what its doing exactly.

sorry I wasn't clearer with the code.
 
Here's something I threw together, but you'll still have to modify it to validate whether each octet is 0-255...
Code:
#include <iostream>
#include <string>
#include <conio.h>
#include <ctype.h>

using namespace std;


int main()
{
	cout << endl << "Enter the IP Address: " << flush;

	int c    = 0;
	int pos  = 0;
	int dots = 0;
	string ipAddr;

	do
	{
		c = getch();

		if ( (pos == 0) && (c == '.') )
		{
			continue;
		}
		else if ( (isdigit( c ) == 0) && (c != '.') )
		{
			break;
		}

		if ( c == '.' )
		{
			++dots;

			if ( dots > 3 )
			{
				break;
			}
		}

		ipAddr += static_cast<char>( c );
		cout << static_cast<char>( c ) << flush;
	} while ( ++pos < 15 );

	return 0;
}
 
Thanks, although I couldn't figure out how to limit the amount of characters inputed using getch()I managed to use the isdigit instead of using my own function. I had to change my program around because for some reason it would compile okay but whenever I entered a number it would crash. I really need to learn my libraries there as so many!
 
I don't think it's worth verifying the IP Address they enter while they're entering it (other than limiting it to 15 characters). After they enter it, you can do your checking and return an error if the IP Address doesn't make sense.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top