charArray and string comparison

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Pshycoone
    New Member
    • Nov 2011
    • 3

    charArray and string comparison

    Hey everyone!
    Just started programming in C (I use visualstudio 2010 pro.) and have previously programmed in C#.

    I have this(ese) really, really basic question(s) about chars and strings;

    I want to have compare and set strings.
    I already know how to compare, by using the "strcmp", now however I have trouble how to figure out how to initiate string/chararray variables with a string I choose. (C# made me spoiled)

    I.E.
    I have a function that receives a string from the prompt (don't think about that, just explaining this so you can understand better);

    Code:
    choice = GetLine();
    
    //Here's the tricky part I can't figure out;
    if(choice = "exit"){
      do this;
    }
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    if you search on google with the key words string compare c, first link you will get is strcmp read about strcmp as well as other string related function

    Comment

    • Maraj
      New Member
      • Nov 2011
      • 24

      #3
      I think you are using assingment operator = instead of eqaulity operator ==
      Code:
      choice = GetLine(); 
        
      //Here's the tricky part I can't figure out; 
      if(choice == "exit"){ 
        do this;

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You should not be using strcmp in C++. Use the string data type instead.

        Comment

        • johny10151981
          Top Contributor
          • Jan 2010
          • 1059

          #5
          OP is using C, thats why c function is suggested

          Comment

          • Pshycoone
            New Member
            • Nov 2011
            • 3

            #6
            @Maraj;
            Haha yeah, I noticed that now, just a quick type error.

            @Weaknesssforca ts;
            Read my post again, this is C, not C++. :)

            @Johny10151981;
            I have read about those, I am however confused about wether it will allow me to send in a pure string, i.e. "exit". I had trouble assigning a string to a charArray directly.

            I thank everyone for their help, I haven't tried the solutions yet since I had trouble with the manifest/side-by-side configuration (error 14001 <3), but will however try again.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              If this is C you have to code:

              Code:
              if (!strcmp(choice, "exit"))
              {
                  do this ...
              }

              Comment

              • Pshycoone
                New Member
                • Nov 2011
                • 3

                #8
                It doesn't work completely, this is how it looks now;

                Code:
                int main(void){
                	char choice[20];
                	printf("Initialized - write help to view a list of commands. \n");
                	scanf_s("%s", &choice);
                	printf("%d", strcmp(choice, "help"));
                	while(RUN_LOOP == 1){
                		if(!strcmp(choice, "help") && EXECUTE_CMD > 0){
                			printf("Command help executed. \n");
                			printf("Help - shows a list of commands. \n Exit - shuts down the program. \n");
                			scanf_s("%s", &choice);
                		}else if(strcmp(choice, "exit") > 0){
                			break;
                		}
                		printf("%s",choice);
                	}
                	printf("Exiting software");
                	printf("%s", choice);
                	system("pause");
                	
                
                	return 0;
                }
                The problem here is;

                Code:
                if(!strcmp(choice, "help") && EXECUTE_CMD > 0){
                			printf("Command help executed. \n");
                			printf("Help - shows a list of commands. \n Exit - shuts down the program. \n");
                			scanf_s("%s", &choice);
                		}else if(strcmp(choice, "exit") > 0){
                			break;
                		}
                The strcmp returns -1 when printed out and when I've typed in "help" at the scanf_s()
                Last edited by Pshycoone; Nov 12 '11, 03:20 PM. Reason: Forgot to write more.

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  You are using scanf_s which is a more secure version of scanf. scanf_s requires that you specify the size of the destination buffer.

                  Your code:
                  Code:
                  scanf_s("%s", &choice);
                  does not do this.

                  Try:
                  Code:
                  scanf_s("%19s", &choice, 20);
                  and let me know what happened.

                  Comment

                  Working...