Applying a do while loop to my program

  • Thread starter Thread starter clook
  • Start date Start date
  • Tags Tags
    Loop Program
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
7 replies · 19K views
clook
Messages
32
Reaction score
0
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:
Code:
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:
#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;
}
 
Physics news on Phys.org
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.
 
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 :(
 
You can do:
Code:
while(1){
    if(someStatement){
         // break loop
         break;
    }else{
        // continue loop
    }
}

Or, say you want to ask 10 questions:
Code:
int nTotalQuestions = 10;
int nCurQuestion;
int nRightAnswers = 0;
int nWrongAnswers = 0;

for(nCurQuestion=0; nCurQuestion<nTotalQuestions; nCurQuestion++){
   if(askQuestion(nCurQuestion)){
       nRightAnswers++;
   }
}
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:
-Job- said:
You can do:
Code:
while(1){
    if(someStatement){
         // break loop
         break;
    }else{
        // continue loop
    }
}

Or, say you want to ask 10 questions:
Code:
int nTotalQuestions = 10;
int nCurQuestion;
int nRightAnswers = 0;
int nWrongAnswers = 0;

for(nCurQuestion=0; nCurQuestion<nTotalQuestions; nCurQuestion++){
   if(askQuestion(nCurQuestion)){
       nRightAnswers++;
   }
}
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.
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.
 
Do something like this:

Code:
#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)");
    while(1){
        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;
        }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;
        }
        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;
}
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:
thanks, works like a charm :)
 
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.
please help me. asap
thanks