Applying a do while loop to my program

In summary: Den = n1.den * n2.den; newNum = n1.num * n2.num; }else{ printf("Invalid operator entered. Exiting program."); break; } 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
  • #1
clook
35
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
  • #2
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
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
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:
  • #5
-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.
 
  • #6
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:
  • #7
thanks, works like a charm :)
 
  • #8
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
 

1. How does a do while loop work?

A do while loop is a type of loop in programming that executes a block of code repeatedly until a certain condition is met. The loop first executes the code block, and then checks the condition. If the condition is true, the loop will continue to execute. If the condition is false, the loop will end.

2. What is the syntax for a do while loop?

The syntax for a do while loop is as follows:

do {

// code to be executed

} while (condition);

The code block will continue to execute as long as the condition is true.

3. When should I use a do while loop?

A do while loop is useful when you want to execute a block of code at least once, regardless of whether the condition is initially true or not. It is also helpful when you want to repeat a task until a specific condition is met.

4. How do I avoid an infinite loop with a do while loop?

To avoid an infinite loop, make sure that the condition in the do while loop will eventually become false. This can be achieved by including a counter or a way to modify the condition within the code block being executed.

5. Can I use a do while loop with any data type?

Yes, a do while loop can be used with any data type, as long as the condition being checked is of a boolean type. This means that the condition should evaluate to either true or false.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
836
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
634
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
20
Views
2K
Back
Top