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;
}

Dani AI

Generated

— the skip is the classic mix-of-operator>> and getline problem. As pointed out, the formatted extraction you used for the integer leaves the newline in the input buffer; the next line-reading call immediately sees that newline and returns an empty line. That diagnosis is correct.

Three safe ways to fix it (pick one that suits your flow):

  • Read the integer as a full line and parse it. This keeps all reads as line-oriented and avoids leftover characters:

    std::string line;
    if (std::getline(std::cin, line)) {
      populationsize = std::stoi(line); // add try/catch or validation
    }
  • Consume leading whitespace before the text-read. Using the stream whitespace manipulator with getline makes the getline skip leftover newline/space:

    std::string input;
    std::getline(std::cin >> std::ws, input);
  • Keep formatted extraction for numbers but ensure the remainder of that line is discarded before the next getline (see for the concise suggestion). When doing that, also check stream state and clear errors before continuing.

A couple of extra notes tied to your snippet: you declared both a type/object named phrase and later a std::string phrase — rename the string to something like target or inputPhrase to avoid confusion for readers and maintainers. Add basic validation when converting strings to integers (stoi throws on bad input) and always check std::cin state after reads if the program will continue.

Quick troubleshooting: isolate the input section into a tiny test program to reproduce the behavior, print the numeric values of leftover characters if needed, and try each fix above to see which integrates cleanly into your design.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.