I find that this function
//letter counter
void letter_counter(void) {
int i = 0, counter = 0;
char word[80];
char letter, function;
printf("Type in a word: ");
gets(word);
printf("Type a letter: ");
scanf("%c", &letter);
while(word[i] != '\0') {
if(word[i] == letter) {
counter++;
}
i++;
}
printf("\nThat letter appeared %d time(s)\n", counter);
}
is producing "Type in a word: Type in a letter: _" (the underscore resembles 'waiting for input').
However having it in main with nothing else fixes the problem. ie:
#include <string.h>
#include <stdio.h>
int main() {
int i = 0, counter = 0;
char word[80];
char letter, function;
printf("Type in a word: ");
gets(word);
printf("Type a letter: ");
scanf("%c", &letter);
while(word[i] != '\0') {
if(word[i] == letter) {
counter++;
}
i++;
}
printf("\nThat letter appeared %d time(s)\n", counter);
}
The full program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void letter_counter(void);
void calculator(void);
int main() {
char function;
printf("Type in a function you would like to use: ");
printf("\nFor letter counter type \"l\"\n");
printf("For calculator type \"c\"");
printf("\n\nEnter a function: ");
while(function != 'e') {
scanf("%c", &function);
if(function == 'l') {
letter_counter();
printf("\nEnter another function or type \"e\" to exit: ");
} else if(function == 'c') {
calculator();
printf("\n\nEnter another function or type \"e\" to exit: ");
} else if(function == 'e') {
exit(0);
}
}
}
//letter counter
void letter_counter(void) {
int i = 0, counter = 0;
char word[80];
char letter, function;
printf("Type in a word: ");
gets(word);
printf("Type a letter: ");
scanf("%c", &letter);
while(word[i] != '\0') {
if(word[i] == letter) {
counter++;
}
i++;
}
printf("\nThat letter appeared %d time(s)\n", counter);
}
//calculator
void calculator(void) {
printf("For: Addition type \"a\"\n : Subtraction type \"s\"\n : Division type \"d\"");
char type;
printf("\n\nEnter calculation type: ");
scanf("%s", &type);
int i = 0, j = 0, k = 0, p = 0, g = 0, h = 0, u = 0, o = 0;
//addition
if(type == 'a') {
printf("\nEnter two numbers to add separated by a space: ");
scanf("%d %d", &i, &j);
i = i + j;
printf("Answer is: %d", i);
//subtraction
} else if(type == 's') {
printf("\nEnter two numbers to subtract separated by a space: ");
scanf("%d %d", &k, &p);
k = k - p;
printf("Answer is: %d", k);
//division
} else if(type == 'd') {
printf("\nEnter two numbers to divide separated by a space: ");
scanf("%d %d", &g, &h);
g = g / h;
printf("Answer is: %d", g);
//multiplication
} else if(type == 'm') {
printf("\nEnter two numbers to multiply separated by a space: ");
scanf("%d %d", &o, &u);
o = o * u;
printf("Answer is: %d", o);
}
}
Hope anyone can help :P
It's not a huge deal, but I really want to know why it's not working