Split a String into Tokens

This is a discussion on Split a String into Tokens within the C++ Programming forums, part of the General Programming Boards category; Don't leave us! Keep pestering us with more questions! That's what we live for, that's what we sit before the ...

  1. #61
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Don't leave us! Keep pestering us with more questions!
    That's what we live for, that's what we sit before the computer for, that's where we get all our posts from!

    Alright, good luck.
    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.

  2. #62
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Did you put the entire if block into the while loop? Is that what you really want to do?

  3. #63
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Thanks for that Elysia.. I am happy to hear that. I will ofcourse keep send you questions. I really appreciate it. Dont worry :P
    I have been sitting with a problem for 20 hours myself searching on the internet and thinking about a solution but cant solve this. I beleive that what I want to do is a bit special, perheps there are functions/syntax to help this problem out.
    Actually it is 2 things/questions that is happening at the same time.
    But I will start out with the first question to see if the first thing could be solved to not make it to difficult.
    .................................................. .................................................. ..........................................
    Here we go
    What I want to do now as I can scan a file and find the lowest value for example "Low" for the entire file. As the example below with 4 lines that contains 2 different dates. I want to store the Lowest Value of Low for each date into a variable. The two variables I have named: "IntraLow9" and "IntraLow8".

    01/12/2006,2005,16.19,16.20,16.19,16.19,2100,16.19
    01/12/2006,2006,16.20,16.20,16.20,16.20,200,16.20
    01/13/2006,2005,16.20,16.20,16.18,16.20,1700,16.18
    01/13/2006,2006,16.18,16.19,16.12,16.19,1500,16.19

    As the code is scanning the file from the top to bottom I have managed to do a code that do a count when a new date occurs.(´Perheps´ this will be of help for the solution)
    The variable is called: "DateCount" and counts down from 11. This meen when the first date happens in the file it will have a count of 9 and for the other date 8.

    So the first key for the first date is 9 then. What I want to do now is to store the lowest value of Low into the variable called: IntraLow9
    I know that my approach: (IntraLow + DateCount) is wrong but perheps you understand what I am ´trying´ to do. (It is kind of "Intelligent" assignment to variables)

    How could it be possible to do something like this ? (Hope you can understand my explanation of the problem )

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    
    using namespace std;
    int main () 
    { 
    	std::string Date;
    	std::string DateDummy;
    	
    	double DateCount = 11;
    	double IntraLow9 = 1000;
    	double IntraLow8 = 1000;
    	char Comma;
    	int Time = 0;
    	double Open = 0;
    	double High = 0;
    	double Low = 0;
    	double Close = 0;
    	int Volume = 0;
    	double IntraHigh = 0;
    	double IntraLow  = 1000;
    	double IntraVolume = 0;
    	ofstream Test;
      
    	Test.open ("file2.txt");
    	ifstream myfile ("CCE.txt");
    	ifstream file0 ("file0.txt");
    
    			
    	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
    		myfile.get();				// read in trailing newline character
    		
    	if ((Low < (IntraLow + DateCount)) && (DateCount != 11) && (Date == DateDummy))     // I am trying to make the code understand 
    	{									                   //	that (IntraLow + DateCount) meens: IntraLow9 where DateCount 
    		(IntraLow + DateCount) = Low;					                  //  for the moment is 9 and then put the lowest value for the 
    	}									                 //  date into double IntraLow9.
    	//////////////////////////////////////////////////////////////////////	
    	if (Date != DateDummy) 
    	{	
    		DateDummy = Date;
    		DateCount = (DateCount - 1);                                         // DateCount has a CountDown for the Dates &#180;while&#180; the code is running. Starting at 11
    	}
    	//////////////////////////////////////////////////////////////////////	
    			
    	}								// End of While Statement
    		Test << IntraLow9 <<"\n";    
    		return 0;	
    
    
    }										// int main braces
    Last edited by Coding; 12-20-2007 at 02:44 PM.

  4. #64
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You probably need to learn arrays for this. I would actually use a map and map the date string to the current low value. If the times will always be greater than 0 then this is really easy once you learn how to use the map.

    I'd also consider starting a new thread, since this one will be hard to follow for anybody who hasn't been doing so already.

  5. #65
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Coding View Post
    Here we go
    What I want to do now as I can scan a file and find the lowest value for example "Low" for the entire file. As the example below with 4 lines that contains 2 different dates. I want to store the Lowest Value of Low for each date into a variable. The two variables I have named: "IntraLow9" and "IntraLow8".
    [snip]
    So the first key for the first date is 9 then. What I want to do now is to store the lowest value of Low into the variable called: IntraLow9
    I know that my approach: (IntraLow + DateCount) is wrong but perheps you understand what I am ´trying´ to do. (It is kind of "Intelligent" assignment to variables)

    How could it be possible to do something like this ? (Hope you can understand my explanation of the problem )
    The quick'n'dirty way to do this would be arrays, I would think. Use IntraLow[DateCount] instead.

    I'm sure the STL has many forms of associative arrays, and someone who actually knows the STL will be along to tell you; ten seconds of Google says that std::map might be something to look at.

  6. #66
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Quote Originally Posted by Coding View Post
    Code:
    		if ((Low < (IntraLow + DateCount)) && (DateCount != 11) && (Date == DateDummy))     // I am trying to make the code understand 
    	{									                   //	that (IntraLow + DateCount) meens: IntraLow9 where DateCount 
    		(IntraLow + DateCount) = Low;					                  //  for the moment is 9 and then put the lowest value for the 
    	}									                 //  date into double IntraLow9.
    	//////////////////////////////////////////////////////////////////////	
    		if (Date != DateDummy) 
    	{	
    		DateDummy = Date;
    		DateCount = (DateCount - 1);                                         // DateCount has a CountDown for the Dates &#180;while&#180; the code is running. Starting at 11
    	}
    	//////////////////////////////////////////////////////////////////////	
    			
    	}								// End of While Statement
    I'm not sure I understand your problem. Perhaps I simply need to re-read it, but even so, this is something I want to comment on. This is bad what you're doing.
    You're actually putting the { in a indentation LESS than the if. { and } should always be on the same level as your block element, such as if, while, etc.
    So indent those { and }, along with all the code inside.

    Should look like this:
    Code:
    		if ((Low < (IntraLow + DateCount)) && (DateCount != 11) && (Date == DateDummy))     // I am trying to make the code understand 
    		{									                   //	that (IntraLow + DateCount) meens: IntraLow9 where DateCount 
    			(IntraLow + DateCount) = Low;					                  //  for the moment is 9 and then put the lowest value for the 
    		}									                 //  date into double IntraLow9.
    
    		if (Date != DateDummy) 
    		{	
    			DateDummy = Date;
    			DateCount = (DateCount - 1);                                         // DateCount has a CountDown for the Dates &#180;while&#180; the code is running. Starting at 11
    		}
    	}								// End of While Statement
    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. #67
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Thanks.. I will read about arrays and maps through internet and also start a new thread later on...
    Coding

  8. #68
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    There are excellent tutorials for that on this site. I think.
    Anyway, check them out and see if they help.
    std::vector and std::map are those you might want to take a look at.
    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. #69
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Ok, thanks Elysia, I will take a look at them too..

Page 5 of 5 FirstFirst 12345
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21