i'm new member. And now, i'm having a exercise: "input a string of character(100,101,102...)(number of character is unlimited) and store numbers in array". My teacher provide us a function. It is ReadWord to read a number in the string. But i still have 2 issues. 1st, in the "for" loop, when i put the "puts" command to print the array's elements, i can't print the last element. And if i put the "puts" command outside of "for" loop, i can't print any element. Please tell me why and how to fixed it. Thanks you very much!
#include <stdio.h>
#include <string.h>
#include<conio.h>
const int MAX_NUM_LEN = 7;
const int MAX_LINE_LEN = 50;
int IsComma(int ch) {
return (ch == ',');
}
int ReadWord(char *num) {
int ch, pos = 0;
ch = getchar();
while (IsComma(ch))
ch = getchar();
while (!IsComma(ch) && (ch != EOF)) {
if (pos < MAX_NUM_LEN) {
num[pos] = (char)ch;
pos++;
}
ch = getchar();
}
num[pos] = '\0';
return pos;
}
int main()
{
int i,j;
int count;
char **ds;
ds=new char *[50];
for (i=0;i<50;i++) ds[i]=new char [3];
char num[MAX_NUM_LEN + 1];
int numLen;
char line[MAX_LINE_LEN + 1];
int lineLen = 0;
i=0;count=0;
for (;;) {
numLen = ReadWord(num);
if (numLen == 0) break;
strcpy(ds[i],num);
puts(ds[i]);
i++;
}
j=i;
printf("\n %d",j);
for(i=0;i<j;i++) printf("%s ",ds[i]);
for(i=0;i<j;i++) delete ds[i];
delete []ds;
getch();
}