Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want the program to end when I type 'END'.
But in my program "Selected for Redteam" is shown after END.

Also what to do if a person's first name is 'End',
I don't want the code to stop when I type 'End'. How to make the compiler understand that 'End' is different from 'END'.

What I have tried:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int no=0;
    int blueteam=0;
    int redteam=0;
    char name[15];
    char END;
    
    printf("Type END to exit\n");
    printf("Note:Make sure the first alphabet is in Uppercase.\n");
    
    do
    { 
        printf("\nStudent Number:%d\t Redteam:%d\t Blueteam:%d",no,redteam,blueteam);
        printf("\nEnter the First name:");
        scanf(" %s", name);
        
        if(name[0] > 'M')
        {
            printf("Selected for Blueteam\n");
            blueteam++;
        }
        else
        {
// So if name[0] is 'E' for END, you will come here
            printf("Selected for Redteam\n");
            redteam++;
        }    
        no++;
    } while(name[2]=="END"); // this won't work
    return 0;
}
Posted
Updated 9-Dec-17 1:36am
v8
Comments
Richard MacCutchan 9-Dec-17 7:17am    
Your code is still wrong: }while(name[2]=="END");
You cannot compare a single character (}name[2]); with a string ("END"), i.e. three characters.
shreyas s 9-Dec-17 7:22am    
Oh crap, you are right! I'm so dumb.
Richard MacCutchan 9-Dec-17 7:36am    
We all make mistakes.
I have tidied up your indentation and added a couple of comments for you to look at.
shreyas s 9-Dec-17 8:26am    
Thanks a lot, sir. You are amazing!!!!

Richard MacCutchan 9-Dec-17 8:50am    
No, I have just been doing this for a bit longer than you.

1 solution

C++
}while(name[2]==END);

That is not a valid statement. Also, if name[0] is not greater than 'M' then it will print the line about red team. Try indenting your code properly and the illogic will become clear.
 
Share this answer
 
Comments
shreyas s 9-Dec-17 6:58am    
Tried to do some editing but no success.
Richard MacCutchan 9-Dec-17 7:10am    
Think about what you need to do, and write down each step on paper. Read through it and check if the logic makes sense. Then when you are happy with that try turning it into actual code.

As to your question about "END" and "End" or "end". Computers see upper and lower case characters as different elements, so you do not need to worry about getting it wrong in your compares. However, if you want to accept any combination of upper and lower case strings containing variants of 'E', 'N', and 'D', there are function to convert them all to upper or lower case.
shreyas s 9-Dec-17 7:20am    
Okay, will do.
Thanks for the quick reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900