Thread: Split a String into Tokens

  1. #31
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Daved,

    That worked out really great... thanks for that.
    Now when I am trying to do an if-statement like this, I have no compile success, though:
    If Close == 35.23, where 35.23 is an integer. What could this depend on:

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    
    using namespace std;
    int main () 
    
    { 
    	std::string Date;
    	int Time = 0;
    	char Comma;
    	double Close = 0;
      ofstream Test;
      Test.open ("file2.txt");
      ifstream myfile ("file1.txt");
      ifstream file0 ("file0.txt");
    getline(myfile, Date, ',');
    myfile >> Time;		
    myfile >> Comma;   //char Comma;
    myfile >> Close;
    
    If (Close == 35.23)                               
    
    {  
    
      Test << Close <<"\n";    
      return 0;
    }
    }

  2. #32
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I have no compile success, though
    What compile error did you get?

    Did you use If or if? Case matters in C++, make sure you spelled it as lower-case if.

  3. #33
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes you solved my problem again... ofcourse I did it with a upper-case like this: &#180;If&#180;
    So the &#180;if&#180; do work now...
    Thanks again Daved...

  4. #34
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I propose you also learn proper indenting. Each block should be indented once.
    You can also try to group your code into blocks - not necessary, but it also makes the code more readable.
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    
    using namespace std;
    int main() 
    { 
    	std::string Date;
    	int Time = 0;
    	char Comma;
    	double Close = 0;
    	ofstream Test;
    
    	Test.open("file2.txt");
    	ifstream myfile("file1.txt");
    	ifstream file0("file0.txt");
    	getline(myfile, Date, ',');
    
    	myfile >> Time;		
    	myfile >> Comma;   //char Comma;
    	myfile >> Close;
    
    	If (Close == 35.23)                               
    	{  
    		Test << Close <<"\n";    
    		return 0;
    	}
    }
    Correctly indented code. And don't mix spaces and tabs; it usually causes headaches for other editors (and the boards as well). Use only tabs or spaces. I recommend tabs.
    Last edited by Elysia; 12-18-2007 at 04:38 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #35
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Thanks for the tip Elysia. I will consider this when writing and post code !
    I have come up with a new problem that I am trying to solve. I beleive I am quite near the solution I hope.
    Before I had one row in a file(The first line/row below) that I was splitting into tokens and putting into variables.

    12/04/2007,2111,35.23,35.23,35.20,35.22,15600,35.22
    12/04/2007,2112,35.23,35.28,35.20,35.22,15600,35.22
    12/04/2007,2113,35.23,40.00,35.20,35.22,15600,35.22

    My next project below is for the Timevariable wich is for the rows: 2111,2112,2113 to make a new variable that finds the &#180;Highest Value&#180; of these and putting this to the Variable I have called: int HighestTim.
    I beleive some new functions comes with this and I have tried to combine a whileloop with ! eof as could be seen in my code.
    The code doesn&#180;t work and I cant find what could be wrong. Am I on the right track.
    (My code do compile success but doesn&#180;t write the variable: HighestTim to the file: Test)

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    
    using namespace std;
    
    int main () 
    
    { 
    	std::string Date;
    	int Time = 0;
    	//
    	int HighestTim = 0;
            ofstream Test;
    	Test.open ("file2.txt");
    	ifstream myfile ("file1.txt");
    	ifstream file0 ("file0.txt");
    
    	getline(myfile, Date, ','); // 12/04/2007
    	myfile >> Time;		        // 2111
    
    while ((! myfile.eof()) & (Date == "12/04/2007") & (Time > Time))  
    { 
    HighestTim = Time; }
    
    if (HighestTim > 2111)
    {
    Test << "Test " << HighestTim <<"\n";}
    
    return 0;}
    Last edited by Coding; 12-18-2007 at 01:30 PM.

  6. #36
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Here we go again... Correctly indenting code:

    Quote Originally Posted by Coding View Post
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    
    using namespace std;
    
    int main () 
    
    { 
    	std::string Date;
    	int Time = 0;
    	//
    	int HigherTim = 0;
            ofstream Test;
    	Test.open ("file2.txt");
    	ifstream myfile ("file1.txt");
    	ifstream file0 ("file0.txt");
    
    	getline(myfile, Date, ','); // 12/04/2007
    	myfile >> Time;		        // 2111
    
    	while ((! myfile.eof()) & (Date == "12/04/2007") & (Time > Time))  
    	{ 
    		HigherTim = Time;
    	}
    	if (HigherTim > 2111)
    	{
    		Test << "Test " << HigherTim <<"\n";
    	}
    	return 0;
    }
    Also note that & will not do what you think it will. It's a binary OR operator, not the AND operator. That's &&. Two & after each other.
    And I generally do not recommend such complex loop conditions, because it's hard to read and easy to make mistakes.
    Instead, what I do, is make an infinite for loop and check conditions in the loop manually and then break if if true. And using eof() as a loop condition is generally bad; you should read the FAQ about that:

    Code:
    for(;;)
    {
    	if (condition) break;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #37
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The new loop is a good idea. But you shouldn't use eof() to control the loop. Instead, check the return value of the read operation to make sure it succeeded.

    In your case, you want the getline and the myFile >> Time to be part of the loop because you want them to be repeated for each line, right? So you have to move them to the loop. I would put the very first read (in this case getline(myfile, Date, ',')) inside the while control like this:
    Code:
    while (getline(myfile, Date, ','))
    That will allow the while loop to break when there are no more Dates in the file that can be read in.

    You also need to code to read in the rest of each line and ignore it. How you do that is up to you, but one way is to call getline and read into a dummy variable with '\n' as the delimiter. That will read in and ignore the rest of the line. If you need to read in and use the data in the rest of the line obviously you wouldn't want to do that.
    Last edited by Daved; 12-18-2007 at 01:36 PM. Reason: fix typo - thanks Elysia

  8. #38
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Daved View Post
    Code:
    while getline(myfile, Date, ','))
    Code:
    while (getline(myfile, Date, ','))

    You forgot a parenthesis.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #39
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    This got me a bit confused, I understand the logic you are describing to move the getline and >> inside the loop to repeat for the lines that comes under.
    I suppose both the &#180;Time&#180;and &#180;Date&#180;should be put into the same while loop.
    But from there I am stuck. I cant really understand how the code will be build enterily.
    I have tried out an example below but something might be wrong with it.

    (I have removed the getline and >> and instead put it inside the while loop. I suppose it only should be inside the loop.)
    Code:
    	while  (getline(myfile, Date, ',')    myfile >> Time)              		
    		{
    			if Time > Time
    		{
    			HighestTim = Time;
    		}}
    
    			
    	if	(HighestTim > 2111)
    		{
    			Test << "Test " << HighestTim <<"\n";    
    		}
    			return 0;
    		}
    Last edited by Coding; 12-18-2007 at 02:35 PM.

  10. #40
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Coding, have you taken a second look at all the code you post?
    All are very hard or confusing to read because you're misusing indentation and spaces.
    Try to reform that last code you posted into something more readable.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #41
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    This is kind of a confusing area. Ideally, you should have the getline and myfile >> Time calls inside the loop itself (inside the curly braces, not the parentheses).

    However, you need some method of breaking the loop when no more input is found.

    My suggestion was to put only the getline part inside the parentheses. It will still get called each time, but it will cause the loop to break when there is no more input.

    It works better than the eof() loop you had originally because eof() isn't always true when the file has no more data in it.

    So my suggestion is to put the getline inside the while control (inside the parentheses like in my example), and then put the myfile >> Time part and the rest of the code inside the loop itself (inside the curly braces).

    Once you do that, you'll have to fix up the rest of the code inside that loop. I would try removing everything else and just get the reading part to work. You'll also have to add code to read the rest of the line like I mentioned before.

  12. #42
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Now I have understand the logic of how the getline and where myfile >> should be placed wich also will break when there is no more input.

    I have also completed the input for the lines I will read in. The thing is that I need every single value on the line as I have coded below, so I don&#180;t want to ignore any of these data.
    So the code look like this for the while loop. Still there is something I dont understand.
    Perheps I should need some code assistance in order to understand what is happening...
    Thanks..

    Code:
             while	(getline(myfile, Date, ','))           		
    		{
    			myfile >> Time;		                // 2111
    			myfile >> Comma;                   
    			myfile >> Open;				// 35.23
    			myfile >> Comma;			
    			myfile >> High;				// 35.23
    			myfile >> Comma;			
    			myfile >> Low;				// 35.20
    			myfile >> Comma;			
    			myfile >> Close;			// 35.22
    			myfile >> Comma;
    			myfile >> Volume;			// 15600
    			myfile >> Comma;			
    			myfile >> Close;			// 35.22
    
    	if Time > Time
    		{ 
    			HighestTim = Time;
    		
    			Test << "Test " << HighestTim <<"\n";    
    		
    			return 0;
    		}}
    Last edited by Coding; 12-18-2007 at 03:20 PM.

  13. #43
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your reading code looks good. There is only one minor problem that you might have left which is that some of the Date fields might have an extra newline character at the start. If you notice this, then add myfile.get(); like this:
    Code:
    myfile >> Close;			// 35.22
    myfile.get();			// read in trailing newline character
    Now, what is it that you don't understand? Your if statement has incorrect syntax. I assume you're trying to remember which is the highest time value. If so, then you have the right idea, except you have to fix your if statement. You have code in there that should be after the while loop.

  14. #44
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Here I am again.. sorry Daved... I beleive I am close to the solution but dont understand why I have no compile success. The code looks as below. I have put the if statement after the loop as you said.
    I did a mistake for the if statement, it should ofcourse be: if Time > HighestTim...

    When I run the code I have these errors and all variables are declared. Perheps there is any detail missing still.
    .\Test.cpp(45) : error C2061: syntax error : identifier 'Time'
    .\Test.cpp(46) : error C2143: syntax error : missing ';' before '{'
    Code:
                            myfile >> Volume;			// 15600
    			myfile >> Comma;			
    			myfile >> Close;			// 35.22
    			myfile.get();			         // read in trailing newline character
    		
    		}                                                // while loop braces
    
    	if Time > HighestTim
    		{ 
    			HighestTim = Time;
    	
    			Test << "Test " << HighestTim <<"\n";    
    		
    			return 0;
    		}
    		}					         // int main braces
    Last edited by Coding; 12-18-2007 at 04:02 PM.

  15. #45
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You are missing parentheses in the if statement. You really gotta work on the basics, here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM