C language: The Array did not return a correct value











up vote
0
down vote

favorite












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 at 2:49















up vote
0
down vote

favorite












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 at 2:49













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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 at 2:46









Swordfish

8,72011335




8,72011335










asked Nov 22 at 2:34









Lee0ne

153




153












  • Possible duplicate of How to do scanf for single char in C
    – Swordfish
    Nov 22 at 2:49


















  • Possible duplicate of How to do scanf for single char in C
    – Swordfish
    Nov 22 at 2:49
















Possible duplicate of How to do scanf for single char in C
– Swordfish
Nov 22 at 2:49




Possible duplicate of How to do scanf for single char in C
– Swordfish
Nov 22 at 2:49












2 Answers
2






active

oldest

votes

















up vote
0
down vote













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






share|improve this answer




























    up vote
    0
    down vote













    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',
      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








      up vote
      0
      down vote













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






      share|improve this answer

























        up vote
        0
        down vote













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






        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          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 at 2:41









          Renjith Raju

          416




          416
























              up vote
              0
              down vote













              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

























                up vote
                0
                down vote













                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























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  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 at 4:55









                  coccodio

                  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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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

                      What visual should I use to simply compare current year value vs last year in Power BI desktop

                      How to ignore python UserWarning in pytest?

                      Alexandru Averescu