Applying a do while loop to my program

  • Thread starter Thread starter clook
  • Start date Start date
  • Tags Tags
    Loop Program
Click For Summary

Discussion Overview

The discussion revolves around implementing a looping structure in a program designed to perform arithmetic operations on fractions. Participants explore how to use a do while loop or alternative looping constructs to allow users to repeatedly perform operations and track their performance, including the number of correct answers.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant describes their program's requirements, including the need for a loop to allow continuous user interaction and to track correct answers.
  • Another participant suggests using a flag and a while(true) loop instead of a do while loop, citing the need for boolean logic.
  • A participant requests clarification on using integers as flags for loops, indicating they are new to programming.
  • Multiple participants provide examples of loop structures, including a while loop that can break based on a condition and a for loop for a fixed number of questions.
  • One participant proposes integrating a loop into the provided code and suggests how to track correct and incorrect answers.
  • A later reply confirms that the suggested code works as intended.
  • Another participant introduces a new topic, requesting help with a different program involving a do while loop for displaying a horoscope.

Areas of Agreement / Disagreement

Participants generally agree on the need for a looping structure but have differing opinions on the specific implementation details, such as the use of do while versus while loops. The discussion remains unresolved regarding the best approach for the new horoscope program.

Contextual Notes

Some participants express uncertainty about boolean logic and its application in programming, which may limit their understanding of certain suggestions. There are also varying levels of familiarity with programming concepts among participants.

Who May Find This Useful

New programmers seeking guidance on implementing loops in C programming, particularly in the context of user-interactive applications.

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
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K