Do While loop help

#include <stdio.h>
#include<math.h>
void main() {

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?
a do-while loop needs a ; at the end. The second to last line should be
}while(ask=='y');
Last edited on
#include <stdio.h>
#include<math.h>
void main() {

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');
}

I get runtime errors with this. What do I do?
#include <stdio.h>
#include<math.h>
void main() {

char ask;
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;
ask=' ';
printf("Do you want to do this again? (y/n) ");
scanf("%c",ask);
getchar();
}while(ask=='y');
}

okay, I got it but I get a weird error that opens input.c which has all of that jazz on it
bump
you can not declare main as void. Declare it
int main()
and don't forget to put a
return 0
or something of the sort at the end.
Topic archived. No new replies allowed.