I understand the structure of a do while; however, when I start plugging in my statements, I am not gettng the result I want.
The logic is wrong. Knowing the general form of the do while is:

while (condition)

and wanting to write a do while code to find out if a number is prime I entered this into the complier:

int input_number2 = 0; //user input number for do-while loop
int mod_number2 = 0;  ////modulus number for do-while loop
int ctr_number = 0;	////counts factor
int i = 0;	


cout << \nPlease enter a number";
cin >> input_number2;

	ctr_number = 0;
	i= input_number2;



while(mod_number2 = i%1);

	cout <<"\nYour number is prime";

cout <<"\nYour number is not prime";
return 0;

Well it does not work. I am not sure of what to do with the counter. I was able to understand it while writing this code as a for loop; but, not as a do while. Any advise is always appreciated.

It may be because you have a semicolon at the end of this line:

while(mod_number2 = i%1);

Try removing that. Also, please use [ CODE ] tags around your code - what you posted was very difficult to read.


[ code ]
[ /code ]

but without the spaces between the brackets and the word code. This will make it show up nice and organized like it did in my reply.

one more thing, when I take the semi-colon out the code will not complie. Does not mean there is an error somewhere else?

This article has been dead for over six months. Start a new discussion instead.