12 Replies - 604 Views - Last Post: 05 April 2012 - 08:57 AM Rate Topic: -----

#1 rudolfone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-April 12

How to read a line with strings and integers alternating

Posted 02 April 2012 - 10:30 AM

Hey guys,

i need to read a line from a file which looks like that:

John Maynard 1234567 Astronomy 87 Mechanics 56 Linear Algebra 40


I only know how to read something like (by using getline):

John Maynard: 1234567

Or probably it's not possible, but how can i read following line:

John Maynard: 1234567: Astronomy: 87: Mechanics: 56: Linear Algebra: 40


What tools do i need to use? i haven't found anything useful via google.

Also, it should be an arbitrary number of courses (eg. astronomy, mechanics etc.).

Has anyone some hints for me how to solve this problem?


Cheers!

Is This A Good Question/Topic? 0
  • +

Replies To: How to read a line with strings and integers alternating

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4280
  • View blog
  • Posts: 13,443
  • Joined: 25-December 09

Re: How to read a line with strings and integers alternating

Posted 02 April 2012 - 10:57 AM

This is basically a Comma Separated File, except it is using a colon as the separator. So if you search for information on reading CSV files you should find many helpful links.

Jim

This post has been edited by jimblumberg: 02 April 2012 - 10:57 AM

Was This Post Helpful? 1
  • +
  • -

#3 rudolfone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-April 12

Re: How to read a line with strings and integers alternating

Posted 03 April 2012 - 04:34 AM

how shall i know it's called csv file...never heard of it :D

thanks, mate!

i nearly found what i need but i want to store my data in different strings/integers and vectors of strings and integers
for instance:

John Maynard 1234567 Astronomy 87 Mechanics 56 Linear Algebra 40

string name;
int id;
vector<string> course;
vector<mark> mark;

it should be stored in the following way:

John Maynard in name
1234567 in id
astronomy, mechanics and linear algebra in course
87, 56 and 50 in mark

Everyone is using getline like that: while(getline(inputfile,anystring, ','))
but in my problem i need to store my data from the same line in different datatypes and i think getline doesn't work for that.
so what can i use instead?
thanks in advance!!
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4280
  • View blog
  • Posts: 13,443
  • Joined: 25-December 09

Re: How to read a line with strings and integers alternating

Posted 03 April 2012 - 05:47 AM

You can use getline() to get your strings, and the extraction operator to extract the numeric inputs followed by the extraction operator to extract and discard the separator.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 rudolfone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-April 12

Re: How to read a line with strings and integers alternating

Posted 04 April 2012 - 05:07 AM

alright, thanks for that tip. it works now.
Was This Post Helpful? 0
  • +
  • -

#6 rudolfone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-April 12

Re: How to read a line with strings and integers alternating

Posted 04 April 2012 - 08:29 AM

unfortunately, my idea wasn't perfect. ;) i still struggle with reading the second line properly. cos once i use my vector (for the different courses in line 1), it stores the name from the second line in my vector but i'd like to use another string for this name. so i think i need to stop after reading my first line and after that i just need to run this code again but i don't know how to tell the compiler to stop after reading one line successfully.

does anyone have a good tip or an example code for me? :)

Thank you!
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 4280
  • View blog
  • Posts: 13,443
  • Joined: 25-December 09

Re: How to read a line with strings and integers alternating

Posted 04 April 2012 - 08:32 AM

Show your code that is causing you problems and ask specific questions about that code, and maybe we can point you in the correct direction.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 rudolfone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-April 12

Re: How to read a line with strings and integers alternating

Posted 04 April 2012 - 09:23 AM

okay thank you.
my code works fine for one line but it doesn't for two or more. i don't want to use my vectors to store the name and the id. to get that i think i need to make clear when my first line ends. my code shall start from the beginning then. but i really don't know how to do that.

this is my code:

#include <iostream>
#include <vector>
#include <string>
#include <fstream>

using namespace std;

