C Programmers please help with small program

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
Hello, I am given a task which is my last one for the term to write a particular program. I am just looking for steps of how to approach it. So the details about this program is:

? make a myScanf() function that works likescanf("%d", &val) for positive integers.

? i.e. myScanf(int * p) reads digit characters from
KB (use getchar()) and converts to integer.
? skips white space.
? reads digits and converts to integer until
? non-digit read, then stores in integer at *p
? You should be able to replace a call to
scanf("%d", &val) with myScanf(&val).

Please help. Again thanks alot for the help with my previous program.
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
Okay, the problem I'm seeing here is that you're pretty much giving us the program to do... you won't learn anything from us doing it.

Now, I'm going to show you how to do this via reading the requirements. Almost all requirements have words in them that clue you in how to complete this. These words don't necessary give you the exact thing to use, but point you toward a type of construct to use.

"reads digits and converts to integer until non-digit read, then stores in integer at *p"

That's all you should need to complete this.
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
I am confused with how to write a function that will read in the keys inputted thru the keyboard. If I could use scanf i could but writing a function that acts like one, I really have no idea where to start...

Wait, do I write a loop in the function using scanf function to pick up the input form the keyboard?
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
Read that line of text that I put at the bottom of my last post. I bolded the clue for ya. I'll even throw out another hint:

That aforementioned statement can be generically written like this: ~keep doing something~ until ~this happens~
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
so a loop checking the integers and an if statement. I dont get how you guys know exactly what to do jus tby looking at it already. Are you guys majoring in computer science? Or do C programming as a living?
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
is this going to be what i use to read in the integers?

fgets(input, sizeof(input), stdin);
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
how does this look so far?

void myScanf(int integers, char outmsg);

int main(void)
{
int input;
char outarray;
myScanf(input, outarray);

return 0;
}

void myScanf(int integers, char outmsg) {
int val;
char input[30];
fgets (input, sizeof(input)-1, stdin);
sscanf (input, "%d", &val);
}
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
Originally posted by: Aznguy1872
so a loop checking the integers and an if statement. I dont get how you guys know exactly what to do jus tby looking at it already. Are you guys majoring in computer science? Or do C programming as a living?

I majored in Computer Science, but noticing how something works and what it'll require usually isn't too language dependent until you get to the nitty-gritty of things.

Your code that you listed doesn't do what you said above:

Originally posted by: Aznguy1872
so a loop checking the integers and an if statement.

Also, your code attempts to use a different scanf function inside a function that's supposed to emulate scanf... naughty naughty! Remember, you're only looking for the first number you see! You can also change a character to its numerical form very easily by doing char - '0' or char - 48 ( they're both the same thing ).
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
kk, I just woke up. I'll be in class till 1 pm, but after that I'll work on it some more and try to create what you said above. Thanks. Computer Science eh, nice. I give peeps props who can program.
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
So instead of using the fgets, use scanf instead and then write a if statement, but how do I write an if statement that checks the first number? This is what i have now.

void myScanf(int integers, char outmsg);

int main(void)
{
int input;
char outarray;
myScanf(input, outarray);

return 0;
}

void myScanf(int integers, char outmsg) {
int val;
char input[30];
while(scanf (input, "%d", &val)) {
if ( blah blah
}
 

SunnyD

Belgian Waffler
Jan 2, 2001
32,675
146
106
www.neftastic.com
Look up the ASCII codes for numeric digits... then if away.

By the way... why are you using scanf if you're supposed to be replacing scanf? :) Re-read your assignment friend...

? make a myScanf() function that works like scanf("%d", &val) for positive integers.
? i.e. myScanf(int * p) reads digit characters from KB (use getchar()) and converts to integer.
? skips white space.
? reads digits and converts to integer until
? non-digit read, then stores in integer at *p
? You should be able to replace a call to scanf("%d", &val) with myScanf(&val).
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
Okay, let's step back here and work properly toward a solution.

You should write out step-by-step what you would do in a piece-wise fashion to solve this problem. Try to look at the information like a computer would and limit yourself to relatively basic operations. DO NOT use procedurally anything like *scan* in your methods. When you're done, post that.
 

Aznguy1872

Senior member
Aug 17, 2005
790
0
0
step 1:
a code that will read in characters from the keyboard
step 2: From the characters that are typed in, convern them to integers.
step 3: if i see a blank like a space, to skip that.
step 4: then store it into p

btw, what does this part mean " reads digits and converts to integer until non-digit read"
? i.e. myScanf(int * p) reads digit characters from
KB (use getchar()) and converts to integer.
? skips white space.
? reads digits and converts to integer until
? non-digit read, then stores in integer at *p
? You should be able to replace a call to
scanf("%d", &val) with myScanf(&val).
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
Okay, it seems your thinking is on the right track, but a little flawed. But it seems you're thinking about too much at once and not a simple and rudimentary solution.

Remember, you're going to be using getchar when you actually code this. Getchar() only grabs a single character and that's what you must work with at a time. Let's think of this as a flow.

1) grab a character from the keyboard
2) while we have a numeric character (If not, stop)
3) put that into our value (this is a tiny bit more complicated than just "putting it" in there.. there's actually a math formula you'll need... very simplistic though)
4) grab a character from the keyboard
5) Go back to 2

I believe this isn't 100% correct, but I have a question on what you need to do:

? skips white space.
? non-digit read, then stores in integer at *p

These two statements directly contradict each other. Whitespace is a non-digit and theoretically, it should cause termination. Unless this is referring to leading and trailing spaces? Where the input " 456 d" would yield 456 and quit at 'd'.