dimanche 22 avril 2018

Nested If statement Ignoring scanf function

I've made a program where the user inputs a x coordinate and a y coordinate, If the user inputs a letter I want the input to be repeated until a valid input has been inputed. After 3 attempts the program will end. However every time I run the program and input a test character the program skips the scanf function and doesn't test the nested if loops, I've tried the adding a space in the scanf function but That doesn't seem to work at all. Would appreciate some help thank you :)

Code:

printf("Insert first vector:\n");
vector1 = scanf(" %f %f",&x1,&y1);
printf("x1 %f y1 %f \n",x1,y1);
if(vector1 == 0)
{
  printf("Invalid input\n");
  printf("Insert first vector;  \n");
  vector2 = scanf(" %f %f",&x1,&y1);
   if(vector2 == 0)
   {
     printf("Invalid input\n");
     printf("Insert first vector;  \n");
     vector3 = scanf(" %f %f",&x1,&y1);
      if(vector3 == 0)
      {
       printf("Invalid input \n");
       x1=WRONG;

Aucun commentaire:

Enregistrer un commentaire