I am using the code below and when I do a printf on my variable which gets filled by a pointer variable, I get all the weird stuff after my data. It looks like lines and a little L. Can someone help?
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
void main(void)
{
void inputch(char *);
char partid[7];
printf("What is the Part ID? "
inputch(partid);
printf("\nValidation done! Part ID is %s\n", partid);
}
void inputch(char *pID)
{
char ch; //hold input from user
int x; //counter to keep track of number of character coming in from user
x = 0; //initialize
do {
ch = toupper(getch()); //uppercase user input
if (ch == '\b') { //equals backspace
putch('\b');
putch(' ');
putch('\b');
x--;
}
else if ((ch != '\0') && (x < 2)) { //doesn't equal null
if (isalpha(ch)) { //if its a character
putch(ch); //write it to the screen
pID[x] = ch; //put character in pointer variable
x = x + 1;
}
}
else if ((ch != '\0') && ((x >= 2) && (x < 6))) {
if (isdigit(ch)) {
putch(ch); //write it to the screen
pID[x] = ch; //put number in pointer variable
x = x + 1;
}
}
} while (ch != '\r'); //carraige return
}
This is all the code so you can test it in the compiler.
Thanks!
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
void main(void)
{
void inputch(char *);
char partid[7];
printf("What is the Part ID? "
inputch(partid);
printf("\nValidation done! Part ID is %s\n", partid);
}
void inputch(char *pID)
{
char ch; //hold input from user
int x; //counter to keep track of number of character coming in from user
x = 0; //initialize
do {
ch = toupper(getch()); //uppercase user input
if (ch == '\b') { //equals backspace
putch('\b');
putch(' ');
putch('\b');
x--;
}
else if ((ch != '\0') && (x < 2)) { //doesn't equal null
if (isalpha(ch)) { //if its a character
putch(ch); //write it to the screen
pID[x] = ch; //put character in pointer variable
x = x + 1;
}
}
else if ((ch != '\0') && ((x >= 2) && (x < 6))) {
if (isdigit(ch)) {
putch(ch); //write it to the screen
pID[x] = ch; //put number in pointer variable
x = x + 1;
}
}
} while (ch != '\r'); //carraige return
}
This is all the code so you can test it in the compiler.
Thanks!