so I'll elaborate on the title -
Iv'e declared a few functions and then called for them in main
.
All functions use scanf
, and are supposed to do their action on an identical input.
My problem is that I'm prompted to give an input for each function (each scanf
), but I want one single prompt. Considering all functions use an identical input, I assumed it would be straight forward.
Here's the big catch though - my input is guaranteed to be a series of letters (i.e a word - like 'hello'), and I can't use any type of strings, arrays or pointers.
I found absolutely nothing about this anywhere. Can this be done?
edit - example code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^
functions - all the functions we later call for in 'main'
*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*/
//*^*^*^*^*^*^* function 1 (find sum of letter values)
int word_sum() {
char letter = '\0';
int sum = 0;
while (letter != '.') {
sum += (int)letter;
scanf("%c", &letter);
}
return sum;
}
//*^*^*^*^*^*^* function 2 (number of letters in input)
int char_count() {
char letter = '\0';
int i = 0;
for (i = 0; letter != '.'; i++) {
scanf("%c", &letter);
}
return i - 1;
}
//*^*^*^*^*^*^* function 3 (does something with same input)
//more functions
/*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^
main
*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*/
int main() {
printf("Enter word:\n");
printf("the sum is %d\nchar num is %d\a", word_sum(), char_count());
return(0);
}
/* ===================
input = hi.
desired output -
--------------------------
Enter word:
hi.
the sum is 209
char num is 2
--------------------------
current output -
--------------------------
Enter word:
hi.
hi.
the sum is 219
char num is 2
--------------------------
=====================*/
I use '.' at the end of the input because we are guaranteed all inputs we get will end with it.
As this shows, I'm unable to use a single prompt to initialize both functions.
Not only do I have to type the word twice, but (I'm guessing) since it reads also 'new line' or something, even the function result gets skewed.
I know this specifically can be done with a single function and scanf
.
But I will have at least 5 more functions later on, and we have line limitation per function. So it would seem I'll have to confront this issue eventually one way or another.
That being said, if you can think of a different solution that maintains these functions, it could be relevant.
Thank you all so much for all your help so far!
scanf()
to read an input likehello
without storing that input in an array? And how willscanf()
act differently on this same input multiple times? What is the goal? – ad absurdumchar myLetter = 'h'
for each of them with lettere
l
l
o
and thenprintf("%c", myLetter)
each of them next to each other. You need to use character types. gl with the homework/scanf("%c", myLetter)
– user8377060