C language: The Array did not return a correct value












0















I have an assignment in C language that requires to ask users to enter values to arrays. My idea is createing two different arrays which one contains integer values and the other holds character values. This is my code so far:



#include <stdio.h>

int main()
{
char continued;
int i = 0;
char instrType[10];
int time[10];

printf("nL-lock a resource");
printf("nU-unlock a resource");
printf("nC-compute");
printf("nPlease Enter The Instruction Type");
printf(" and Time Input:");
scanf("%c", &instrType[0]);
scanf("%d", &time[0]);
printf("nContinue? (Y/N) ");
scanf("%s", &continued);
i = i + 1;

while (continued == 'Y' || continued == 'y')
{
printf("nL-lock a resource");
printf("nU-unlock a resource");
printf("nC-compute");
printf("nPlease Enter The Instruction Type ");
printf("Time Input:");
scanf("%c", &instrType[i]);
scanf("%d", &time[i]);
printf("nContinue? (Y/N) ");
scanf("%s", &continued);
i = i + 1;
}

return 0;
}


The expected value should be: L1 L2 C3 U1
My Screenshotenter image description here



The loop just stopped when I tried to enter new values and the condition did not check the value even I entered 'Y' meaning 'yes to continue' please help :(










share|improve this question

























  • Possible duplicate of How to do scanf for single char in C

    – Swordfish
    Nov 22 '18 at 2:49
















0















I have an assignment in C language that requires to ask users to enter values to arrays. My idea is createing two different arrays which one contains integer values and the other holds character values. This is my code so far:



#include <stdio.h>

int main()
{
char continued;
int i = 0;
char instrType[10];
int time[10];

printf("nL-lock a resource");
printf("nU-unlock a resource");
printf("nC-compute");
printf("nPlease Enter The Instruction Type");
printf(" and Time Input:");
scanf("%c", &instrType[0]);
scanf("%d", &time[0]);
printf("nContinue? (Y/N) ");
scanf("%s", &continued);
i = i + 1;

while (continued == 'Y' || continued == 'y')
{
printf("nL-lock a resource");
printf("nU-unlock a resource");
printf("nC-compute");
printf("nPlease Enter The Instruction Type ");
printf("Time Input:");
scanf("%c", &instrType[i]);
scanf("%d", &time[i]);
printf("nContinue? (Y/N) ");
scanf("%s", &continued);
i = i + 1;
}

return 0;
}


The expected value should be: L1 L2 C3 U1
My Screenshotenter image description here



The loop just stopped when I tried to enter new values and the condition did not check the value even I entered 'Y' meaning 'yes to continue' please help :(










share|improve this question

























  • Possible duplicate of How to do scanf for single char in C

    – Swordfish
    Nov 22 '18 at 2:49














0












0








0








I have an assignment in C language that requires to ask users to enter values to arrays. My idea is createing two different arrays which one contains integer values and the other holds character values. This is my code so far:



#include <stdio.h>

int main()
{
char continued;
int i = 0;
char instrType[10];
int time[10];

printf("nL-lock a resource");
printf("nU-unlock a resource");
printf("nC-compute");
printf("nPlease Enter The Instruction Type");
printf(" and Time Input:");
scanf("%c", &instrType[0]);
scanf("%d", &time[0]);
printf("nContinue? (Y/N) ");
scanf("%s", &continued);
i = i + 1;

while (continued == 'Y' || continued == 'y')
{
printf("nL-lock a resource");
printf("nU-unlock a resource");
printf("nC-compute");
printf("nPlease Enter The Instruction Type ");
printf("Time Input:");
scanf("%c", &instrType[i]);
scanf("%d", &time[i]);
printf("nContinue? (Y/N) ");
scanf("%s", &continued);
i = i + 1;
}

return 0;
}


The expected value should be: L1 L2 C3 U1
My Screenshotenter image description here



The loop just stopped when I tried to enter new values and the condition did not check the value even I entered 'Y' meaning 'yes to continue' please help :(










share|improve this question
















I have an assignment in C language that requires to ask users to enter values to arrays. My idea is createing two different arrays which one contains integer values and the other holds character values. This is my code so far:



#include <stdio.h>

int main()
{
char continued;
int i = 0;
char instrType[10];
int time[10];

printf("nL-lock a resource");
printf("nU-unlock a resource");
printf("nC-compute");
printf("nPlease Enter The Instruction Type");
printf(" and Time Input:");
scanf("%c", &instrType[0]);
scanf("%d", &time[0]);
printf("nContinue? (Y/N) ");
scanf("%s", &continued);
i = i + 1;

while (continued == 'Y' || continued == 'y')
{
printf("nL-lock a resource");
printf("nU-unlock a resource");
printf("nC-compute");
printf("nPlease Enter The Instruction Type ");
printf("Time Input:");
scanf("%c", &instrType[i]);
scanf("%d", &time[i]);
printf("nContinue? (Y/N) ");
scanf("%s", &continued);
i = i + 1;
}

return 0;
}


The expected value should be: L1 L2 C3 U1
My Screenshotenter image description here



The loop just stopped when I tried to enter new values and the condition did not check the value even I entered 'Y' meaning 'yes to continue' please help :(







c arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 2:46









Swordfish

9,34811336




9,34811336










asked Nov 22 '18 at 2:34









Lee0neLee0ne

114




114













  • Possible duplicate of How to do scanf for single char in C

    – Swordfish
    Nov 22 '18 at 2:49



















  • Possible duplicate of How to do scanf for single char in C

    – Swordfish
    Nov 22 '18 at 2:49

















Possible duplicate of How to do scanf for single char in C

– Swordfish
Nov 22 '18 at 2:49





Possible duplicate of How to do scanf for single char in C

– Swordfish
Nov 22 '18 at 2:49












2 Answers
2






active

oldest

votes


















0














You are comparing a string with a character that is instead of using scanf("%s",&continued) try using "%c"






share|improve this answer































    0














    The main problem is scanf("%c", &char) because scanf() after had read the input print a n to pass at the next line, this cause that the next scanf() instead of reading your input, go to read n, causing the failure in the reading of the input.
    To avoid this problem put a space before %c ==> scanf(" %c", &char)



    #include <stdio.h>

    int main()
    {
    char continued;
    int i = 0;
    char instrType[10];
    int time[10];

    do
    {
    printf("L-lock a resourcen");
    printf("U-unlock a resourcen");
    printf("C-computen");
    printf("Please Enter The Instruction Type and Time Input: ");
    scanf(" %c%d", &instrType[i], &time[i]);
    printf("Continue? (Y/N) ");
    scanf(" %c", &continued);
    i++;
    } while (continued == 'Y' || continued == 'y');

    return 0;
    }


    Other things:



    Instead of i = i + 1 you can use i++



    Instead of using a while() is better using a do{...}while() for saving some line of code.



    You can concatenate more inputs in a single line ==> scanf(" %c%d", &instrType[i], &time[i])






    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%2f53423106%2fc-language-the-array-did-not-return-a-correct-value%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You are comparing a string with a character that is instead of using scanf("%s",&continued) try using "%c"






      share|improve this answer




























        0














        You are comparing a string with a character that is instead of using scanf("%s",&continued) try using "%c"






        share|improve this answer


























          0












          0








          0







          You are comparing a string with a character that is instead of using scanf("%s",&continued) try using "%c"






          share|improve this answer













          You are comparing a string with a character that is instead of using scanf("%s",&continued) try using "%c"







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 2:41









          Renjith RajuRenjith Raju

          506




          506

























              0














              The main problem is scanf("%c", &char) because scanf() after had read the input print a n to pass at the next line, this cause that the next scanf() instead of reading your input, go to read n, causing the failure in the reading of the input.
              To avoid this problem put a space before %c ==> scanf(" %c", &char)



              #include <stdio.h>

              int main()
              {
              char continued;
              int i = 0;
              char instrType[10];
              int time[10];

              do
              {
              printf("L-lock a resourcen");
              printf("U-unlock a resourcen");
              printf("C-computen");
              printf("Please Enter The Instruction Type and Time Input: ");
              scanf(" %c%d", &instrType[i], &time[i]);
              printf("Continue? (Y/N) ");
              scanf(" %c", &continued);
              i++;
              } while (continued == 'Y' || continued == 'y');

              return 0;
              }


              Other things:



              Instead of i = i + 1 you can use i++



              Instead of using a while() is better using a do{...}while() for saving some line of code.



              You can concatenate more inputs in a single line ==> scanf(" %c%d", &instrType[i], &time[i])






              share|improve this answer




























                0














                The main problem is scanf("%c", &char) because scanf() after had read the input print a n to pass at the next line, this cause that the next scanf() instead of reading your input, go to read n, causing the failure in the reading of the input.
                To avoid this problem put a space before %c ==> scanf(" %c", &char)



                #include <stdio.h>

                int main()
                {
                char continued;
                int i = 0;
                char instrType[10];
                int time[10];

                do
                {
                printf("L-lock a resourcen");
                printf("U-unlock a resourcen");
                printf("C-computen");
                printf("Please Enter The Instruction Type and Time Input: ");
                scanf(" %c%d", &instrType[i], &time[i]);
                printf("Continue? (Y/N) ");
                scanf(" %c", &continued);
                i++;
                } while (continued == 'Y' || continued == 'y');

                return 0;
                }


                Other things:



                Instead of i = i + 1 you can use i++



                Instead of using a while() is better using a do{...}while() for saving some line of code.



                You can concatenate more inputs in a single line ==> scanf(" %c%d", &instrType[i], &time[i])






                share|improve this answer


























                  0












                  0








                  0







                  The main problem is scanf("%c", &char) because scanf() after had read the input print a n to pass at the next line, this cause that the next scanf() instead of reading your input, go to read n, causing the failure in the reading of the input.
                  To avoid this problem put a space before %c ==> scanf(" %c", &char)



                  #include <stdio.h>

                  int main()
                  {
                  char continued;
                  int i = 0;
                  char instrType[10];
                  int time[10];

                  do
                  {
                  printf("L-lock a resourcen");
                  printf("U-unlock a resourcen");
                  printf("C-computen");
                  printf("Please Enter The Instruction Type and Time Input: ");
                  scanf(" %c%d", &instrType[i], &time[i]);
                  printf("Continue? (Y/N) ");
                  scanf(" %c", &continued);
                  i++;
                  } while (continued == 'Y' || continued == 'y');

                  return 0;
                  }


                  Other things:



                  Instead of i = i + 1 you can use i++



                  Instead of using a while() is better using a do{...}while() for saving some line of code.



                  You can concatenate more inputs in a single line ==> scanf(" %c%d", &instrType[i], &time[i])






                  share|improve this answer













                  The main problem is scanf("%c", &char) because scanf() after had read the input print a n to pass at the next line, this cause that the next scanf() instead of reading your input, go to read n, causing the failure in the reading of the input.
                  To avoid this problem put a space before %c ==> scanf(" %c", &char)



                  #include <stdio.h>

                  int main()
                  {
                  char continued;
                  int i = 0;
                  char instrType[10];
                  int time[10];

                  do
                  {
                  printf("L-lock a resourcen");
                  printf("U-unlock a resourcen");
                  printf("C-computen");
                  printf("Please Enter The Instruction Type and Time Input: ");
                  scanf(" %c%d", &instrType[i], &time[i]);
                  printf("Continue? (Y/N) ");
                  scanf(" %c", &continued);
                  i++;
                  } while (continued == 'Y' || continued == 'y');

                  return 0;
                  }


                  Other things:



                  Instead of i = i + 1 you can use i++



                  Instead of using a while() is better using a do{...}while() for saving some line of code.



                  You can concatenate more inputs in a single line ==> scanf(" %c%d", &instrType[i], &time[i])







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 4:55









                  coccodiococcodio

                  262




                  262






























                      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%2f53423106%2fc-language-the-array-did-not-return-a-correct-value%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

                      Create new schema in PostgreSQL using DBeaver

                      Deepest pit of an array with Javascript: test on Codility

                      Costa Masnaga