0

I´m suppose to count the votes of 2 candidates and show the result of who won, but when the user tells how many electors there are, the programs only runs half of the times it should. Also, for some reason the questions always appears two times, like if it passes through the loop but doesnt do anything (if n is an odd number, it approaches down.

I´ve also tried to do it using the loop 'for', but I had the same problem

#include<stdin.h>
#include<stdlib.h>

void main()
{
  int sumj=0,summ=0,counter=0,n;
  char vote;

  printf("how many electors are there?\n");
  scanf("%d",&n);

  do
  {
    printf("Whats your vote? m for maria j for james\n");
    scanf("%c",&vote);

    if (vote=='j')
    {
        sumj++;
    }
    if (vote=='m')
    {
        summ++;
    }

    counter++;
  } while (counter<n);

  if (sumj>summ)
    printf("james won");
  if (summ>sumj)
    printf("maria won");
  if (sumj==summ)
    printf("its a tie");
}
Porses
  • 11
  • 5
  • First, what does `scanf()` actually read? Second, does `scanf()` even work? You never check either. – Andrew Henle May 06 '19 at 21:54
  • 3
    most likely you are pressing the `j` key and then hitting enter. The `scanf` will leave the carriage return character in the buffer for the next round. See this question and answer for details: https://stackoverflow.com/q/5240789/1212725 – bruceg May 06 '19 at 22:04
  • 1
    Possible duplicate of [scanf() leaves the new line char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer) – bruceg May 06 '19 at 22:06
  • In a standards conforming implementation, the allowable declarations for `main` for are `int main (void)` and `int main (int argc, char *argv[])` (which you will see written with the equivalent `char **argv`). [C11 Standard - §5.1.2.2.1 Program startup(p1)](http://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.1p1). See also: [What should main() return in C and C++?](http://stackoverflow.com/questions/204476/) – David C. Rankin May 06 '19 at 22:54

3 Answers3

2

Also, for some reason the questions always appears two times, like if it passes through the loop but doesnt do anything

This is a big clue, of course... if the loop is executing twice for each input but not seeming to do anything the second time, then that would explain why it only seems to execute half as many times as you expect, right?

printf("Whats your vote? m for maria j for james\n");
scanf("%c",&vote);

Hmmm, scanf() is often at the root of these kinds of problems. Your scanf() call only reads one character at a time, right? But when you enter a vote, how many characters do you type? I bet you type something like j<return>. That's two characters, not one.

Set a breakpoint on the first if in the loop and see if half the times you hit it, the value of vote is \n.

Caleb
  • 124,013
  • 19
  • 183
  • 272
0

Scanf is returning whitespace characters. Since you only look for 'j' or 'm' you are not seeing these, but it is still incrementing counter each time through the loop.

If you add a statement inside your loop you can see this:

do
{
    ...
    printf("(vote='%c' counter=%d)\n", vote, counter);
    ...
} while (counter<n);

It looks to me like if you enter three electors, your actual input stream is: '3', '\n', 'm', '\n'. The 3 gets consumed by the scanf, but the '\n' (when you pressed enter) ends up being the first vote, the 'm' is the second vote, and the other '\n' is the third vote.

There are several ways you could fix this, but the easiest is just to ignore any characters that are not votes:

if (vote=='j')
{
    sumj++;
    counter++;
}
else if (vote=='m')
{
    summ++;
    counter++;
}

While the whitespace characters still cause the loop to run through, it will do nothing.

Alcamtar
  • 1,478
  • 13
  • 19
  • Thank you! it worked just the way you said, the whitespace characters are still running but have no effect in the final result. I putted fflush(stdin) before the scanf and the problem was solved, but I dont understanda exactily why, can you explain it to me? – Porses May 06 '19 at 22:24
  • @Porses `fflush(stdin)` is *Undefined Behavior* on a majority of systems and is Non-Portable. Instead, *empty_stdin* after each call to `scanf`, e.g. `for (int c = getchar(); c != '\n' && c != EOF; c = getchar()) {}` – David C. Rankin May 06 '19 at 22:36
0

I am providing you a java code for what you are doing, I have tried it using different input and it runs complete,

your code seems to be fine it should run, what I am doing is same is what you did, here it is looping through all electors and giving output who won... it counts the votes which is sumj or summ and compares them and prints who won... hope I helped by providing an equivalent code.

import java.lang.Math;
import java.util.*;

public class HelloWorld
{
    public static void main(String[] args)
    {
        int sumj=0,summ=0,n;
        char vote=0;

        Scanner scan = new Scanner(System.in);

        System.out.println("how many electors are there?");
        n = scan.nextInt();
        System.out.println("here are " + n + " Elector's are there\n\n");

        for(int i=0;i<n;i++)
        {
            System.out.println("Whats your vote? m for maria j for james\n");

            vote = scan.next().charAt(0);

            if(vote == 'j')
            {
                sumj++; 
            }
            else if(vote == 'm')
            {
                summ++;
            }
        }

        if(sumj > summ)
        {
            System.out.println("james won"); 
        }
        else if(summ > sumj)
        {
            System.out.println("maria won"); 
        }
        else
        {
            System.out.println("It's a Tie"); 
        }
}
  • 2
    While your intent is fine, posing a `java` solution to a question tagged `c` isn't an appropriate answer. You are a new contributor, so I will not downvote, but do not be surprised if others do. You may want to consider deleting the answer. – David C. Rankin May 06 '19 at 22:39
  • 1
    Please delete this. – Jonathan Leffler May 07 '19 at 01:15