Problem with Strings

0 Muhammed Ashab · September 22, 2015
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<math.h>

main()
{
int i, diceRolls, sum;
char con[3] ;
 while(1)
 {
     for(i = 0 ; i<20 ; i++)
     {
         diceRolls = (rand()%6) + 1 ;
         sum += diceRolls ;
         printf("Dice Rolled = %d\n", diceRolls);
     }
      printf("\nDo you want to continue?\n\n");
      gets(con);
      if(con != "yes") // the problem is here
      {
       break;
      }
      else
      {
       continue ;
      }
 }

}



I am having a problem with strings in an if else statement.
When i write "yes" in the program it stops the program rather than continuing the program.


Post a Reply

Replies

Oldest  Newest  Rating
0 Roberto Isuga · September 24, 2015
Why not just use
scanf(" %c", con);

and change your if statement like this

if(con=='y') {
continue;
}
else{
break;
}

that way you can repeat it again and again as long as you input "y"
0 c student · September 22, 2015
you cannot use comparisons like that in c.  use the strncmp function.  also, by typing "yes", you are effectively overflowing the buffer and it would intentionally overflow because you used the gets function as it is insecure for writing into buffers.  i very strongly suggest you use fgets instead of gets from hereon.
  • 1

C

107,040 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator