Hi. I want to enter a number of chars on a command line. For example....... a b c 4 6
How do I take these valuesin and treat them as seperate values?
I want to use getchar instead of getch or scanf, thanks
Hi. I want to enter a number of chars on a command line. For example....... a b c 4 6
How do I take these valuesin and treat them as seperate values?
I want to use getchar instead of getch or scanf, thanks
Have you looked at an example from the API reference? You should be able to modify that code easily to do what you want.
I really don't see how it's limited. I think is capable of doing exactly what you've asked.
If you know how many "char"s to ask for input, then just loop that many times and store each "getchar" value into some variable, say an array. If you dont know how many input values will be given, then there isn't really anything magical to do: you need a delimiter. In the example in the link I gave, the delimiter is a ".", which you can of course change.
If you still think it is limited, then I think you're maybe missing something in your requirements that you haven't told us, in which case you should explain more what you need to do with examples.
I've written the following rough piece of code.......
What I'm hoping for here is that I want to take in 2 chars, but exclude spaces. Unfortunately I'm still seeing ' ' in putchar. Can anyone see where I'm going wrong?Code:for(i=0; i<2 && c!=' '; i++) { if(c != ' ') { c=getchar(); putchar (c); } }
You want to take a char in, before you make an if() statement involving it. Of course, c can only be ONE char, not two, so do you really want to skip every other char?
The outer loop should be set to the strlen() of the line, OR be a while loop, that lasts until the end of the line (hopefully marked with a newline).
IMO you should ditch getchar(), and use an index in a for or while loop, to get what you want. It's very easy that way.
fgets() puts the char's into your char array, then you walk through each char, using an index:
Code:for(i = 0; i < strlen(charArrayName); i++) { //your logic code goes in here - just an example: if(charArrayName[i] != ' ') putchar(charArrayName[i]); }