Hello again! Today's problem is one that is bothering me. My research into it has once again gone to waste, so I hope somebody here can help me understand why this is happening. The problem is everything is working honky dory, however I find myself having to enter the same command up to 3 times to get it to register. How do I make the program more efficient so I only have to enter a command once?

#include <stdio.h>
#define MAX 105 /*what the pop price is in cents*/
#define MIN 5

char c;
int leftover=POPPRICE; //
int value; // what was entered

void entryChecker (char c)
{
 if (c=='d' || c=='D'){
        printf("Dime detected!");
        c='\0';}
else if (c=='q' || c=='Q'){
        printf("Quarter detected!");
        c='\0';}
else if (c=='n' || c=='N'){
        printf("Nickel detected!");
        c='\0';}
else if (c=='r' || c=='R'){
        printf("Returning money!");
        c='\0';}
else{
        printf("That was not a valid command.");}
}

int main(int argc, char *argv[])
{

        value=atoi(argv[1]);
        if ((value % 5) != 0&&value<=MAX&&value>=MIN)
        {
                printf("Please enter a number of multiples of 5\n");
                printf("Make sure the price is between 5 - 105 cents\n");
}
        else
        {

        printf("The price of pop is %d, cents\n", value);
        printf("Please insert any combination of nickels [N or n],\n");
        printf("dimes [D or d] or quarters [Q or q].\n");
        printf("You can also return your money to you by pressing R or r\n");
        printf("%d",value);
        while ((c=getchar())!='e' && (c=getchar())!='E')
        {
                scanf("%c",&c);
                entryChecker (c);
        }
}
}

Once again, I deeply appreciate any assistance thrown my way.

EDIT: I also realize using atoi is not the best way to handle this, however for time constraints it's what i'm using now.

To answer my own question, the problem seems to have stemmed from the while loop from the main. Since that required input of some sort, and then the method required input, it was constantly waiting for me to fill it with information.

Changing the while loop seemed to have worked. Can anybody confirm that this was infact, the problem, or am I assuming something incorrectly?

while ((c=getchar())!='e' && (c=getchar())!='E') Think about this a little bit more.
You are reading from stdin twice: (c=getchar())!='e' and then you read again another character (c=getchar())!='E' . Most like it an entered letter and the "ENTER" key. If that evaluates to TRUE in the loop you read one more time with scanf("%c",&c); without prompting for it.

Is not that too much reading?

This article has been dead for over six months. Start a new discussion instead.