I have this program where it lets the user input a list of numbers, then the program finds the largest number amongst the inputs, and count how many times that largest number was inputted.
When I use space as a separator, the program runs well. But when I use a comma as a separator, there seems to be a logical error.
Here is my source code:
int i, numberOfIntegers, listOfIntegers, largest = 0, occurrence = 0;
printf("\n \t \t \t Finding the LARGEST integer \n");
printf("\n How many integers do you want to enter? ");
scanf("%i", &numberOfIntegers);
printf("\n Input %i list of integers: \n ", numberOfIntegers);
for (i = 1; i <= numberOfIntegers; i )
{
printf("\t");
scanf("%i", &listOfIntegers);
if (listOfIntegers > largest)
{
largest = listOfIntegers;
occurrence = 1;
}
else if (listOfIntegers == largest)
{
occurrence ;
}
}
printf("\n The largest value is %i and the number of occurrence is %i \n ", largest, occurrence);
return 0;
Here is an example of output where I use a comma as a separator:
How many integers do you want to enter? 4
Input 4 list of integers:
5, 6, 6, 6
The largest value is 5 and the number of occurrence is 4
Whereas, the correct output should be:
How many integers do you want to enter? 4
Input 4 list of integers:
5, 6, 6, 6
The largest value is 6 and the number of occurrence is 3
Can someone point out where exactly am I doing it wrong?
CodePudding user response:
The basic problem is that when reading input in C, you need to account for every character (potentially) in the input -- every space and every newline and every comma or other punctuation character, as well as all the values you actually care about and want to read.
When using scanf
to read input, whitespace is special as it is easy to ignore. Every %
directive in the scanf string except for %c
, %[
and %%
will ignore leading whitespace automatically. With "%i"
like you are using, the newline after the 4
in your input is automatically ignored (skipped) by the first scanf call in the loop, and the spaces between the numbers will skipped by later calls. However, any commas (or other punctuation characters) will not be. You need to skip (read) them explicitly. As it is, when your program gets to the scanf
call in the second iteration of the loop, the next character to be read is ,
(not a digit and not whitespace), so the conversion fails and nothing is stored into listOfIntegers
and a 0 is returned (no directives matched). Since you ignore the return value of scanf, you don't notice this and blissfully continue with the same value left over from the first iteration.
One thing you could try is scanf("%i,", &listOfIntegers)
in your loop. This will read a single ,
if it appears immediately after your number. If the character after the number is not a ,
it will do nothing. While this will work for your example, it wont work for an input like
5 , 6, 6 , 6
due to the extra space before the comma. A more accepting possibility is
scanf("%i%*[ \t,;.]", &listOfIntegers)
which will skip (and ignore) all spaces, tabs, commas, semicolons and periods after the number.
In any case, it would also be a good idea to check the return value of scanf:
if (scanf("%i%*[ \t,;.]", &listOfIntegers) < 1) {
... something is wrong -- the next input is not a number
to catch someone entering a letter or some other non-number input.