LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-16-2012, 06:40 PM   #1
deadleaves
LQ Newbie
 
Registered: Mar 2012
Posts: 1

Rep: Reputation: Disabled
c++ help?


Hi, im very new to C++ programming and i need some help. We are making an algorithm program that pulls information from a file.
the file shows the name, years worked, salary, and monthly sales.
The file reads:
Mom and Pop Store Number 3 Some Town in WY
Pop Hacker 15 2500 2000
Mom Hacker 15 2500 500
Junior Hacker 5 1500 5000

how do i get the name, base salary, years, sales for each employee? Now i have done research on the getline() function but im still lost on how to do this. As said im very new at programming so sorry for the newbie questions.
 
Old 03-16-2012, 10:19 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Not a problem!

You already know that the getline() function exists to retrieve "a line of" data from the file. That's the first step.

So... now you have "a string of text," so the next objective will be how to break apart that string of text into usable pieces ... and here is where you might have a bit of thinking-cap work to do!

The string "Mom and Pop Store Number 3 Some Town in WY" consists of nine space-delimited words, whereas the next string, "Pop Hacker 15 2500 2000" (and every string thereafter...) appears to consist consistently of five.

Therefore the thought question: "what's the bright line rule?" You decide. But, you have to decide, because a digital computer only deals in "1" vs. "0". True or False. Yes or No.

And so it goes... I hope that this little diatribe gives you some sort of useful starting-point and food for thought.

And, trust me, there are no "newbie questions." My first computer program (never-you-mind how many years ago...) was eight lines long, took me six months to write, and had a bug in it. I'm still fascinated by computer programming, and I still enjoy doing it. Welcome to the profession.

Last edited by sundialsvcs; 03-16-2012 at 10:21 PM.
 
Old 03-20-2012, 02:06 AM   #3
mashhur
LQ Newbie
 
Registered: Jan 2012
Posts: 14

Rep: Reputation: Disabled
Quote:
Originally Posted by deadleaves View Post
Hi, im very new to C++ programming and i need some help. We are making an algorithm program that pulls information from a file.
the file shows the name, years worked, salary, and monthly sales.
The file reads:
Mom and Pop Store Number 3 Some Town in WY
Pop Hacker 15 2500 2000
Mom Hacker 15 2500 500
Junior Hacker 5 1500 5000

how do i get the name, base salary, years, sales for each employee? Now i have done research on the getline() function but im still lost on how to do this. As said im very new at programming so sorry for the newbie questions.
hi there,
it is not big deal, you need to know only strtok() function (see its prototype and how to use: http://linux.die.net/man/3/strtok)
and also you can be googling with the "how to parse string in c" key words.

good luck!
 
Old 03-20-2012, 04:53 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by mashhur View Post
hi there,
it is not big deal, you need to know only strtok() function (see its prototype and how to use: http://linux.die.net/man/3/strtok)
and also you can be googling with the "how to parse string in c" key words.

good luck!
For a simple string, such as the one the OP has posted, usage strtok() is not necessary. Using std::istringstream should be sufficient.

@ the OP: Here is a link to the std::istringstream documentation: http://www.cplusplus.com/reference/i...istringstream/
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration