if statements did not work correctly in C
I am a new beginner in C
This is my code:
#include <stdio.h>
int main(void) {
int choice;
int clientNum;
printf("nAssume that in the main memory contain 16 frameSizen");
printf("Each frame has 256 bitsn");
printf("How many clients: ");
scanf("%d", &clientNum);
printf("nPlease choose the Scheduling Algorithm 1. FCFS 2.Round Robin: ");
scanf("%d", &choice);
while(choice !=1 || choice !=2){
printf("nINVALID!!! The Server only has either FCFS or Round Robind Algorithm");
printf("nPlease choose the Scheduling Algorithm again 1. FCFS 2.Round Robin: ");
scanf("%d", &choice);
}
if (choice==1){
printf("FCFS");
}
if (choice==2){
printf("Round Robind");
}
return 0;
}
I want to compare the value of choice with number 1 and 2. However, If statements did not work correctly. it did not compare choice with any value
Is there any error in syntax or logic?
The output:
gcc version 4.6.3
Assume that in the main memory contain 16 frameSize
Each frame has 256 bits
How many clients: 3
Please choose the Scheduling Algorithm 1. FCFS 2.Round Robin: 1
INVALID!!! The Server only has either FCFS or Round Robind Algorithm
Please choose the Scheduling Algorithm again 1. FCFS 2.Round Robin: 2
INVALID!!! The Server only has either FCFS or Round Robind Algorithm
Please choose the Scheduling Algorithm again 1. FCFS 2.Round Robin: 1
INVALID!!! The Server only has either FCFS or Round Robind Algorithm
Please choose the Scheduling Algorithm again 1. FCFS 2.Round Robin:
c
add a comment |
I am a new beginner in C
This is my code:
#include <stdio.h>
int main(void) {
int choice;
int clientNum;
printf("nAssume that in the main memory contain 16 frameSizen");
printf("Each frame has 256 bitsn");
printf("How many clients: ");
scanf("%d", &clientNum);
printf("nPlease choose the Scheduling Algorithm 1. FCFS 2.Round Robin: ");
scanf("%d", &choice);
while(choice !=1 || choice !=2){
printf("nINVALID!!! The Server only has either FCFS or Round Robind Algorithm");
printf("nPlease choose the Scheduling Algorithm again 1. FCFS 2.Round Robin: ");
scanf("%d", &choice);
}
if (choice==1){
printf("FCFS");
}
if (choice==2){
printf("Round Robind");
}
return 0;
}
I want to compare the value of choice with number 1 and 2. However, If statements did not work correctly. it did not compare choice with any value
Is there any error in syntax or logic?
The output:
gcc version 4.6.3
Assume that in the main memory contain 16 frameSize
Each frame has 256 bits
How many clients: 3
Please choose the Scheduling Algorithm 1. FCFS 2.Round Robin: 1
INVALID!!! The Server only has either FCFS or Round Robind Algorithm
Please choose the Scheduling Algorithm again 1. FCFS 2.Round Robin: 2
INVALID!!! The Server only has either FCFS or Round Robind Algorithm
Please choose the Scheduling Algorithm again 1. FCFS 2.Round Robin: 1
INVALID!!! The Server only has either FCFS or Round Robind Algorithm
Please choose the Scheduling Algorithm again 1. FCFS 2.Round Robin:
c
7
while(choice !=1 || choice !=2)
->while(choice !=1 && choice !=2)
-- it happens... Generally if you are thinking "(insert language feature) is not working in C"... it's generally not the language feature that isn't working:)
– David C. Rankin
Nov 5 '18 at 6:10
You wanted to compare value with 1 AND 2, but you did compare it with 1 OR 2. That's the problem, not IF.
– RyanB
Nov 5 '18 at 6:12
I got it! Thanks All :D
– Lee0ne
Nov 5 '18 at 6:14
add a comment |
I am a new beginner in C
This is my code:
#include <stdio.h>
int main(void) {
int choice;
int clientNum;
printf("nAssume that in the main memory contain 16 frameSizen");
printf("Each frame has 256 bitsn");
printf("How many clients: ");
scanf("%d", &clientNum);
printf("nPlease choose the Scheduling Algorithm 1. FCFS 2.Round Robin: ");
scanf("%d", &choice);
while(choice !=1 || choice !=2){
printf("nINVALID!!! The Server only has either FCFS or Round Robind Algorithm");
printf("nPlease choose the Scheduling Algorithm again 1. FCFS 2.Round Robin: ");
scanf("%d", &choice);
}
if (choice==1){
printf("FCFS");
}
if (choice==2){
printf("Round Robind");
}
return 0;
}
I want to compare the value of choice with number 1 and 2. However, If statements did not work correctly. it did not compare choice with any value
Is there any error in syntax or logic?
The output:
gcc version 4.6.3
Assume that in the main memory contain 16 frameSize
Each frame has 256 bits
How many clients: 3
Please choose the Scheduling Algorithm 1. FCFS 2.Round Robin: 1
INVALID!!! The Server only has either FCFS or Round Robind Algorithm
Please choose the Scheduling Algorithm again 1. FCFS 2.Round Robin: 2
INVALID!!! The Server only has either FCFS or Round Robind Algorithm
Please choose the Scheduling Algorithm again 1. FCFS 2.Round Robin: 1
INVALID!!! The Server only has either FCFS or Round Robind Algorithm
Please choose the Scheduling Algorithm again 1. FCFS 2.Round Robin:
c
I am a new beginner in C
This is my code:
#include <stdio.h>
int main(void) {
int choice;
int clientNum;
printf("nAssume that in the main memory contain 16 frameSizen");
printf("Each frame has 256 bitsn");
printf("How many clients: ");
scanf("%d", &clientNum);
printf("nPlease choose the Scheduling Algorithm 1. FCFS 2.Round Robin: ");
scanf("%d", &choice);
while(choice !=1 || choice !=2){
printf("nINVALID!!! The Server only has either FCFS or Round Robind Algorithm");
printf("nPlease choose the Scheduling Algorithm again 1. FCFS 2.Round Robin: ");
scanf("%d", &choice);
}
if (choice==1){
printf("FCFS");
}
if (choice==2){
printf("Round Robind");
}
return 0;
}
I want to compare the value of choice with number 1 and 2. However, If statements did not work correctly. it did not compare choice with any value
Is there any error in syntax or logic?
The output:
gcc version 4.6.3
Assume that in the main memory contain 16 frameSize
Each frame has 256 bits
How many clients: 3
Please choose the Scheduling Algorithm 1. FCFS 2.Round Robin: 1
INVALID!!! The Server only has either FCFS or Round Robind Algorithm
Please choose the Scheduling Algorithm again 1. FCFS 2.Round Robin: 2
INVALID!!! The Server only has either FCFS or Round Robind Algorithm
Please choose the Scheduling Algorithm again 1. FCFS 2.Round Robin: 1
INVALID!!! The Server only has either FCFS or Round Robind Algorithm
Please choose the Scheduling Algorithm again 1. FCFS 2.Round Robin:
c
c
edited Nov 12 '18 at 15:37
Mike
2,0581725
2,0581725
asked Nov 5 '18 at 6:07
Lee0neLee0ne
114
114
7
while(choice !=1 || choice !=2)
->while(choice !=1 && choice !=2)
-- it happens... Generally if you are thinking "(insert language feature) is not working in C"... it's generally not the language feature that isn't working:)
– David C. Rankin
Nov 5 '18 at 6:10
You wanted to compare value with 1 AND 2, but you did compare it with 1 OR 2. That's the problem, not IF.
– RyanB
Nov 5 '18 at 6:12
I got it! Thanks All :D
– Lee0ne
Nov 5 '18 at 6:14
add a comment |
7
while(choice !=1 || choice !=2)
->while(choice !=1 && choice !=2)
-- it happens... Generally if you are thinking "(insert language feature) is not working in C"... it's generally not the language feature that isn't working:)
– David C. Rankin
Nov 5 '18 at 6:10
You wanted to compare value with 1 AND 2, but you did compare it with 1 OR 2. That's the problem, not IF.
– RyanB
Nov 5 '18 at 6:12
I got it! Thanks All :D
– Lee0ne
Nov 5 '18 at 6:14
7
7
while(choice !=1 || choice !=2)
-> while(choice !=1 && choice !=2)
-- it happens... Generally if you are thinking "(insert language feature) is not working in C"... it's generally not the language feature that isn't working :)
– David C. Rankin
Nov 5 '18 at 6:10
while(choice !=1 || choice !=2)
-> while(choice !=1 && choice !=2)
-- it happens... Generally if you are thinking "(insert language feature) is not working in C"... it's generally not the language feature that isn't working :)
– David C. Rankin
Nov 5 '18 at 6:10
You wanted to compare value with 1 AND 2, but you did compare it with 1 OR 2. That's the problem, not IF.
– RyanB
Nov 5 '18 at 6:12
You wanted to compare value with 1 AND 2, but you did compare it with 1 OR 2. That's the problem, not IF.
– RyanB
Nov 5 '18 at 6:12
I got it! Thanks All :D
– Lee0ne
Nov 5 '18 at 6:14
I got it! Thanks All :D
– Lee0ne
Nov 5 '18 at 6:14
add a comment |
2 Answers
2
active
oldest
votes
while ((choice !=1) && (choice !=2))
{
code.....
}
after the loop you will end up with 2 choices either 1 or 2 so :
if (choice == 1)
{
printf("FCFS");
}
else
{
printf("Round Robind");
add a comment |
This should work:
#include <stdio.h>
int main(void)
{
int choice;
int clientNum;
printf("nAssume that in the main memory contain 16 frameSizen");
printf("Each frame has 256 bitsn");
printf("How many clients: ");
scanf("%d", &clientNum);
printf("nPlease choose the Scheduling Algorithm 1. FCFS 2.Round Robin: ");
scanf("%d", &choice);
while (choice !=1 && choice !=2) //change || into &&
{
printf("nINVALID!!! The Server only has either FCFS or Round Robind Algorithm");
printf("nPlease choose the Scheduling Algorithm again 1. FCFS 2.Round Robin: ");
scanf("%d", &choice);
}
if (choice == 1)
{
printf("FCFS");
}
if (choice == 2)
{
printf("Round Robind");
}
return 0;
}
The OP's coding style does not influence their problem whatsoever.
– Tau
Nov 12 '18 at 16:20
add a comment |
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
});
}
});
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%2f53149153%2fif-statements-did-not-work-correctly-in-c%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
while ((choice !=1) && (choice !=2))
{
code.....
}
after the loop you will end up with 2 choices either 1 or 2 so :
if (choice == 1)
{
printf("FCFS");
}
else
{
printf("Round Robind");
add a comment |
while ((choice !=1) && (choice !=2))
{
code.....
}
after the loop you will end up with 2 choices either 1 or 2 so :
if (choice == 1)
{
printf("FCFS");
}
else
{
printf("Round Robind");
add a comment |
while ((choice !=1) && (choice !=2))
{
code.....
}
after the loop you will end up with 2 choices either 1 or 2 so :
if (choice == 1)
{
printf("FCFS");
}
else
{
printf("Round Robind");
while ((choice !=1) && (choice !=2))
{
code.....
}
after the loop you will end up with 2 choices either 1 or 2 so :
if (choice == 1)
{
printf("FCFS");
}
else
{
printf("Round Robind");
answered Jan 2 at 12:41
FethiFethi
87
87
add a comment |
add a comment |
This should work:
#include <stdio.h>
int main(void)
{
int choice;
int clientNum;
printf("nAssume that in the main memory contain 16 frameSizen");
printf("Each frame has 256 bitsn");
printf("How many clients: ");
scanf("%d", &clientNum);
printf("nPlease choose the Scheduling Algorithm 1. FCFS 2.Round Robin: ");
scanf("%d", &choice);
while (choice !=1 && choice !=2) //change || into &&
{
printf("nINVALID!!! The Server only has either FCFS or Round Robind Algorithm");
printf("nPlease choose the Scheduling Algorithm again 1. FCFS 2.Round Robin: ");
scanf("%d", &choice);
}
if (choice == 1)
{
printf("FCFS");
}
if (choice == 2)
{
printf("Round Robind");
}
return 0;
}
The OP's coding style does not influence their problem whatsoever.
– Tau
Nov 12 '18 at 16:20
add a comment |
This should work:
#include <stdio.h>
int main(void)
{
int choice;
int clientNum;
printf("nAssume that in the main memory contain 16 frameSizen");
printf("Each frame has 256 bitsn");
printf("How many clients: ");
scanf("%d", &clientNum);
printf("nPlease choose the Scheduling Algorithm 1. FCFS 2.Round Robin: ");
scanf("%d", &choice);
while (choice !=1 && choice !=2) //change || into &&
{
printf("nINVALID!!! The Server only has either FCFS or Round Robind Algorithm");
printf("nPlease choose the Scheduling Algorithm again 1. FCFS 2.Round Robin: ");
scanf("%d", &choice);
}
if (choice == 1)
{
printf("FCFS");
}
if (choice == 2)
{
printf("Round Robind");
}
return 0;
}
The OP's coding style does not influence their problem whatsoever.
– Tau
Nov 12 '18 at 16:20
add a comment |
This should work:
#include <stdio.h>
int main(void)
{
int choice;
int clientNum;
printf("nAssume that in the main memory contain 16 frameSizen");
printf("Each frame has 256 bitsn");
printf("How many clients: ");
scanf("%d", &clientNum);
printf("nPlease choose the Scheduling Algorithm 1. FCFS 2.Round Robin: ");
scanf("%d", &choice);
while (choice !=1 && choice !=2) //change || into &&
{
printf("nINVALID!!! The Server only has either FCFS or Round Robind Algorithm");
printf("nPlease choose the Scheduling Algorithm again 1. FCFS 2.Round Robin: ");
scanf("%d", &choice);
}
if (choice == 1)
{
printf("FCFS");
}
if (choice == 2)
{
printf("Round Robind");
}
return 0;
}
This should work:
#include <stdio.h>
int main(void)
{
int choice;
int clientNum;
printf("nAssume that in the main memory contain 16 frameSizen");
printf("Each frame has 256 bitsn");
printf("How many clients: ");
scanf("%d", &clientNum);
printf("nPlease choose the Scheduling Algorithm 1. FCFS 2.Round Robin: ");
scanf("%d", &choice);
while (choice !=1 && choice !=2) //change || into &&
{
printf("nINVALID!!! The Server only has either FCFS or Round Robind Algorithm");
printf("nPlease choose the Scheduling Algorithm again 1. FCFS 2.Round Robin: ");
scanf("%d", &choice);
}
if (choice == 1)
{
printf("FCFS");
}
if (choice == 2)
{
printf("Round Robind");
}
return 0;
}
edited Jan 2 at 11:55
answered Nov 5 '18 at 6:59
MikeMike
2,0581725
2,0581725
The OP's coding style does not influence their problem whatsoever.
– Tau
Nov 12 '18 at 16:20
add a comment |
The OP's coding style does not influence their problem whatsoever.
– Tau
Nov 12 '18 at 16:20
The OP's coding style does not influence their problem whatsoever.
– Tau
Nov 12 '18 at 16:20
The OP's coding style does not influence their problem whatsoever.
– Tau
Nov 12 '18 at 16:20
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.
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%2f53149153%2fif-statements-did-not-work-correctly-in-c%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
7
while(choice !=1 || choice !=2)
->while(choice !=1 && choice !=2)
-- it happens... Generally if you are thinking "(insert language feature) is not working in C"... it's generally not the language feature that isn't working:)
– David C. Rankin
Nov 5 '18 at 6:10
You wanted to compare value with 1 AND 2, but you did compare it with 1 OR 2. That's the problem, not IF.
– RyanB
Nov 5 '18 at 6:12
I got it! Thanks All :D
– Lee0ne
Nov 5 '18 at 6:14