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 Screenshot
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
add a comment |
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 Screenshot
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
Possible duplicate of How to do scanf for single char in C
– Swordfish
Nov 22 at 2:49
add a comment |
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 Screenshot
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
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 Screenshot
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
c arrays
edited Nov 22 at 2:46
Swordfish
1
1
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
add a comment |
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
add a comment |
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"
add a comment |
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])
add a comment |
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"
add a comment |
up vote
0
down vote
You are comparing a string with a character that is instead of using scanf("%s",&continued) try using "%c"
add a comment |
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"
You are comparing a string with a character that is instead of using scanf("%s",&continued) try using "%c"
answered Nov 22 at 2:41
Renjith Raju
416
416
add a comment |
add a comment |
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])
add a comment |
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])
add a comment |
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])
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])
answered Nov 22 at 4:55
coccodio
262
262
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Possible duplicate of How to do scanf for single char in C
– Swordfish
Nov 22 at 2:49