char ask;
do{
double ai, aj, ak;
printf("Please enter an ai value. ");
scanf("%lf", &ai);
getchar();
printf("Please enter a aj value. ");
scanf("%lf", &aj);
getchar();
printf("Please enter an ak value. ");
scanf("%lf", &ak);
getchar();
printf("ai=%lf\naj=%lf\nak=%lf",ai,aj,ak);
getchar();
double ai_2, aj_2, ak_2, sum, magnitude;
ai_2=pow(ai,2);
aj_2=pow(aj,2);
ak_2=pow(ak,2);
sum=aj_2+ak_2+ai_2;
magnitude=sqrt(sum);
printf("Your magnitude is %lf",magnitude);
getchar();
printf("Do you want to do this again? (y/n) ");
getchar();
}while(ask=='y')
}
I have a problem with the do while loop. I want the program to repeat the code if the user presses 'y' and exit if they press anything else. Can anyone help?
char ask;
do{
double ai, aj, ak;
printf("Please enter an ai value. ");
scanf("%lf", &ai);
getchar();
printf("Please enter a aj value. ");
scanf("%lf", &aj);
getchar();
printf("Please enter an ak value. ");
scanf("%lf", &ak);
getchar();
printf("ai=%lf\naj=%lf\nak=%lf",ai,aj,ak);
getchar();
double ai_2, aj_2, ak_2, sum, magnitude;
ai_2=pow(ai,2);
aj_2=pow(aj,2);
ak_2=pow(ak,2);
sum=aj_2+ak_2+ai_2;
magnitude=sqrt(sum);
printf("Your magnitude is %lf",magnitude);
getchar();
char ask;
printf("Do you want to do this again? (y/n) ");
scanf("%c",ask);
getchar();
}while(ask=='y');
}