PDA

View Full Version : applying a do while loop to my program


alvinleephd
03-10-2006, 04:26 AM
I have to create a program that will calculate addition, subtraction, multiplication, and division of fractions.

I have that part done.

But one other requirement is that I need to have it looped so that the user could continue as long as he likes.

I also need a way to output the number of correct answers out of the number of attempts. in this form:

You answered 6 out of 8 questions correctly.
You have an overall percentage of 75.0%. Good bye.
I have no idea how to do this. The only thing remotely similar in my knowledge is calculating average of values. Any tips or help would be appreciated.

For the loop, i attempted to use a do while statement like so:
do
while ("%c == %%", &tem);

however, i'm not sure if i did it right, or where exactly to place it. whenever i tried placing it somewhere, i twould give me 2-3 errors.

here's the whole program:
#include <stdio.h>
int main()
{

struct fraction
{
int num;
int den;

};
struct fraction n1,n2,n3;
char sign,tem;
int newDen,newNum;



{
printf("%s","Alvin Lee's Rational Tutorial Program.\n");
printf("\n\n");
printf("Please follow instructions carefully.");
printf("\n\n");
printf("Enter your operation like 1/2 + 1/4");
printf("\n\n");
printf("Enter % operator to stop the program (1/2 % 1/4)");

{

printf("\n\n");
printf("Please enter your operation:");
scanf("%d%c%d %c %d%c%d",&n1.num,&tem,&n1.den,&sign,&n2.num,&tem,&n2.den);
printf("\n\n");



if (sign == '+')
{

newDen = n1.den * n2.den;
newNum = n1.num*n2.den + n2.num*n1.den;


}


if (sign == '-')
{

newDen = n1.den * n2.den;
newNum = n1.num*n2.den - n2.num*n1.den;


}

if (sign == '/')
{


newDen = n1.den * n2.num;
newNum = n1.num*n2.den;


}

if (sign == '*')
{
newDen = n1.den * n2.den;
newNum = n1.num * n2.num;
}

printf("Please enter your result:");
scanf("%d%c%d", &n3.num,&tem,&n3.den);
if(n3.num==newNum && n3.den==newDen)
printf("\nCongratulations! It is correct: %d/%d\n",newNum,newDen);

else
printf("\nIt is incorrect. The correct answer is: %d/%d\n",newNum,newDen);

}
printf("Its floating point value is: %f", (float)newNum / (float)newDen);

}
return 0;
}

rlemon
03-10-2006, 04:47 AM
best not double post - not allowed. people will respond when they have time.

alvinleephd
03-10-2006, 04:49 AM
ah crap, i accidentally posted two threads. i thought the first one didn't go through.

deleting other thread..

Melon00
03-10-2006, 01:58 PM
Do you have to use a do while, is that part of the assignment or something? If not I would place a while(1) loop around everything in your main() except your declarations. Then prompt the user after the caculations are made if they would like to continue. If not, then break.

Example

int main()
//declarations here

while(1)
{
//prompt user, and calculate here

printf("Would you like to continue (Y/N)?");
scanf("%c", ans);
putchar('\n');
if(ans == 'n' || ans = 'N') break;
}

Melon00
03-10-2006, 02:03 PM
Your calculations for the percentage should be straight forward. All you need is a counter of how many times you go through the loop, then another counter that gets incremented only when the person gets a right answer. Then take a temporary float value and divide the two counters and spit it out into a printf statement.

oracleguy
03-10-2006, 10:31 PM
while(1)
{
//prompt user, and calculate here

printf("Would you like to continue (Y/N)?");
scanf("%c", ans);
putchar('\n');
if(ans == 'n' || ans = 'N') break;
}

IMO that isn't good programming practice.

A better method would be:

bool repeat = true;
do
{
//Prompt user and calculate here
printf("Would you like to continue (Y/N)?");
scanf("%c", ans);
putchar('\n');
if(ans == 'n' || ans = 'N')
repeat = false;
}
while(repeat);

Melon00
03-11-2006, 07:44 PM
No it probably isn't oracleguy, but it will work. I am used to programming embedded systems. Engineers I have worked with like to use the while(1) statements to signify that something is going to be constantly running until some action is going to take place, which makes larger code bases easier to read. Many firmware programs do this, so it is just a habit.