Some input is being skipped and it sets the variable to null or empty or something. Here is the relevant code
1 2 3 4 5 6 7 8 9 10 11 12 13
shortint bindedPort;
//prompt for the port
std::cout<<"What port would you like to bind to?"<<std::flush;
std::cin >> bindedPort;
std::cout << std::endl;
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
// Let the server know this client is connected
std::string ID;
std::cout<<"\nEnter an ID: "<<std::flush;
ID = std::cin.get();
The first cin works but the second one is skipped and an empty string is sent to the server. Can someone help me?
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
short bindedPort;
//prompt for the port
std::cout<<"What port would you like to bind to?"<<std::flush;
std::cin >> bindedPort;
std::cout << std::endl;
//std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
// Let the server know this client is connected
std::string ID;
std::cout<<"\nEnter an ID: "<<std::flush;
std::getline(cin, ID);
return(0);
}
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
short bindedPort;
//prompt for the port
std::cout<<"What port would you like to bind to?"<<std::flush;
std::cin >> bindedPort;
std::cout << std::endl;
//std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
// Let the server know this client is connected
std::string ID;
std::cout<<"\nEnter an ID: "<<std::flush;
cin.get(); //changed
std::getline(cin, ID);
system("pause");
return(0);
}
Is this what you wanted? Also I have a question why do you have using namespace std; but you are typing std before everything?