- #1
subwaybusker
- 51
- 0
I am required to use getline() and sscanf() somewhere in my function to read standard input and store it into arrays.
I have input like this:
10/10/08 12:00
A1:2.31:1:P
2B:1.98:2:g
2b:1.82:3:b
the first line is the time, which i used strcpy to copy it to an array called time[]
the second to fourth lines i am required to check that the first two characters before the colon are between a-z, A-Z, 0-9, plus i need to store it in a two dimensional array which i have no idea how. The characters after the first colon need to be to 2 decimal places only and positive, the characters after the second colon need to be positive, and the character after the third colon can only be chosen from a few specific characters.
So far i have this in my function:
int c;
int a=0;
while((getline(line, NUM))>0)
if (line[2]=='/')
strcpy(time, line);
else strcpy(purchaseline, line);
/*don't know how to filter out the characters that don't meet the requirements. maybe use a for loop? maybe i could declare 5 new int/chars so that sscanf can store the input there, and then i could test the condition of the values stored in these int/chars and choose to store them in the actual name, price, quantity, taxtype arrays?*/
sscanf(purchaseline, "%c%c:%0.2lf:%d:%c", name+a, name+a+1, price+a, quantity+a, taxtype+a);
/*name, price, quantity, and purchaseline are all arrays*/
this is basically the boiled down version of the assignment i think
if anyone has time to read more about the assignment, the link is here:
http://www.megaupload.com/?d=D9BDOK1N
I have input like this:
10/10/08 12:00
A1:2.31:1:P
2B:1.98:2:g
2b:1.82:3:b
the first line is the time, which i used strcpy to copy it to an array called time[]
the second to fourth lines i am required to check that the first two characters before the colon are between a-z, A-Z, 0-9, plus i need to store it in a two dimensional array which i have no idea how. The characters after the first colon need to be to 2 decimal places only and positive, the characters after the second colon need to be positive, and the character after the third colon can only be chosen from a few specific characters.
So far i have this in my function:
int c;
int a=0;
while((getline(line, NUM))>0)
if (line[2]=='/')
strcpy(time, line);
else strcpy(purchaseline, line);
/*don't know how to filter out the characters that don't meet the requirements. maybe use a for loop? maybe i could declare 5 new int/chars so that sscanf can store the input there, and then i could test the condition of the values stored in these int/chars and choose to store them in the actual name, price, quantity, taxtype arrays?*/
sscanf(purchaseline, "%c%c:%0.2lf:%d:%c", name+a, name+a+1, price+a, quantity+a, taxtype+a);
/*name, price, quantity, and purchaseline are all arrays*/
this is basically the boiled down version of the assignment i think
if anyone has time to read more about the assignment, the link is here:
http://www.megaupload.com/?d=D9BDOK1N
Last edited by a moderator: