Nested if statement in C
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
My code need to execute only one case path from 3x2 table which is; for Male 140 to 160 cm: short, 161 to 180 cm: medium; 181 to 199 cm: tall and for Female 120 to 140 cm: short, 141 to 165 cm: medium and 166 to 180 cm: tall.
For example, if i enter 153 cm woman, output is need to be only medium. But now my code gives output if i enter 153 cm woman, medium & tall together at the end.
How can i edit this code so far to execute for only one case for two combinations, age and length, and one of the three option for length; for example if i enter 153 cm woman, it will need say only medium.
#include <stdio.h>
int main()
{
int length;
char gender;
printf("enter gender: ");
scanf("%c", &gender);
printf("enter length: ");
scanf("%d", &length);
if (gender == 'M' || 'm') {
if (length <= 140 && length <= 160 ) {
printf ("short");
}
if (length <= 161 && length <= 180 ) {
printf ("medium");
}
if (length <= 181 && length <= 199 ) {
printf ("tall");
}
if (gender == 'W' || gender == 'w') {
if (length <= 120 || length <=140) {
printf("short");
if (length <=141 || length <=165) {
printf ("medium");
if (length <=166 || length <=180) {
printf ("tall");}
else {
printf("error");
}
}
}
}
}
return 0;
}
c if-statement
|
show 6 more comments
My code need to execute only one case path from 3x2 table which is; for Male 140 to 160 cm: short, 161 to 180 cm: medium; 181 to 199 cm: tall and for Female 120 to 140 cm: short, 141 to 165 cm: medium and 166 to 180 cm: tall.
For example, if i enter 153 cm woman, output is need to be only medium. But now my code gives output if i enter 153 cm woman, medium & tall together at the end.
How can i edit this code so far to execute for only one case for two combinations, age and length, and one of the three option for length; for example if i enter 153 cm woman, it will need say only medium.
#include <stdio.h>
int main()
{
int length;
char gender;
printf("enter gender: ");
scanf("%c", &gender);
printf("enter length: ");
scanf("%d", &length);
if (gender == 'M' || 'm') {
if (length <= 140 && length <= 160 ) {
printf ("short");
}
if (length <= 161 && length <= 180 ) {
printf ("medium");
}
if (length <= 181 && length <= 199 ) {
printf ("tall");
}
if (gender == 'W' || gender == 'w') {
if (length <= 120 || length <=140) {
printf("short");
if (length <=141 || length <=165) {
printf ("medium");
if (length <=166 || length <=180) {
printf ("tall");}
else {
printf("error");
}
}
}
}
}
return 0;
}
c if-statement
3
if (gender == 'M' || 'm')
is always true. Did you meanif (gender == 'M' || gender == 'm')
? Andif (gender == 'W' || gender == 'w')
will never be true as it is inside theif
that checks if the gender is Male. Also, all yourlength
conditions are wrong.length <= 140 && length <= 160
->length >= 140 && length <= 160
etc
– Spikatrix
Nov 17 '18 at 7:25
ah yes, typo error in that line, this should be (gender == 'M' || gender == 'm') for capital/lower case. thanks. trying this way now.
– kadir
Nov 17 '18 at 8:19
still doesnt work, how should i seperate man and woman blocks, i am confusing curly bracket using. (even inside of blocks)
– kadir
Nov 17 '18 at 8:48
if(gender == 'M' || gender == 'm') { /* length checks */ } else if(gender == 'W' || gender == 'w') { /* length checks */ }
– Spikatrix
Nov 17 '18 at 8:49
man block ok but woman block has still problem output is: enter gender: w enter length: 142 shortmediumtall
– kadir
Nov 17 '18 at 8:56
|
show 6 more comments
My code need to execute only one case path from 3x2 table which is; for Male 140 to 160 cm: short, 161 to 180 cm: medium; 181 to 199 cm: tall and for Female 120 to 140 cm: short, 141 to 165 cm: medium and 166 to 180 cm: tall.
For example, if i enter 153 cm woman, output is need to be only medium. But now my code gives output if i enter 153 cm woman, medium & tall together at the end.
How can i edit this code so far to execute for only one case for two combinations, age and length, and one of the three option for length; for example if i enter 153 cm woman, it will need say only medium.
#include <stdio.h>
int main()
{
int length;
char gender;
printf("enter gender: ");
scanf("%c", &gender);
printf("enter length: ");
scanf("%d", &length);
if (gender == 'M' || 'm') {
if (length <= 140 && length <= 160 ) {
printf ("short");
}
if (length <= 161 && length <= 180 ) {
printf ("medium");
}
if (length <= 181 && length <= 199 ) {
printf ("tall");
}
if (gender == 'W' || gender == 'w') {
if (length <= 120 || length <=140) {
printf("short");
if (length <=141 || length <=165) {
printf ("medium");
if (length <=166 || length <=180) {
printf ("tall");}
else {
printf("error");
}
}
}
}
}
return 0;
}
c if-statement
My code need to execute only one case path from 3x2 table which is; for Male 140 to 160 cm: short, 161 to 180 cm: medium; 181 to 199 cm: tall and for Female 120 to 140 cm: short, 141 to 165 cm: medium and 166 to 180 cm: tall.
For example, if i enter 153 cm woman, output is need to be only medium. But now my code gives output if i enter 153 cm woman, medium & tall together at the end.
How can i edit this code so far to execute for only one case for two combinations, age and length, and one of the three option for length; for example if i enter 153 cm woman, it will need say only medium.
#include <stdio.h>
int main()
{
int length;
char gender;
printf("enter gender: ");
scanf("%c", &gender);
printf("enter length: ");
scanf("%d", &length);
if (gender == 'M' || 'm') {
if (length <= 140 && length <= 160 ) {
printf ("short");
}
if (length <= 161 && length <= 180 ) {
printf ("medium");
}
if (length <= 181 && length <= 199 ) {
printf ("tall");
}
if (gender == 'W' || gender == 'w') {
if (length <= 120 || length <=140) {
printf("short");
if (length <=141 || length <=165) {
printf ("medium");
if (length <=166 || length <=180) {
printf ("tall");}
else {
printf("error");
}
}
}
}
}
return 0;
}
c if-statement
c if-statement
asked Nov 17 '18 at 7:14
kadirkadir
96
96
3
if (gender == 'M' || 'm')
is always true. Did you meanif (gender == 'M' || gender == 'm')
? Andif (gender == 'W' || gender == 'w')
will never be true as it is inside theif
that checks if the gender is Male. Also, all yourlength
conditions are wrong.length <= 140 && length <= 160
->length >= 140 && length <= 160
etc
– Spikatrix
Nov 17 '18 at 7:25
ah yes, typo error in that line, this should be (gender == 'M' || gender == 'm') for capital/lower case. thanks. trying this way now.
– kadir
Nov 17 '18 at 8:19
still doesnt work, how should i seperate man and woman blocks, i am confusing curly bracket using. (even inside of blocks)
– kadir
Nov 17 '18 at 8:48
if(gender == 'M' || gender == 'm') { /* length checks */ } else if(gender == 'W' || gender == 'w') { /* length checks */ }
– Spikatrix
Nov 17 '18 at 8:49
man block ok but woman block has still problem output is: enter gender: w enter length: 142 shortmediumtall
– kadir
Nov 17 '18 at 8:56
|
show 6 more comments
3
if (gender == 'M' || 'm')
is always true. Did you meanif (gender == 'M' || gender == 'm')
? Andif (gender == 'W' || gender == 'w')
will never be true as it is inside theif
that checks if the gender is Male. Also, all yourlength
conditions are wrong.length <= 140 && length <= 160
->length >= 140 && length <= 160
etc
– Spikatrix
Nov 17 '18 at 7:25
ah yes, typo error in that line, this should be (gender == 'M' || gender == 'm') for capital/lower case. thanks. trying this way now.
– kadir
Nov 17 '18 at 8:19
still doesnt work, how should i seperate man and woman blocks, i am confusing curly bracket using. (even inside of blocks)
– kadir
Nov 17 '18 at 8:48
if(gender == 'M' || gender == 'm') { /* length checks */ } else if(gender == 'W' || gender == 'w') { /* length checks */ }
– Spikatrix
Nov 17 '18 at 8:49
man block ok but woman block has still problem output is: enter gender: w enter length: 142 shortmediumtall
– kadir
Nov 17 '18 at 8:56
3
3
if (gender == 'M' || 'm')
is always true. Did you mean if (gender == 'M' || gender == 'm')
? And if (gender == 'W' || gender == 'w')
will never be true as it is inside the if
that checks if the gender is Male. Also, all your length
conditions are wrong. length <= 140 && length <= 160
-> length >= 140 && length <= 160
etc– Spikatrix
Nov 17 '18 at 7:25
if (gender == 'M' || 'm')
is always true. Did you mean if (gender == 'M' || gender == 'm')
? And if (gender == 'W' || gender == 'w')
will never be true as it is inside the if
that checks if the gender is Male. Also, all your length
conditions are wrong. length <= 140 && length <= 160
-> length >= 140 && length <= 160
etc– Spikatrix
Nov 17 '18 at 7:25
ah yes, typo error in that line, this should be (gender == 'M' || gender == 'm') for capital/lower case. thanks. trying this way now.
– kadir
Nov 17 '18 at 8:19
ah yes, typo error in that line, this should be (gender == 'M' || gender == 'm') for capital/lower case. thanks. trying this way now.
– kadir
Nov 17 '18 at 8:19
still doesnt work, how should i seperate man and woman blocks, i am confusing curly bracket using. (even inside of blocks)
– kadir
Nov 17 '18 at 8:48
still doesnt work, how should i seperate man and woman blocks, i am confusing curly bracket using. (even inside of blocks)
– kadir
Nov 17 '18 at 8:48
if(gender == 'M' || gender == 'm') { /* length checks */ } else if(gender == 'W' || gender == 'w') { /* length checks */ }
– Spikatrix
Nov 17 '18 at 8:49
if(gender == 'M' || gender == 'm') { /* length checks */ } else if(gender == 'W' || gender == 'w') { /* length checks */ }
– Spikatrix
Nov 17 '18 at 8:49
man block ok but woman block has still problem output is: enter gender: w enter length: 142 shortmediumtall
– kadir
Nov 17 '18 at 8:56
man block ok but woman block has still problem output is: enter gender: w enter length: 142 shortmediumtall
– kadir
Nov 17 '18 at 8:56
|
show 6 more comments
3 Answers
3
active
oldest
votes
For below lines, it has to be length >= 161.
Pls check similar lines for this.
if (length <= 161 && length <= 180 ) {
printf ("medium");
}
thanx ill check.
– kadir
Nov 17 '18 at 10:43
add a comment |
kadir you have done a number of mistakes , no problem . Practice makes a man perfect ...!
Compare your code and your code that i changed .. and realize the mistakes . Explanation is given as comments in code .
#include<stdio.h>
int main()
{
int length;
char gender;
printf("enter gender: ");
scanf("%c", &gender);
printf("enter length: ");
scanf("%d", &length);
if (gender == 'M' ||gender == 'm') { //comparison should be done in both sides of or
if (length >= 140 && length <= 160 ) {
printf ("short");
}
else if (length >= 161 && length <= 180 ) {
printf ("medium");
}
else if (length >= 181 && length <= 199 ) {
printf ("tall");
}
}
else if (gender == 'W' || gender == 'w') {
if (length >= 120 && length <=140) {
printf("short");
}
else if (length >=141 && length <=165) { // & should be used.
printf ("medium");
}
else if (length >=166 && length <=180) {
printf ("tall");}
else { /* if you are using else simply , it corresponds to the last if not all the if's in front so try using if else .*/
printf("error");
}
}
return 0;
}
Good Luck
add a comment |
You need to compare length in an exclusive range. Here is the code:
#include <stdio.h>
int main()
{
int length;
char gender;
printf("enter gender: ");
scanf("%c", &gender);
printf("enter length: ");
scanf("%d", &length);
if (gender == 'M' || gender == 'm'){
if (length <= 160 )
printf ("shortn");
if (161 <= length && length <= 180)
printf ("mediumn");
if (181 <= length && length <= 199)
printf ("talln");
}
if (gender == 'W' || gender == 'w'){
if (length <= 140)
printf ("shortn");
if (141 <= length && length <= 165)
printf ("mediumn");
if (166 <= length && length <= 180)
printf ("talln");
if(length>=180)
printf("error n");
}
else if(gender != 'm'|| gender!='M'|| gender!='w'||gender!='W')
printf("error");
return 0;
}
Also consider trying out either topper(gender) or tolower(gender) functions from the ctype library :)
– reedx8
Nov 17 '18 at 11:20
thanks, how can i add an error message for wrong char entry, which is not m/M or w/W? i mean user can only enter m/M or w/W. otherwise program will say error.
– kadir
Nov 17 '18 at 11:32
Consider usingelse if
s @reedx8
– Spikatrix
Nov 17 '18 at 11:34
@kadir add an else condition with the error message you would like. Perhaps have user repeat using a do-while loop as well around all of the conditions until user enters a w or m. So, else{printf(“error message here”)}
– reedx8
Nov 17 '18 at 12:17
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%2f53349077%2fnested-if-statement-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
For below lines, it has to be length >= 161.
Pls check similar lines for this.
if (length <= 161 && length <= 180 ) {
printf ("medium");
}
thanx ill check.
– kadir
Nov 17 '18 at 10:43
add a comment |
For below lines, it has to be length >= 161.
Pls check similar lines for this.
if (length <= 161 && length <= 180 ) {
printf ("medium");
}
thanx ill check.
– kadir
Nov 17 '18 at 10:43
add a comment |
For below lines, it has to be length >= 161.
Pls check similar lines for this.
if (length <= 161 && length <= 180 ) {
printf ("medium");
}
For below lines, it has to be length >= 161.
Pls check similar lines for this.
if (length <= 161 && length <= 180 ) {
printf ("medium");
}
answered Nov 17 '18 at 10:23
anandanand
1317
1317
thanx ill check.
– kadir
Nov 17 '18 at 10:43
add a comment |
thanx ill check.
– kadir
Nov 17 '18 at 10:43
thanx ill check.
– kadir
Nov 17 '18 at 10:43
thanx ill check.
– kadir
Nov 17 '18 at 10:43
add a comment |
kadir you have done a number of mistakes , no problem . Practice makes a man perfect ...!
Compare your code and your code that i changed .. and realize the mistakes . Explanation is given as comments in code .
#include<stdio.h>
int main()
{
int length;
char gender;
printf("enter gender: ");
scanf("%c", &gender);
printf("enter length: ");
scanf("%d", &length);
if (gender == 'M' ||gender == 'm') { //comparison should be done in both sides of or
if (length >= 140 && length <= 160 ) {
printf ("short");
}
else if (length >= 161 && length <= 180 ) {
printf ("medium");
}
else if (length >= 181 && length <= 199 ) {
printf ("tall");
}
}
else if (gender == 'W' || gender == 'w') {
if (length >= 120 && length <=140) {
printf("short");
}
else if (length >=141 && length <=165) { // & should be used.
printf ("medium");
}
else if (length >=166 && length <=180) {
printf ("tall");}
else { /* if you are using else simply , it corresponds to the last if not all the if's in front so try using if else .*/
printf("error");
}
}
return 0;
}
Good Luck
add a comment |
kadir you have done a number of mistakes , no problem . Practice makes a man perfect ...!
Compare your code and your code that i changed .. and realize the mistakes . Explanation is given as comments in code .
#include<stdio.h>
int main()
{
int length;
char gender;
printf("enter gender: ");
scanf("%c", &gender);
printf("enter length: ");
scanf("%d", &length);
if (gender == 'M' ||gender == 'm') { //comparison should be done in both sides of or
if (length >= 140 && length <= 160 ) {
printf ("short");
}
else if (length >= 161 && length <= 180 ) {
printf ("medium");
}
else if (length >= 181 && length <= 199 ) {
printf ("tall");
}
}
else if (gender == 'W' || gender == 'w') {
if (length >= 120 && length <=140) {
printf("short");
}
else if (length >=141 && length <=165) { // & should be used.
printf ("medium");
}
else if (length >=166 && length <=180) {
printf ("tall");}
else { /* if you are using else simply , it corresponds to the last if not all the if's in front so try using if else .*/
printf("error");
}
}
return 0;
}
Good Luck
add a comment |
kadir you have done a number of mistakes , no problem . Practice makes a man perfect ...!
Compare your code and your code that i changed .. and realize the mistakes . Explanation is given as comments in code .
#include<stdio.h>
int main()
{
int length;
char gender;
printf("enter gender: ");
scanf("%c", &gender);
printf("enter length: ");
scanf("%d", &length);
if (gender == 'M' ||gender == 'm') { //comparison should be done in both sides of or
if (length >= 140 && length <= 160 ) {
printf ("short");
}
else if (length >= 161 && length <= 180 ) {
printf ("medium");
}
else if (length >= 181 && length <= 199 ) {
printf ("tall");
}
}
else if (gender == 'W' || gender == 'w') {
if (length >= 120 && length <=140) {
printf("short");
}
else if (length >=141 && length <=165) { // & should be used.
printf ("medium");
}
else if (length >=166 && length <=180) {
printf ("tall");}
else { /* if you are using else simply , it corresponds to the last if not all the if's in front so try using if else .*/
printf("error");
}
}
return 0;
}
Good Luck
kadir you have done a number of mistakes , no problem . Practice makes a man perfect ...!
Compare your code and your code that i changed .. and realize the mistakes . Explanation is given as comments in code .
#include<stdio.h>
int main()
{
int length;
char gender;
printf("enter gender: ");
scanf("%c", &gender);
printf("enter length: ");
scanf("%d", &length);
if (gender == 'M' ||gender == 'm') { //comparison should be done in both sides of or
if (length >= 140 && length <= 160 ) {
printf ("short");
}
else if (length >= 161 && length <= 180 ) {
printf ("medium");
}
else if (length >= 181 && length <= 199 ) {
printf ("tall");
}
}
else if (gender == 'W' || gender == 'w') {
if (length >= 120 && length <=140) {
printf("short");
}
else if (length >=141 && length <=165) { // & should be used.
printf ("medium");
}
else if (length >=166 && length <=180) {
printf ("tall");}
else { /* if you are using else simply , it corresponds to the last if not all the if's in front so try using if else .*/
printf("error");
}
}
return 0;
}
Good Luck
answered Nov 17 '18 at 19:54
Siva PratheepSiva Pratheep
965
965
add a comment |
add a comment |
You need to compare length in an exclusive range. Here is the code:
#include <stdio.h>
int main()
{
int length;
char gender;
printf("enter gender: ");
scanf("%c", &gender);
printf("enter length: ");
scanf("%d", &length);
if (gender == 'M' || gender == 'm'){
if (length <= 160 )
printf ("shortn");
if (161 <= length && length <= 180)
printf ("mediumn");
if (181 <= length && length <= 199)
printf ("talln");
}
if (gender == 'W' || gender == 'w'){
if (length <= 140)
printf ("shortn");
if (141 <= length && length <= 165)
printf ("mediumn");
if (166 <= length && length <= 180)
printf ("talln");
if(length>=180)
printf("error n");
}
else if(gender != 'm'|| gender!='M'|| gender!='w'||gender!='W')
printf("error");
return 0;
}
Also consider trying out either topper(gender) or tolower(gender) functions from the ctype library :)
– reedx8
Nov 17 '18 at 11:20
thanks, how can i add an error message for wrong char entry, which is not m/M or w/W? i mean user can only enter m/M or w/W. otherwise program will say error.
– kadir
Nov 17 '18 at 11:32
Consider usingelse if
s @reedx8
– Spikatrix
Nov 17 '18 at 11:34
@kadir add an else condition with the error message you would like. Perhaps have user repeat using a do-while loop as well around all of the conditions until user enters a w or m. So, else{printf(“error message here”)}
– reedx8
Nov 17 '18 at 12:17
add a comment |
You need to compare length in an exclusive range. Here is the code:
#include <stdio.h>
int main()
{
int length;
char gender;
printf("enter gender: ");
scanf("%c", &gender);
printf("enter length: ");
scanf("%d", &length);
if (gender == 'M' || gender == 'm'){
if (length <= 160 )
printf ("shortn");
if (161 <= length && length <= 180)
printf ("mediumn");
if (181 <= length && length <= 199)
printf ("talln");
}
if (gender == 'W' || gender == 'w'){
if (length <= 140)
printf ("shortn");
if (141 <= length && length <= 165)
printf ("mediumn");
if (166 <= length && length <= 180)
printf ("talln");
if(length>=180)
printf("error n");
}
else if(gender != 'm'|| gender!='M'|| gender!='w'||gender!='W')
printf("error");
return 0;
}
Also consider trying out either topper(gender) or tolower(gender) functions from the ctype library :)
– reedx8
Nov 17 '18 at 11:20
thanks, how can i add an error message for wrong char entry, which is not m/M or w/W? i mean user can only enter m/M or w/W. otherwise program will say error.
– kadir
Nov 17 '18 at 11:32
Consider usingelse if
s @reedx8
– Spikatrix
Nov 17 '18 at 11:34
@kadir add an else condition with the error message you would like. Perhaps have user repeat using a do-while loop as well around all of the conditions until user enters a w or m. So, else{printf(“error message here”)}
– reedx8
Nov 17 '18 at 12:17
add a comment |
You need to compare length in an exclusive range. Here is the code:
#include <stdio.h>
int main()
{
int length;
char gender;
printf("enter gender: ");
scanf("%c", &gender);
printf("enter length: ");
scanf("%d", &length);
if (gender == 'M' || gender == 'm'){
if (length <= 160 )
printf ("shortn");
if (161 <= length && length <= 180)
printf ("mediumn");
if (181 <= length && length <= 199)
printf ("talln");
}
if (gender == 'W' || gender == 'w'){
if (length <= 140)
printf ("shortn");
if (141 <= length && length <= 165)
printf ("mediumn");
if (166 <= length && length <= 180)
printf ("talln");
if(length>=180)
printf("error n");
}
else if(gender != 'm'|| gender!='M'|| gender!='w'||gender!='W')
printf("error");
return 0;
}
You need to compare length in an exclusive range. Here is the code:
#include <stdio.h>
int main()
{
int length;
char gender;
printf("enter gender: ");
scanf("%c", &gender);
printf("enter length: ");
scanf("%d", &length);
if (gender == 'M' || gender == 'm'){
if (length <= 160 )
printf ("shortn");
if (161 <= length && length <= 180)
printf ("mediumn");
if (181 <= length && length <= 199)
printf ("talln");
}
if (gender == 'W' || gender == 'w'){
if (length <= 140)
printf ("shortn");
if (141 <= length && length <= 165)
printf ("mediumn");
if (166 <= length && length <= 180)
printf ("talln");
if(length>=180)
printf("error n");
}
else if(gender != 'm'|| gender!='M'|| gender!='w'||gender!='W')
printf("error");
return 0;
}
edited Nov 17 '18 at 13:46
Matthieu Brucher
17.8k52445
17.8k52445
answered Nov 17 '18 at 11:13
reedx8reedx8
186
186
Also consider trying out either topper(gender) or tolower(gender) functions from the ctype library :)
– reedx8
Nov 17 '18 at 11:20
thanks, how can i add an error message for wrong char entry, which is not m/M or w/W? i mean user can only enter m/M or w/W. otherwise program will say error.
– kadir
Nov 17 '18 at 11:32
Consider usingelse if
s @reedx8
– Spikatrix
Nov 17 '18 at 11:34
@kadir add an else condition with the error message you would like. Perhaps have user repeat using a do-while loop as well around all of the conditions until user enters a w or m. So, else{printf(“error message here”)}
– reedx8
Nov 17 '18 at 12:17
add a comment |
Also consider trying out either topper(gender) or tolower(gender) functions from the ctype library :)
– reedx8
Nov 17 '18 at 11:20
thanks, how can i add an error message for wrong char entry, which is not m/M or w/W? i mean user can only enter m/M or w/W. otherwise program will say error.
– kadir
Nov 17 '18 at 11:32
Consider usingelse if
s @reedx8
– Spikatrix
Nov 17 '18 at 11:34
@kadir add an else condition with the error message you would like. Perhaps have user repeat using a do-while loop as well around all of the conditions until user enters a w or m. So, else{printf(“error message here”)}
– reedx8
Nov 17 '18 at 12:17
Also consider trying out either topper(gender) or tolower(gender) functions from the ctype library :)
– reedx8
Nov 17 '18 at 11:20
Also consider trying out either topper(gender) or tolower(gender) functions from the ctype library :)
– reedx8
Nov 17 '18 at 11:20
thanks, how can i add an error message for wrong char entry, which is not m/M or w/W? i mean user can only enter m/M or w/W. otherwise program will say error.
– kadir
Nov 17 '18 at 11:32
thanks, how can i add an error message for wrong char entry, which is not m/M or w/W? i mean user can only enter m/M or w/W. otherwise program will say error.
– kadir
Nov 17 '18 at 11:32
Consider using
else if
s @reedx8– Spikatrix
Nov 17 '18 at 11:34
Consider using
else if
s @reedx8– Spikatrix
Nov 17 '18 at 11:34
@kadir add an else condition with the error message you would like. Perhaps have user repeat using a do-while loop as well around all of the conditions until user enters a w or m. So, else{printf(“error message here”)}
– reedx8
Nov 17 '18 at 12:17
@kadir add an else condition with the error message you would like. Perhaps have user repeat using a do-while loop as well around all of the conditions until user enters a w or m. So, else{printf(“error message here”)}
– reedx8
Nov 17 '18 at 12:17
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%2f53349077%2fnested-if-statement-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
3
if (gender == 'M' || 'm')
is always true. Did you meanif (gender == 'M' || gender == 'm')
? Andif (gender == 'W' || gender == 'w')
will never be true as it is inside theif
that checks if the gender is Male. Also, all yourlength
conditions are wrong.length <= 140 && length <= 160
->length >= 140 && length <= 160
etc– Spikatrix
Nov 17 '18 at 7:25
ah yes, typo error in that line, this should be (gender == 'M' || gender == 'm') for capital/lower case. thanks. trying this way now.
– kadir
Nov 17 '18 at 8:19
still doesnt work, how should i seperate man and woman blocks, i am confusing curly bracket using. (even inside of blocks)
– kadir
Nov 17 '18 at 8:48
if(gender == 'M' || gender == 'm') { /* length checks */ } else if(gender == 'W' || gender == 'w') { /* length checks */ }
– Spikatrix
Nov 17 '18 at 8:49
man block ok but woman block has still problem output is: enter gender: w enter length: 142 shortmediumtall
– kadir
Nov 17 '18 at 8:56