# Applying a do while loop to my program

1. Mar 9, 2006

### clook

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:

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:
Code (Text):
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.

Code (Text):
#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("\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");
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;
}

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

2. Mar 9, 2006

### neurocomp2003

ooh ugly tabbing..

for the loop criterion, i suggest using the concept of a flag, don't know if you learned the boolean stuff yet. and use a while(true) rather than a do {} while. If you haven't learned boolean then an INT can be used in place you just designate the value you want as true and the others as false.

for the 6/8: you need variables to save accumulate these 2 values,
these can be updated using the prefix or postfix increments ++;

for the final printf conclusion you should be able to do, msdn has a good online library but if your using linux hopefully the man pages will have them

lastly your do while loop will not work...do while uses boolean logic and
is not an IO command.

3. Mar 9, 2006

### clook

sorry, i haven't learned boolean yet.

would you mind giving an example of where you can use INT for loops?

and i had no ideawhat you said for the 6/8. sorry.. i'm pretty new to programming :(

4. Mar 9, 2006

### -Job-

You can do:
Code (Text):

while(1){
if(someStatement){
// break loop
break;
}else{
// continue loop
}
}

Or, say you want to ask 10 questions:
Code (Text):

int nTotalQuestions = 10;
int nCurQuestion;

for(nCurQuestion=0; nCurQuestion<nTotalQuestions; nCurQuestion++){
}
}
printf("You have correctly answered %d out of %d questions right", nRightAnswers, nTotalQuestions);

Where askQuestion is a function that asks a question, waits for the user answer, determines if it is correct or not, and returns 1 if the user answered right and a 0 if otherwise.

Last edited: Mar 9, 2006
5. Mar 9, 2006

### clook

hmm, could that be tailored to make the program accept as many questions as the user wanted?

also, where would i enter the loop? the while statement in front and the if else in the bottom?

if you could implement it into the code i gave above, that would be great. i'm trying to learn how they could connect.

6. Mar 9, 2006

### -Job-

Do something like this:

Code (Text):

#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("\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)");
while(1){
printf("\n\n");
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;
}else if (sign == '-'){
newDen = n1.den * n2.den;
newNum = n1.num*n2.den - n2.num*n1.den;
}else if (sign == '/'){
newDen = n1.den * n2.num;
newNum = n1.num*n2.den;
}else if (sign == '*'){
newDen = n1.den * n2.den;
newNum = n1.num * n2.num;
}else if (sign == '#'){
return 0;
}
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;
}

Keping track of right/wrong answers then is pretty straightforward, keep a variable for each, initialized to 0, and increment whenever you find a right/wrong answer. After the loop has exited you can print something about how many questions the user got right/wrong. Notice that i'm using return 0 to exit the whole program when i see a "#'. If you want to do something after the loop, then just do break; instead of return 0;, this way whatever you have after the loop will execute.

Last edited: Mar 9, 2006
7. Mar 10, 2006

### clook

thanks, works like a charm :)

8. Oct 6, 2009

### riofu

i also need an immediate response.
i have to make a program that will display a two year horoscope and using a do while loop.