C Checking for EOF












0















I developed a C program that does addition of integers but the problem I am having with this is that it needs to take input from stdin via input text (not interactive). However after testing I can only get it to work for a single line of input when it should work for multiple, assume this sample input are all in a single input file ((Line 1) 123+456= (Line 2) 999999999+1= ).



I think it has to do with EOF and it should go through and repeat until finished. I tried putting an EOF in the first while loop but the output is very different than what it should be. The input format has to stay the same and cannot be changed. Is there anyway I can get it to do operations until the end of input file?



#include<stdio.h>
#include<stdbool.h>

int main() {
char c;
char a[100], b[100], array[101];
int n = 0;
//It should scan through everyrthing then exit and it think start arround HERE
while ((c = (char) getchar()) != '=') {
array[n] = c;
putchar(c);
n++;
}
printf("=n");
array[n] = '';
int n1 = 0, n2 = -1;
while (n1 + n2 < n) { //Splits the array into two separate char arrays
if (array[n1] == '+') {
n2 = 0;
n1 += 1;
}
if (n2 == -1) {
a[n1] = array[n1];
n1++;
} else {
b[n2] = array[n1 + n2];
n2++;
}
}
n1--;
a[n1] = b[n2] = '';
int i = 0, z = 0, p = 0;
char array2[101];
bool val = false;
while (n1 > 0 || n2 > 0) { //Addition Calculation
int x = 0, y = 0;
if (n1 > 0)
x = a[n1 - 1] - '0';

if (n2 > 0)
y = b[n2 - 1] - '0';
z = x + y;

if (val)
z = z + p;

if (z > 9) {
p = z % 10;
array2[i++] = (char) (p + '0');
p = z / 10;
val = true;

} else {
array2[i++] = (char) (z + '0');
val = false;
}
n1--;
n2--;
}
if (val) {
array2[i++] = (char) (p + '0');
array2[i] = '';
}

for (n = i - 1; n >= 0; n--) { //Print Result
printf("%c", array2[n]);
}
printf("n");

return 0;
}









share|improve this question


















  • 1





    Why don't you read ints using scanf() when you want ints? while ((c = (char) getchar()) != '=') is why your code only processes the first line.

    – Swordfish
    Nov 21 '18 at 3:31











  • I needed to find a way to split the characters between input and exit once it reaches = unless it can be done with scanf and still work the same. getchar just seemed easier since it reads character by character.

    – GameCoder
    Nov 21 '18 at 3:41











  • You have to get rid of the misconception that you need to read single chars in the first place. You want numbers? Read numbers.

    – Swordfish
    Nov 21 '18 at 3:58
















0















I developed a C program that does addition of integers but the problem I am having with this is that it needs to take input from stdin via input text (not interactive). However after testing I can only get it to work for a single line of input when it should work for multiple, assume this sample input are all in a single input file ((Line 1) 123+456= (Line 2) 999999999+1= ).



I think it has to do with EOF and it should go through and repeat until finished. I tried putting an EOF in the first while loop but the output is very different than what it should be. The input format has to stay the same and cannot be changed. Is there anyway I can get it to do operations until the end of input file?



#include<stdio.h>
#include<stdbool.h>

int main() {
char c;
char a[100], b[100], array[101];
int n = 0;
//It should scan through everyrthing then exit and it think start arround HERE
while ((c = (char) getchar()) != '=') {
array[n] = c;
putchar(c);
n++;
}
printf("=n");
array[n] = '';
int n1 = 0, n2 = -1;
while (n1 + n2 < n) { //Splits the array into two separate char arrays
if (array[n1] == '+') {
n2 = 0;
n1 += 1;
}
if (n2 == -1) {
a[n1] = array[n1];
n1++;
} else {
b[n2] = array[n1 + n2];
n2++;
}
}
n1--;
a[n1] = b[n2] = '';
int i = 0, z = 0, p = 0;
char array2[101];
bool val = false;
while (n1 > 0 || n2 > 0) { //Addition Calculation
int x = 0, y = 0;
if (n1 > 0)
x = a[n1 - 1] - '0';

if (n2 > 0)
y = b[n2 - 1] - '0';
z = x + y;

if (val)
z = z + p;

if (z > 9) {
p = z % 10;
array2[i++] = (char) (p + '0');
p = z / 10;
val = true;

} else {
array2[i++] = (char) (z + '0');
val = false;
}
n1--;
n2--;
}
if (val) {
array2[i++] = (char) (p + '0');
array2[i] = '';
}

for (n = i - 1; n >= 0; n--) { //Print Result
printf("%c", array2[n]);
}
printf("n");

return 0;
}









