Link to home
Start Free TrialLog in
Avatar of OPT Expert
OPT Expert

asked on

scanf and assignment suppression

Hi,

I am stuck on a question I have to do and would appreciate some help. Here is the question:

Write a C program called q6.c. It should read input from standard input until end-of-file is encountered and echo the total. It should skip over everything that's not in 0 ... 9 and just pick out the integers.
 
 Here is an example:
$
$ cat numbers3
%&4**XY6
3~~=VR)[2*n1
a*(b
10Hello


$
$ q6 < numbers3
4
6
3
2
1
10
total = 26
$


I understand that you need to use scanf with assignment suppression, but stuck.

Thanks in advance.
Avatar of ozo
ozo
Flag of United States of America image

what have you tried, and what are you stuck on?
You wouldn't necessarily have to use scanf with assignment suppression; you could also get a character at a time--but scanf might be easiest and fastest.  You want to consider what format string to use to skip all the non-digits (I'm assuming you know how to get the digits, because you asked about assignment suppression and you wouldn't want to suppress the digits.)
This looks like a homework assignment.  Is that correct?  I'm going to assume that it is (for now).

Is using scanf() a requirement.  Are these integers supposed to be type int or type long or could they be arbitrary length integers?  (If this is a class assignment, what level class are you in?  If it's an introductory class and it just started, I doubt the teacher is trying to fool you just yet.  It wasn't until the first week of my second semester did the professor fool the entire class to get our attention.)
Avatar of OPT Expert
OPT Expert

ASKER

Here is code i wrote to capture the digits but ignore white spaces.

I just do know how to go about using assignment suppression with scanf to ignore all characters except for numbers.

#include <stdio.h>

int main(void)
{
int total, in;
scanf("%i",&in);

while(in>=0 && in <=9)
{
printf("%i\n",in);
total=total + in;
scanf("%i",&in);
}

total= total + in;
printf("%i\n",in);
printf("Total = %i\n", total);
return 0;
}


any help would be appreciated. Thanks
CONVERSIONS
     Following the % character introducing a conversion, there may be a number
     of flag characters, as follows:

     *        Suppresses assignment.  The conversion that follows occurs as
              usual, but no pointer is used; the result of the conversion is
              simply discarded.


     The following conversions are available:
     [     Matches a nonempty sequence of characters from the specified set of
           accepted characters; the next pointer must be a pointer to char,
           and there must be enough room for all the characters in the string,
           plus a terminating NUL character.  The usual skip of leading white
           space is suppressed.  The string is to be made up of characters in
           (or not in) a particular set; the set is defined by the characters
           between the open bracket [ character and a close bracket ] charac-
           ter.  The set excludes those characters if the first character
           after the open bracket is a circumflex ^.  To include a close
           bracket in the set, make it the first character after the open
           bracket or the circumflex; any other position will end the set.
           The hyphen character - is also special; when placed between two
           other characters, it adds all intervening characters to the set.
           To include a hyphen, make it the last character before the final
           close bracket.  For instance, `[^]0-9-]' means the set ``everything
           except close bracket, zero through nine, and hyphen''.  The string
           ends with the appearance of a character not in the (or, with a cir-
           cumflex, in) set or when the field width runs out.
Somethings to think about: what would stop the program at the end of the file?  What does the program do when a character is not 0-9?  Is the first character is the file 0-9?
Again, do you have to use scanf?  IMHO, the project is simpler without it.
The main things wrong with your program are, your test that "in" is between 0 and 9 won't necessarily work when scanf fails to find an integer (it could still be between 0 and 9 then), and also that scanf won't consume its input if it doesn't find an integer, so you'll end up with an infinite loop.  Ozo's suggestion from the scanf man-page would be one way of skipping the non-numeric input (it's what I was trying to hint at before without wanting to be too specific about a homework problem), but just reading a character when scanf doesn't find an integer would also work.

Also, from your problem description, you should be checking for end-of-file, and your numbers shouldn't be restricted to 0-9 (scanf can read multiple digits at once).
a number <0 or >9 appeared to be the stopping condition
(10 in the numbers3 example)
But it may also be a good idea to consider what happens when no such number appears in the input.
I thought we could address things one step at a time and deal with that after adding assignment suppression to skip over non-digits, which would be adequate for the example which does contain a number >9
We could then also deal with the issue of initializing total.
I've requested that this question be deleted for the following reason:

Question is very old, for some time i did not use EE and then I abandoned the project in question.
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I agree with the objection because the author was largely non-responsive and people donated their time (without so much as a "thank you" from the author).  I propose that the best answer is 40636078.