• 0

(C++) cin.ignore question


Question

I am slightly confused as to how cin.ignore works. I'll try to make this short. I need to write a program where the user inputs a bunch of data in 1 string, for example : 46728s1000 2750.55John Brown

I need to extract the different parts of that input. I need to extract the first 5 digits, the 's', the 1000, the 2750.55 and John Brown.

int getInformation (bool isFirstInput)

{	int accountNumber;
	char accountType;
	int minBalance;
	double oldBalance;
	string firstNamelastName;
	const string welcomeMessage = "Welcome to Brandon Lashmet's payroll program\n. Please enter account number, account type, minimum balance, a space, old balance, and first and last name of account owner.";

	if (isFirstInput==true)
	{
		cout<<welcomeMessage;
	}
	else
	{
		cout<< "Thank you. Please enter next user's information";
	}

		cin>> accountNumber;
		cin.ignore(5);
		cin>> accountType;
		cin.ignore(6);
		cin>> minBalance; 
		cin.ignore(11);
		cin>> oldBalance;
		getline(cin, firstNamelastName);

}

Does anyone have any idea how this works? Thanks.

Link to comment
https://www.neowin.net/forum/topic/677750-c-cinignore-question/
Share on other sites

7 answers to this question

Recommended Posts

  • 0

I am not a big C++ user, but in c# I would read the entire input and then use string functions to look at it. From there you could split it up. From this site: c++ reference it seems like you can use the brackets [] operator to access a part of the string. There is also a length method in the class. Hope this helps.

  • 0

again, I am not a c++ developer, but I think it would be kind of like this

string variable;

cin >> variable;

string part1;

part1 = variable[0] + variable[1] + variable[2] + variable[3] + variable [4]; //messy concatenation, sure there is a better function

in c# this would be much easier, but seems like you need to use c++ for some reason.

I am sure the std::str library provides much easier functions, but that is a [downside] of c++, way too many options and libraries to choose from.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.