int main(){

ifstream input;
input.open("database.txt");

string name;
int id;
vector<string> course;
vector<double> mark;

			getline(input,name,',');
			input>>id;
			cout<<"Student name:	"<<name<<"	"<<"Student ID:	"<<id<<endl;
			string s;
			getline(input,s,',');
			while(getline(input,s,',')){
				course.push_back(s);
				int no;
				input>>no;
				mark.push_back(no);
			
				//cout<<"Course: "<<s<<" Mark: "<<no<<endl;
				getline(input,s,',');
			}
			int t = course.size();
			for(int i=0;i<t;++i)
			cout<<"Course:	"<<course[i]<<"	"<<"Mark:	"<<mark[i]<<endl;

} // end of main


/*
database.txt:

John Smith,1234567,intro to qm,87,programming in c++,60,java,100
Thomas Edison,12567,intro to qm,87,programming in c++,60
John Adams,123467,intro to qm,87,programming in c++,60,pascal,99
James Bond,123456,intro to qm,87,programming in c++,60

*/


Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 4280
  • View blog
  • Posts: 13,443
  • Joined: 25-December 09

Re: How to read a line with strings and integers alternating

Posted 04 April 2012 - 09:58 AM

Did you create your database.txt file, or was that file given to you?

What exactly are you trying to retrieve and use from your file?

Do you need to retrieve all the fields from the file or just a few?

You should start to think about placing the data into a structure, and then creating a vector of that structure instead of multiple vectors.

Jim
Was This Post Helpful? 1
  • +
  • -

#10 rudolfone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-April 12

Re: How to read a line with strings and integers alternating

Posted 05 April 2012 - 02:47 AM

i created the file database.txt and i need to read and store all the data and i want to print it in a more sensible way.

i think it doesn't matter how the database.txt look like but i just thought a format like that "Thomas,12345,C++,60,Mechanics,70,QM,60" could be easy to read but apparently i'm wrong.

actually the code i posted is just a part of my programme and name, id and my vectors are inside a class.


is there an easier format in order to read the data properly?
Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,857
  • Joined: 16-October 07

Re: How to read a line with strings and integers alternating

Posted 05 April 2012 - 04:03 AM

You need to first read then entire line. Then, read the elements of that line. Look into std::stringstream. This magic little object turns strings into, well, streams.

sAlso, use some functions and structs! This is how I'd start your program:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

struct Student {
	string name;
	int id;
	vector<string> course;
	vector<double> mark;
};

typedef vector <Student> Students;

void print(const Student &);
void print(const Students &);
void read(istream &, Student &);
void read(istream &, Students &);
void read(const char *, Students &);


int main() {
	Students students;
	
	read("database.txt", students);
	print(students);
	
	return 0;
}



Ok, to be honest... parallel vectors?!? No, bad, don't do it. Instead:
struct Course {
	string name;
	double mark;
};

struct Student {
	string name;
	int id;
	vector<Course> courses;
};



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon


Reputation: 4280
  • View blog
  • Posts: 13,443
  • Joined: 25-December 09

Re: How to read a line with strings and integers alternating

Posted 05 April 2012 - 06:52 AM

By using commas to separate your fields your fields can contain spaces and still be processed easily. Just remember when you set up your data file it is easiest if each line contains the same number of elements. However you can store your data with a different number of fields as long as there is a way of telling where the variation starts and ends. Which in your case the variation starts after the second entry and would stop at the end of the line. IE:

Quote

Thomas,12345,C++,60,Mechanics,70,QM,60
Joe Jones,12349,Intro into C,50, Quantum Mechanics, 90, Basic Accounting, 20, Astronomy 102, 90
Bob Smith,32541,Advanced C++, 89, Basic Algebra,90

You start by retrieving the entire line into a string then process that string using stringstreams. When parsing your student string you start by retrieving the name, and student Id. Then in a loop retrieve the course title then course grade until you process all the courses. Continue this for each line.

Jim
Was This Post Helpful? 0
  • +
  • -

#13 rudolfone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-April 12

Re: How to read a line with strings and integers alternating

Posted 05 April 2012 - 08:57 AM

alright thank you very much for helping me. my code works now. i only have a minor problem cos i cannot read the last line but i can cheat easily :) by extending the file by 1 line
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1