C++

C++

I'm trying to read a file with dates and values separated by whitespace but I only care about the values. The dates are written like 4/21/2016 so I don't know what data type to use for that. Is there any way to just skip over the dates and use the values I want?

Other urls found in this thread:

pastebin.com/LNdF2P5M
pastebin.com/dFRKwNnn
twitter.com/SFWRedditImages

Please help.

fscanf("%d/%d/%d %d", &a, &b, &c, &d)

I only know some C but I guess I'd try something like this and just ignore a, b, c values

So it's like
"date dick_sucked"
>4/11/2016 9
>4/20/2016 18
>4/21/2016 15

#include
using std::fstream;
#include
using std::string;
using std::stod; // use stoi if it's integer data
#include
using std::vector;

int main(){
fstream dick_file("mydailydickcount.txt"); //opens you dick notch count
string dick_line;
vector dick_sucked;

while(dick_file.good()){
dick_file>>dick_line;
dick_sucked.push_back( stod( dick_line.substr(dick_line.find(" ")+1) ) );
}
dick_file.close(); //no more dicks out of you

//do shit with your dick sucking

return 0; //return the value your parents hold of you
}

Thanks man. Could you explain what happens in this line?

dick_sucked.push_back( stod( dick_line.substr(dick_line.find(" ")+1) ) );

I don't understand what happens after push_back

>dick_sucked.push_back( stod( dick_line.substr(dick_line.find(" ")+1) ) );

dick_line.find(" ") // finds where the space is and returns the position index
... +1 //moves the index to the number
dick_line.substr( ... ) // get the string starting at the number
stod/stoi( ... ) // turns the string into a double/int
dick_sucked.push_back( ... ) // adds it to the end of the vector keeping track of all the numbers

I thought I was going to be able to help you until you said the dates were listed like m/dd/yyyy. Your best bet will probably be to use cin.read(variable, number of characters to read) then cin.ignore(number of characters to ignore, character to ignore to). So, let's say you've named your input file file (type ifstream) and a line looks like the following:

value1 value2 value3 4/21/2016

You'll want to type the following:

file >> variable1 >> variable2 >> variable3;
file.ignore(1,' ');
file.read(month, 1);
file.ignore(1, '/');
file.read(day, 2);
file.ignore(1, '/');
file.read(year, 4);
file.ignore(1000, '\n');

Followup question to anyone who's good at c++, is it possible to make a vector (v1) that is type string and has elements that are made up of different iteration of v (using a while loop, so different values) which is type char? I don't know if that makes sense, I can clarify if needed.

>and has elements that are made up of different iteration of v (using a while loop, so different values) which is type char? I don't know if that makes sense, I can clarify if needed.

Not sure what you're trying to get at. Are you asking for something like stringstream?

So, basically, the situation is I need to decode the information on an input file (which adds two integers, the result of which correspond to a letter). So, my plan of attack has been using a while loop (which runs until the end of a line - which corresponds to one word) within a while loop (which runs until the end of a paragraph - the end of a sentence/message) within one last while loop (which runs until the end of the file). The inner most while loop decodes one letter each iteration and saves it to a vector, v. When this while loop stops running, it is the end of the line to be decoded (which corresponds to one word in the result). This word (which is currently saved on vector (type char), v, is then saved on vector, v1 (type string)). Finally v1 is saved onto vector, v2 (one message at a time).
Basically, characters are saved on v once at a time. Then when a word is formed I'm trying to save v onto v1 (one word at a time). Finally, when a message (sentence) is formed, I want to save the message (that is currently on v1) to v2. I will also need to reverse v2, but I'm not there yet.

I have the while loops set up fine along with saving spaces whrre needed, but do you know of a way to save iterations of v (in the innermost while loop) as the elements of v1? I think I could get the elements of v onto v1, but I would like the words in v1 to be the elements instead of individual characters being elements.

