Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C looping using EOF, how to read next line?

I’m facing an issue while trying to resolve a particular problem in C. User input must be read in encrypted form and the output must be the decrypted message, that is, it is a replacement cipher.The problem is that when I read the first input, the program loops printing the decrypted message, it doesn’t ask to read other inputs.
I’m really confused about this as I was told that I should use EOF, something I’m not very knowledgeable about. I could even solve it another way, but I need to implement the EOF, and because I don’t know that, I’m having difficulty solving the exercise.

I am aware that the code I created is not the best for the resolution, however, it might work if someone helps me with this loop issue.

Source Code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include <stdio.h>
#include <string.h>

void alterChar(int i, char phrase[])
{
    for (i = 0; phrase[i] != '\0'; i++)
    {
        if (phrase[i] == '@')
        {
            phrase[i] = 'a';
        }
                
        if (phrase[i] == '&')
        {
            phrase[i] = 'e';
        }
                
        if (phrase[i] == '!')
        {
            phrase[i] = 'i';
        }
                
        if (phrase[i] == '*')
        {
            phrase[i] = 'o';
        }
                
        if (phrase[i] == '#')
        {
            phrase[i] = 'u';
        }
                
        printf("%s\n", phrase);
    }
}

int main()
{
    int i;
    char phrase[256];
    
    while (scanf("%[^\n]s", phrase) != EOF)
    {
        ///scanf(" %[^\n]s", phrase);
    
        alterChar(i, phrase);
    }
    
    return 0;
}

>Solution :

i think just putting the printf outside of the loop help and use feof (fetch en of file) to know where you are.

#include <stdio.h>
#include <string.h>

void alterChar(char phrase[]) {
    for (int i = 0; phrase[i] != '\0'; i++) {
        if (phrase[i] == '@') {
            phrase[i] = 'a';
        }

        if (phrase[i] == '&') {
            phrase[i] = 'e';
        }

        if (phrase[i] == '!') {
            phrase[i] = 'i';
        }

        if (phrase[i] == '*') {
            phrase[i] = 'o';
        }

        if (phrase[i] == '#') {
            phrase[i] = 'u';
        }
    }
    printf("%s\n", phrase);
}

int main() {
    char phrase[256];

    while (!feof(stdin)) {
        scanf(" %[^\n]s", phrase);
        alterChar(phrase);
    }

    return 0;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading