• 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.
  • Posts

    • UK funds £60M AI labs to challenge US tech dominance with open-source models by Paul Hill The UK government has awarded £60 million to Oxford University and University College London to help keep the country in the AI race by focusing on open-source, low-hardware alternatives. This is in stark contrast to the expensive, closed-source, and high-hardware-requirement models being created in the United States and elsewhere. The money will be shared among two new academic research labs over six years to help them redesign the fundamental mathematics and architectures of AI to help the UK reduce its reliance on a handful of US tech firms. Commenting on the development, AI Minister Kanishka Narayan said: Initially, the government planned to fund just one lab with a £40 million investment, but with this update, two labs will now get access to a larger pool of funds. The labs are expected to invest in the top AI researchers at every career stage, with £2 million per lab being set aside for hiring at least ten doctoral students. The government hopes that this will grow the UK’s talent in the field of AI. The labs are also expected to work closely with the leaders in British AI research, such as the Alan Turing Institute and UKRI’s AI research hubs. This will allow the various teams to collaborate and create new solutions faster than they could alone. This development is pretty interesting for a number of reasons, chiefly that it could create a long-term challenge for US tech firms if these labs successfully scale these open-source architectures that bypass the proprietary ecosystems. It could also give British businesses and public sector organizations access to AI features without paying high licensing fees to foreign providers or needing to invest in specialized server infrastructure.
    • If I were them, I'm gonna hold out until the prices of these semiconductor parts normalize. $1,049 for a ~5 year old hardware is DoA, more so for gamers. On a deeper note, if Steam Machine is priced like this, we are soo effed up for the next gen Xbox console and PS6. With great AAA titles releasing at the end of the year, this just creates more demand...and more tears for me. Lol.
    • I suspect one reason its hard to justify subsidising is that it's not a locked down device like a traditional console is. In this climate sadly if it was a "good deal" you'd get people hoarding them for anything but gaming. The Lenovo Legion Go 2 with the Ryzen Z2 Extreme is £1300 here ($1720) for some context on how other SteamOS like devices are now priced. I got the older Z1 Extreme model for £300 with a dock, just shows how insane prices have got recently.
    • If you have an account with any of Meta's services, you technically have an account with all of their services. You may not use it, but it exists.
  • Popular Contributors

    1. 1
      +primortal
      469
    2. 2
      +Edouard
      185
    3. 3
      PsYcHoKiLLa
      93
    4. 4
      Michael Scrip
      87
    5. 5
      neufuse
      67
  • Tell a friend

    Love Neowin? Tell a friend!