-1

this program is almost finished. The only problem is that when the user inputs quit quit, it should close the program. But it doesn't and it keeps repeating. I know that if I use it a single character (such as 'q') it works because I already tried it. But I want the program to detect that the user has input the string "quit". Could you help me with this? Thanks a lot!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILENAME "sales.txt"

int main()
{
   char firstName[41];
   char itemSold[41];
   FILE *salesFile;

   salesFile = fopen(FILENAME, "w");

   if( salesFile == NULL )
   {
       printf("Error opening file\n");
       return 1;
   }

   printf("Please enter first name and item sold\n");
   printf("Please quit quit to end\n");
   printf("> ");
   scanf("%40s %40s", firstName, itemSold);

   while(firstName != "quit" && itemSold != "quit")
   {
       fprintf(salesFile, "%s %s\n", firstName, itemSold);
       printf("> ");
       scanf("%40s %40s", firstName, itemSold);
   }

   fclose(salesFile);

   return 0;


}
Eddy Mogollón
  • 15
  • 1
  • 1
  • 4

1 Answers1

1

You compare strings not with:

firstName == "quit"
firstName != "quit"

(which just compares addresses of them), but with:

strcmp (firstName, "quit") == 0
strcmp (firstName, "quit") != 0

which compares the content.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Awesome! It worked, now I got it. I was confused because I did not want it to equal to 0, but to actually be not equal. But I made it work, thanks so much! – Eddy Mogollón Nov 28 '14 at 20:21