Hi all,
I'm trying to write a program that will take some strings in as input, and merge them into one string to output. This program doesn't want to execute the last getchar() function through, and I can't figure out why.
Here is the code:
Output:
The last call to 'get'
would printf the text, but then gets returned to command prompt instead of waiting for user input. If I were to move the gettype() function before the first get() function however, the code would get fully executed.
Does anyone have any ideas to why this is happening? Any help would be appreciated.
Andy
I'm trying to write a program that will take some strings in as input, and merge them into one string to output. This program doesn't want to execute the last getchar() function through, and I can't figure out why.
Here is the code:
Code:
#include <stdio.h>
int get(char *x);
int gettype(void);
int fetchext(char *y);
main()
{
int type, i, j;
char one[16];
char two[16];
char three[6];
for (i = 0; i < 16; i++)
one[i] = 0;
for (i = 0; i < 16; i++)
two[i] = 0;
for (i = 0; i < 6; i++)
three[i] = 0;
printf("Enter number: ");
if (!get(&three[0])) {
printf("Error\n");
}
printf("%s", &three[0]);
type = gettype();
printf("%s", &two[0]);
printf("Enter Internal IP: ");
if (!get(&two[0])) {
printf("Error\n");
}
return 0;
}
int get(char *x)
{
int i = 0;
while ((x[i++]=getchar()) != '\n')
putchar(x[i]);
x[i] = '\0';
return 1;
}
int gettype(void)
{
int x = 0;
while (x < 1 || x > 3) {
printf("\none\t1\ntwo\t2\nthree\t3\n");
printf("Select one: ");
x = (getchar() - '0');
}
return x;
}
int fetchext(char *y)
{
int i = 0;
while ((y[i++]=getchar()) != '\n')
;
y[i] = '\0';
return 1;
}
Output:
Code:
%./a.out
Enter number: 1
1
one 1
two 2
three 3
Select one: 1
Enter Internal IP: %
The last call to 'get'
Code:
if (!get(&two[0])) {
Does anyone have any ideas to why this is happening? Any help would be appreciated.
Andy