Possibly like stringstream (though I'm not really familiar with it). I seem to remember my professor recommending stringstream, I kind of just ran off on my own.

vector< vector > ?

Is that how I would declare v1? I never thought to do that. Right now I'm using push_back, so the line where an iteration of v becomes an element of v1 is as follows:

v1.push_back(v);

Now, I want the inner vector to be type char (I used cast to char to get a character from an integer) and the outer one to be type string, so would I just declare them as such individually at the top and write something like v1?

OP here. I tried to not give my entire problem initially and see if I could use what you guys wrote and apply it to my problem but I'm just having too much trouble so I'm gonna be more specific. The first two lines of my file look like this.

Date/Time Name of column 1 name of column 2 name of column 3
4/21/2016 10:45 column1data column2data column3data

Every row after that looks like row 2. All data are ints and doubles. So I have two problems. I want to completely ignore the first line and I want to ignore the date and time which are separated by a space.

Still looking into how to use this. Might be helpful.

I could use this if I only needed 1 value from each line but I need 3.

I tried using this:

file.ignore(15,'4/21/2016 10:45');
file>>value1>>value2>>value3;

It runs but returns all zeros for the values. I'd like to avoid using this however because I don't want to have to specify the amount of characters to ignore as it'll change according to the date and time.

I'm gonna be working on this all night so any help is appreciated.

>I could use this if I only needed 1 value from each line but I need 3.

same shit

#include
using std::stringstream;

...

if(dick_file.good()) dick_file>>dick_line; //throw out the first virgin dick
while(dick_file.good()){

...

size_t first_space=dick_line.find(" "); //pass the date
size_t second_space=dick_line.find(" ", firstspace+1); //pass the time
stringstream streaming_dicks >data1;
streaming_dicks>>data2;
streaming_dicks>>data3;

Thank you you dick obsessed motherfucker. I think this is gonna work. Currently it's telling me expected initializer before '

stringstream streaming_dicks;
streaming_dicks

print data1 gives me data from different columns and different rows in no order I can tell. data2 and data3 are all zeros.

I used a for loop instead of while(shits.good()) that counts to how many lines of code I use. I did this because some of the data are zeros and ones so I thought that give good() some trouble.

So when I print the counter variable n at the end of each loop (just to test), it starts at 22 as long as I initialize it as any number below 22. like -500 and 21. Both start at 22. Any number above 22 and it'll start there.

This is fucked. I'm just gonna post my code in a sec with variable and file names changed.

post the code on pastebin

pastebin.com/LNdF2P5M

Oh come on you mean I can't even post my homework an hour after you ask for it and still have you do it? sci is so lazy nowadays.

Would seriously appreciate any help on this. Can carry you in multiple videogames.

>I did this because some of the data are zeros and ones so I thought that give good() some trouble

It won't.

>print data1 gives me data from different columns and different rows in no order I can tell. data2 and data3 are all zeros

Whoops, forgot how >> worked - it stop at the first space. Use getline(file, string) to get the whole line like this:
pastebin.com/dFRKwNnn

or just go directly at it like this:

while(fakedatafile.good())
fakedatafile>>fakeline;//date
fakedatafile>>fakeline;//time
fakedatafile>>data1;
fakedatafile>>data2;
fakedatafile>>data3;
cout

You can't combine all of the ignores into one line. Imagine a cursor moving from left to right on whatever your input is, so you are moving it to the right by reading and saving information and by ignoring information.

> I'd like to avoid using this however because I don't want to have to specify the amount of characters to ignore as it'll change according to the date and time.

Oh, you mean like some days are one digit, some are 2?

>while(fakedatafile.good())
>fakedatafile>>fakeline;//date
>fakedatafile>>fakeline;//time
>fakedatafile>>data1;
>fakedatafile>>data2;
>fakedatafile>>data3;
>cout

Yes time as well plus I'd imagine I'd have to do that for every line which is super inefficient. But I like this guy's solution. It's really fucking close to working.

>So when I print the counter variable n at the end of each loop (just to test), it starts at 22 as long as I initialize it as any number below 22. like -500 and 21. Both start at 22. Any number above 22 and it'll start there.
>Only thing is it keeps starting on the 24th line (not including the first line).

Is it jumping to 24th line of the file or is it the int n jumping to 24?

Alrighty, sounds good. Best of luck.

Both. The first 3 data it prints are from the 24th line and then it prints N IS HERE 24. Next 3 data from the 25th line. Then N IS HERE 25. etc.

What does the for loop look like?

I went with the while loop. Here's the relevant part of the code.

int n = 0;

if(fakedata.good())){
getline(fakedata,trash);}

while(fakedata.good()){
fakedata>>trash;
fakedata>>trash;
fakedata>>data1;
fakedata>>data2;
fakedata>>data3;

cout

>n++;
>cout

Yeah everything is exactly as it is there. Just took out the stuff before it. I'll try that.

When I debug it starts at 1 properly. When I compile and run it starts at 24

What are you compiling with?

The little button at the top that says compile and run. Sometimes I hit the compile button then the run button.

...

I'm pretty new to C++ so sorry if this isn't what you're asking.

I decided if it was working in the debugger but not running properly that it was worth a shot to keep on trucking with the rest of my program and it seems to be working just fine now. Thank you a shit ton for helping out guys.

Use sstream and istringstream, it has space as the default delimiter

just prepare the file with cut or sed or something before you feed it to your program

...

step 1: write a lexer
step 2: write a parser

Well if its exactly formatted the way you said you can either make a small lexical analyzer, or say fuck it and just make some small routine that only gets the values. I did this once.

Its better to make a function, but it might be simpler to just take the input as a string do stuff with it to clean it up and then feed it to a function to take away dates.