1

I want the loop to end when I type FINISH.

#include<stdio.h>

int main()
{
    int score,count=0;
    char name[10];
    printf("please enter your name and score by order(enter FINISH in name to end)-->");
    scanf_s("%s%d",name,10,&score);

    while (name!="FINISH")
Chris
  • 26,361
  • 5
  • 21
  • 42
  • 2
    Does this answer your question? [How do I properly compare strings in C?](https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c) – kaylum Aug 11 '22 at 04:43

3 Answers3

3

String comparisons don't work this way in C. Use the strcmp function from the string.h header. strcmp returns 0 to indicate equality.

while (strcmp(name, "FINISH") != 0)

As an added note, if you want to run this multiple times, reading in the first name and grade, then starting your loop doesn't make much sense. An "infinite loop" with a break if FINISH is entered makes more sense.

int main() {
    int score, count = 0;
    char name[10];

    while (1) {
        printf("please enter your name and score by order(enter FINISH in name to end)-->");
        scanf_s("%s%d", name, 10, &score);

        if (strcmp(name, "FINISH") == 0) break;

        // Do something with this data
    }
Chris
  • 26,361
  • 5
  • 21
  • 42
  • 1
    I don't know the details of the non-standard `scanf_s`, but this looks like potential undefined behavior if `scanf_s` does not write anything to the uninitialized `name`. Always check the value returned by `scanf` to ensure that the data you expect to have been written is actually written, or initialize the variables. Since this type of loop is a common source of error, you might want to structure it as `while( printf(...) > 0 && scanf_s(...) == 2 ){ ... }` – William Pursell Aug 11 '22 at 13:25
  • Agreed. I did not address that, focusing on the string comparison issue and loop logic. – Chris Aug 11 '22 at 19:32
0

The objective seems to be: prompt, input, evaluate, do stuff (maybe)

int score, count = 0;
char name[10];

while( 1 ) {
    printf("please enter your name and score by order(enter FINISH in name to end)-->");
    scanf_s("%s%d", name, 10, &score);
    if ( strcmp( name, "FINISH" ) == 0 )
        break;
    // Do something with this data
}
// wrap things up
Fe2O3
  • 6,077
  • 2
  • 4
  • 20
-1
#include <stdio.h>
#include <string.h>

int main() {
  char name[10];
  int score, count = 0;
  printf("please enter your name and score by order(enter FINISH in name to end) --> ");
  scanf("%d %s", &score, name);
  
  // strcmp will return 0 if it is same and break a loop
  while (strcmp(name, "FINISH")) {
    printf("please enter your name and score by order(enter FINISH in name to end) --> ");
    scanf("%d %s", &score, name);
  }
}
  • This answer would benefit from elaboration. – Chris Aug 11 '22 at 04:47
  • explain WHY use `strcmnp` – Mike Aug 11 '22 at 04:51
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 15 '22 at 06:51