954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

why does it skip when reading input??

its it skipping. This is the part of the code. :(

cout<<"enter a string\n";
getline(std::cin,phrase);

#include <iostream>
#include <string>
#include "phrase.h"
#include "EA.h"
#include <cstdlib>
#include <time.h>

int main()
{
	phrase a;
	EA b;
	std::string phrase;
	int populationsize;
	//int percentmutation;
	std::cout<<"enter a population size\n";
	std::cin>>populationsize;
	b.setpopulationsize(populationsize);
	//std::cout<<"enter the percentage you would like to mutate\n";
	//std::cin>>percentmutation;
	std::cout<<"enter a string\n";
	getline(std::cin,phrase);
	
	//std::cout<<a.getphraselength();

	srand ((unsigned)time(0));
	
	
	b.initalizepopulation();
	/*while(phrase!=bestofpopulation())
	{
		//mutate and crossover to create new population
	}*/
	
	return 0;
}
lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

Put in a cin.ignore() after line 16 and before line 21. Your excess '\n' from when you enter the integer remains in the stream and gets caught by the getline. getline figures it has a line and stops reading.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You