-4

Below is my c code and the forever for(;;) loop is not breaking out base on the condition i have given it using an if statement. Is there something am doing wrong? my codes are as below:

 #include <stdio.h>
 main()
{
/*
 * Program starts
 */

 int       tracks;                             /* tracks is declared as a variable for the number of tracks */
 float     price;                              /* Price is declared as variable for number of tracks */
 char      title[100];                         /* title is declared as a varibel for the title of thr CD*/
 char      album_single[2];                    /* album_single is a variable declared  for either the CD is a single or an album */
 char      artiste[50];


 printf("Welcome to the CD database\n\n");
 printf("Please enter the details of the CD below...\n");

/*
 * First, title
 */
 printf("Title? ");
 scanf("%[^\n]", title);

/*
 * Next, Artiste
 */
 printf("Artiste? ");
 fflush(stdin);
 scanf("%[^\n]", artiste);

/*
 * Next, number of tracks
 */
 printf("Number of Tracks? ");
 fflush(stdin);
 scanf("%d",&tracks);

/*
 * Next, Type(album or single)
 */

 for(; ;)
 {
     printf("ALbum or a single (Enter 'a' for an album and 's' for a single): ");
     fflush(stdin);
     scanf("%c", &album_single);

     if(album_single == "a" || album_single == "s")
          break;

     printf("Error!\n");
 }


/*
 * Conditions to assign the right type(album/single) to the variable album_single
 */
 if(strcmp(album_single, "a") == 0)
 {

      strcpy(album_single,"Album");
 }
  else
  {
     if(strcmp(album_single, "s") == 0)
         strcpy(album_single, "single");
  }

/*
 * Finally, Price
 */
 printf("Retail Price(e.g $4.66)? ");
 fflush(stdin);
 scanf("%f", &price);

/*
 * Details, finallly output
 */
 printf("\n\nDetails of  %s's CD\n", title);
 printf("========================\n");
 printf("Title: %s\n",title);
 printf("Artiste: %s\n", artiste);
 printf("Number of tracks: %d\n",tracks);
 printf("Album/Single: %s\n", album_single);
 printf("Price:$ %.2f\n", price);
 printf("========================\n");

/*
 * User Friendly exit of the program
 */
 printf("\n Press ENTER to exit the program.");

/* 
 * Program end
 */
 fflush(stdin);
 getchar();
}

Below is the part of the forever for(;;) loop which is not breaking out:

 for(; ;)
 {
     printf("ALbum or a single (Enter 'a' for an album and 's' for a single): ");
     fflush(stdin);
     scanf("%c", &album_single);

     if(album_single == "a" || album_single == "s")
          break;

     printf("Error!\n");
 }

This loop keep on looping even if the input is 'a' or 's'. what am i doing wrong in this codes ?

3 Answers3

0

Try this:

char      album_single; 

while (album_single != 'a' && album_single != 's')
{
     printf("Album or a single (Enter 'a' for an album and 's' for a single): ");
     scanf("%c", &album_single);
     scanf("%c"); // discard carriage return
}

Experiment commenting out the last scanf() statement, see what happens.

Nav
  • 13
  • 3
  • `fflush(stdin)` is undefined behavior in Standard C. – ad absurdum Mar 19 '17 at 22:38
  • @DavidBowling fair enough, I removed the call. It did compile & execute on gcc however. – Nav Mar 19 '17 at 23:02
  • @Nav-- it is explicitly UB in the Standard, but that does not stop individual implementations from defining a behavior. You might double check that `fflush(stdin)` works as expected with GCC; though the Linux man pages suggest that `fflush(stdin)` is defined, it has never worked when I tested it, and there is an argument that this may not work when `stdin` is a terminal [in this excellent discussion of the issue](http://stackoverflow.com/a/34247021/6879826). Nonetheless, it is certainly not portable. – ad absurdum Mar 20 '17 at 00:30
0

this code solved the question using the strcmp() function in the comparison instead of the ==.

correct code if(strcmp(album_single, "a") == 0 || strcmp(album_single, "s") == 0 )

wrong code if(album_single == "a" || album_single == "s")

thanks for ur contribution guys .!!

0

You can not compare strings using == operator. Either declare album_single as a character, or use strcmp() function.

if(strcmp(album_single, "a")==0||strcmp(album_single, "s")==0) break;

Remember to include the correct header file.

Devansh
  • 293
  • 1
  • 3
  • 10