share|improve this question


















  • 1





    Why don't you read ints using scanf() when you want ints? while ((c = (char) getchar()) != '=') is why your code only processes the first line.

    – Swordfish
    Nov 21 '18 at 3:31











  • I needed to find a way to split the characters between input and exit once it reaches = unless it can be done with scanf and still work the same. getchar just seemed easier since it reads character by character.

    – GameCoder
    Nov 21 '18 at 3:41











  • You have to get rid of the misconception that you need to read single chars in the first place. You want numbers? Read numbers.

    – Swordfish
    Nov 21 '18 at 3:58














0












0








0








I developed a C program that does addition of integers but the problem I am having with this is that it needs to take input from stdin via input text (not interactive). However after testing I can only get it to work for a single line of input when it should work for multiple, assume this sample input are all in a single input file ((Line 1) 123+456= (Line 2) 999999999+1= ).



I think it has to do with EOF and it should go through and repeat until finished. I tried putting an EOF in the first while loop but the output is very different than what it should be. The input format has to stay the same and cannot be changed. Is there anyway I can get it to do operations until the end of input file?



#include<stdio.h>
#include<stdbool.h>

int main() {
char c;
char a[100], b[100], array[101];
int n = 0;
//It should scan through everyrthing then exit and it think start arround HERE
while ((c = (char) getchar()) != '=') {
array[n] = c;
putchar(c);
n++;
}
printf("=n");
array[n] = '';
int n1 = 0, n2 = -1;
while (n1 + n2 < n) { //Splits the array into two separate char arrays
if (array[n1] == '+') {
n2 = 0;
n1 += 1;
}
if (n2 == -1) {
a[n1] = array[n1];
n1++;
} else {
b[n2] = array[n1 + n2];
n2++;
}
}
n1--;
a[n1] = b[n2] = '';
int i = 0, z = 0, p = 0;
char array2[101];
bool val = false;
while (n1 > 0 || n2 > 0) { //Addition Calculation
int x = 0, y = 0;
if (n1 > 0)
x = a[n1 - 1] - '0';

if (n2 > 0)
y = b[n2 - 1] - '0';
z = x + y;

if (val)
z = z + p;

if (z > 9) {
p = z % 10;
array2[i++] = (char) (p + '0');
p = z / 10;
val = true;

} else {
array2[i++] = (char) (z + '0');
val = false;
}
n1--;
n2--;
}
if (val) {
array2[i++] = (char) (p + '0');
array2[i] = '';
}

for (n = i - 1; n >= 0; n--) { //Print Result
printf("%c", array2[n]);
}
printf("n");

return 0;
}









share|improve this question














I developed a C program that does addition of integers but the problem I am having with this is that it needs to take input from stdin via input text (not interactive). However after testing I can only get it to work for a single line of input when it should work for multiple, assume this sample input are all in a single input file ((Line 1) 123+456= (Line 2) 999999999+1= ).



I think it has to do with EOF and it should go through and repeat until finished. I tried putting an EOF in the first while loop but the output is very different than what it should be. The input format has to stay the same and cannot be changed. Is there anyway I can get it to do operations until the end of input file?



#include<stdio.h>
#include<stdbool.h>

int main() {
char c;
char a[100], b[100], array[101];
int n = 0;
//It should scan through everyrthing then exit and it think start arround HERE
while ((c = (char) getchar()) != '=') {
array[n] = c;
putchar(c);
n++;
}
printf("=n");
array[n] = '';
int n1 = 0, n2 = -1;
while (n1 + n2 < n) { //Splits the array into two separate char arrays
if (array[n1] == '+') {
n2 = 0;
n1 += 1;
}
if (n2 == -1) {
a[n1] = array[n1];
n1++;
} else {
b[n2] = array[n1 + n2];
n2++;
}
}
n1--;
a[n1] = b[n2] = '';
int i = 0, z = 0, p = 0;
char array2[101];
bool val = false;
while (n1 > 0 || n2 > 0) { //Addition Calculation
int x = 0, y = 0;
if (n1 > 0)
x = a[n1 - 1] - '0';

if (n2 > 0)
y = b[n2 - 1] - '0';
z = x + y;

if (val)
z = z + p;

if (z > 9) {
p = z % 10;
array2[i++] = (char) (p + '0');
p = z / 10;
val = true;

} else {
array2[i++] = (char) (z + '0');
val = false;
}
n1--;
n2--;
}
if (val) {
array2[i++] = (char) (p + '0');
array2[i] = '';
}

for (n = i - 1; n >= 0; n--) { //Print Result
printf("%c", array2[n]);
}
printf("n");

return 0;
}






