0
votes

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!

1
"can't use any type of strings, arrays or pointers"-- kind of limiting your options, no?ad absurdum
yes, unfortunately. this is part of a homework exercise...Ok_j
You are going to need to provide some code; your question is unclear. How will you use scanf() to read an input like hello without storing that input in an array? And how will scanf() act differently on this same input multiple times? What is the goal?ad absurdum
you can use char myLetter = 'h' for each of them with letter e l l o and then printf("%c", myLetter) each of them next to each other. You need to use character types. gl with the homework/ scanf("%c", myLetter)user8377060
What do you say like this ?BLUEPIXY

1 Answers

0
votes

What you need to do is break your actual function into step functions that will perform one step of the calculation on a single character as -

int sum_word(char letter, int previous_sum) {
    return letter + previous_sum;
}

int count_word(char letter, int previous_count) {
    return previous_count + 1;
}

Now you need to read character by character and process each step as

void process_input(void) {
    char c;
    sum=0, count=0;
    while(scanf("%c", &c) == 1 && c != '\n'){
        sum = sum_word(c, sum);
        count = count_word(c, count);
    }
    printf ("Sum = %d, Count = %d\n", sum, count);
}

You can also use global/static variables for sum and count if you don't want to pass them along every time.

Now whatever other operations you want to do, just make similar one letter processing functions for them and call them where I called sum_word and count_word.

Ofcourse you cannot only support functions that can be broken down into a step function. Functions which need to look back cannot be supported.