I’m running into a weird issue when I try and move my Do loop, or add another loop to nest it. I place the DO where you see it below, it loops through fine:
{
float numbers[MAX_DATA];
printf("Please Eneter the Amount of Numbers You Would Like to Use\n");
scanf("%d",&amount);
if (amount>MAX_DATA)
{
printf("Your entry was too big");
}
else
{
input(numbers,amount);
do
{
printf("\nStatistical Calculator Menu\n");
printf("\n(1) Mean\n(2) Standard Deviation\n(3) Range\n(4) Restart/Exit\n");
scanf("%d",&input2);
if (input2==1)
{
mean(numbers,amount);
}
else if (input2==2)
{
standard(numbers,amount);
}
else if (input2==3)
{
range(numbers,amount);
}
else if(input2==4)
{
printf("Would You Like to (5)Restart or (6)Quit?");
scanf("%d",&input2);
}
}
while(input2!=4);
}
getch();
return 0;
}
But if I place it here or add an additional do loop to nest it, I get a “expect while before getch” error.
{
int amount=0;
int input2;
do
{
float numbers[MAX_DATA];
printf("Please Eneter the Amount of Numbers You Would Like to Use\n");
scanf("%d",&amount);
if (amount>MAX_DATA)
{
printf("Your entry was too big");
}
else
{
input(numbers,amount);
printf("\nStatistical Calculator Menu\n");
printf("\n(1) Mean\n(2) Standard Deviation\n(3) Range\n(4) Restart/Exit\n");
scanf("%d",&input2);
if (input2==1)
{
mean(numbers,amount);
}
else if (input2==2)
{
standard(numbers,amount);
}
else if (input2==3)
{
range(numbers,amount);
}
else if(input2==4)
{
printf("Would You Like to (5)Restart or (6)Quit?");
scanf("%d",&input2);
}
}
while(input2!=4);
}
getch();
return 0;
}
Why do I get this error?
Please learn how to use indent (a C code beautifier).
Your indents and your logical flow are completely different.
Firstly, Why is the first if indented and the corresponding else should be at the same level of indentation.
The cause of your error is that the do loop is part of the “else” section of your if statement. But you indent it as if you want it to execute always.