c






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 3:25









GameCoderGameCoder

102




102








  • 1





    Why don't you read ints using scanf() when you want ints? while ((c = (char) getchar()) != '=') is why your code only processes the first line.

    – Swordfish
    Nov 21 '18 at 3:31











  • I needed to find a way to split the characters between input and exit once it reaches = unless it can be done with scanf and still work the same. getchar just seemed easier since it reads character by character.

    – GameCoder
    Nov 21 '18 at 3:41











  • You have to get rid of the misconception that you need to read single chars in the first place. You want numbers? Read numbers.

    – Swordfish
    Nov 21 '18 at 3:58














  • 1





    Why don't you read ints using scanf() when you want ints? while ((c = (char) getchar()) != '=') is why your code only processes the first line.

    – Swordfish
    Nov 21 '18 at 3:31











  • I needed to find a way to split the characters between input and exit once it reaches = unless it can be done with scanf and still work the same. getchar just seemed easier since it reads character by character.

    – GameCoder
    Nov 21 '18 at 3:41











  • You have to get rid of the misconception that you need to read single chars in the first place. You want numbers? Read numbers.

    – Swordfish
    Nov 21 '18 at 3:58








1




1





Why don't you read ints using scanf() when you want ints? while ((c = (char) getchar()) != '=') is why your code only processes the first line.

– Swordfish
Nov 21 '18 at 3:31





Why don't you read ints using scanf() when you want ints? while ((c = (char) getchar()) != '=') is why your code only processes the first line.

– Swordfish
Nov 21 '18 at 3:31













I needed to find a way to split the characters between input and exit once it reaches = unless it can be done with scanf and still work the same. getchar just seemed easier since it reads character by character.

– GameCoder
Nov 21 '18 at 3:41





I needed to find a way to split the characters between input and exit once it reaches = unless it can be done with scanf and still work the same. getchar just seemed easier since it reads character by character.

– GameCoder
Nov 21 '18 at 3:41













You have to get rid of the misconception that you need to read single chars in the first place. You want numbers? Read numbers.

– Swordfish
Nov 21 '18 at 3:58





You have to get rid of the misconception that you need to read single chars in the first place. You want numbers? Read numbers.

– Swordfish
Nov 21 '18 at 3:58












1 Answer
1






active

oldest

votes


















0














Using scanf() what you want is a 4-liner:



#include <stdio.h>

int main(void)
{
int a, b;
char op, ch;
while (scanf(" %d %c %d %c", &a, &op, &b, &ch) == 4 && op == '+' && ch == '=')
printf("%d+%d=%dn", a, b, a + b);
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404841%2fc-checking-for-eof%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Using scanf() what you want is a 4-liner:



    #include <stdio.h>

    int main(void)
    {
    int a, b;
    char op, ch;
    while (scanf(" %d %c %d %c", &a, &op, &b, &ch) == 4 && op == '+' && ch == '=')
    printf("%d+%d=%dn", a, b, a + b);
    }





    share|improve this answer




























      0














      Using scanf() what you want is a 4-liner:



      #include <stdio.h>

      int main(void)
      {
      int a, b;
      char op, ch;
      while (scanf(" %d %c %d %c", &a, &op, &b, &ch) == 4 && op == '+' && ch == '=')
      printf("%d+%d=%dn", a, b, a + b);
      }





      share|improve this answer


























        0












        0








        0







        Using scanf() what you want is a 4-liner:



        #include <stdio.h>

        int main(void)
        {
        int a, b;
        char op, ch;
        while (scanf(" %d %c %d %c", &a, &op, &b, &ch) == 4 && op == '+' && ch == '=')
        printf("%d+%d=%dn", a, b, a + b);
        }





        share|improve this answer













        Using scanf() what you want is a 4-liner:



        #include <stdio.h>

        int main(void)
        {
        int a, b;
        char op, ch;
        while (scanf(" %d %c %d %c", &a, &op, &b, &ch) == 4 && op == '+' && ch == '=')
        printf("%d+%d=%dn", a, b, a + b);
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 3:54









        SwordfishSwordfish

        10.2k11437




        10.2k11437
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404841%2fc-checking-for-eof%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?