why this do while loop keep running?

Hi all,
I'm new to c++ and try to do this homework but run into this problem. If I commend the the first 4 line as shown below, the loop work as expected. But if I take away the comment the loop run nonstop. What did I do wrong? Thanks in advance for any input!

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;



int main()
{
char string1[30];
int encryp_code;


/*cout<<"****ENCRYPTER****"<<endl;
cout<<"Insert the secret phrase: ";
cin>> string1;
cout<<endl;*/

do
{
cout << "Insert the encrypting code <1-50> ";
cin >> encryp_code;

}
while (encryp_code < 1 || encryp_code > 50);





return 0;
}
I don't see anything blatantly wrong. Have you given it input that should stop it?
Your phrase probably contains a space.

Using cin >> with a string will get the first *word*, not a line.

Use getline() and it should solve the issue.
Is it the full code or just a fragment of it...if its just a fragment then plz post the full code....
Hi all thanks for the responding! I'm able to input the string but when I hit enter the loop run none stop. This is a part of the program where I supposed to encrypting the input string then using the encrypting code number from 1-50. I will try as firedraco suggested to see what happening. Thanks again!
Well, the string1 has nothing to do with his do while loop here. So unless your feeding it weird input for encryp_code, I don't see what could be going wrong. And OP isn't mixing cin.getline() with cin<<, so there shouldn't a buffer issue.
Hi all, I got it.

Here is my code that I fixed. I change "string" to "<cstring>" and at the body I change as suggested "cin.getline (string1,30);". Thank you you all!

#include <iostream>
#include <cstring>
#include <iomanip>

using namespace std;



int main()
{
char string1[30];
int encryp_code;


cout<<"****ENCRYPTER****"<<endl;
cout<<"Insert the secret phrase: ";
cin.getline (string1,30);
cout<<endl;

do
{
cout << "Insert the encrypting code <1-50>: ";
cin >> encryp_code;
}
while (encryp_code < 1 || encryp_code > 50);





return 0;
}
Topic archived. No new replies allowed.