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; Code: if ( condition ) { code; } Is it just me or are you COMPLETELY ignoring my suggestions about ...

  1. #46
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Code:
    if (condition)
    {
    	code;
    }
    Is it just me or are you COMPLETELY ignoring my suggestions about fixing your code mess?
    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. #47
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Yes, you are right about that.. i forgot it... but still the function to find the "Highest Time Value" doesn´t work...
    I do have compile success now but with the code above I have no output to the file: Test

  3. #48
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Look at the code inside the if block. Should that code really be there?

    Also, it's hard to find exact problems without the latest, complete code. Maybe you can fix up all your indentations and post the latest complete code so we can have a better picture of what is going on.

  4. #49
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Elyisa, I am defenetively not ignoring your suggestions. Remember I am a quite of a beginner with this compared to you... my code doesnt look like a mess that you said.. but not perfect though...
    But I am trying better and better... The tabs doesn´t really work out 100 % for me why the code could look a bit like that... but the important is that I am trying.. please have patience...
    Last edited by Coding; 12-18-2007 at 03:13 PM.

  5. #50
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    I understand, but indentation is not a difficult thing and from what I can see, all posts look the same.
    Try to fix up the code mess into something more readable. It will be better for yourself and for others when they read your code too.
    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.

  6. #51
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Thanks Elysia...I will do that...trust me I am trying. Many things to think of in the begining

  7. #52
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you should really un-indent your if-line to the same level as the while above it, as it's at the same level. And look at the code inside the if-statement - what does it actually do?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #53
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    This is my complete code (I hope this is more readable, good for me to know if it´s not. Thanks!)
    I think the code inside the if statement (if (TIme > HighestTim) should be there as if it finds a higher value
    of the time that is "saved" or by default in the variable: HighestTim. Then the current Time that is red will replace this value with this higher TimeValue ?

    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 Open = 0;
    	double High = 0;
    	double Low = 0;
    	double Close = 0;
    	int Volume = 0;
    	int HighestTim = 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 (Time > HighestTim)
    	{ 
    		HighestTim = Time;
    		Test << "Test " << HighestTim <<"\n";    
    		return 0;	
    	}
    }										// int main braces
    Last edited by Coding; 12-18-2007 at 03:36 PM.

  9. #54
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Yep, much better!
    You probably don't need to indent so much, though, because it will make your code zillioningly long when you have a lot of blocks! Just one tab or 4 spaces are usually enough.
    You also missed indenting inside that while.

    Otherwise it looks very good!
    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.

  10. #55
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Thanks Elysia. I beleive you meen like that inside of the while. If it is... Then I should know now how it should be done

  11. #56
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    You are doing two different things inside your if block. One, you are checking to see if the Time is greater than the current highest value. Two, you are printing out the highest time that was found. But checking the current time to see if it is the highest is something that should be done while you are reading in the times, right? And printing out the highest should only be done at the end after all times have been read in, right?

  12. #57
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Quote Originally Posted by Coding View Post
    Thanks Elysia. I beleive you meen like that inside of the while. If it is... Then I should know now how it should be done
    That's the ticket! Now it looks very nice!
    Now you just have to hunt down those bugs in the code
    Also note that main should always return a value and so by putting it inside that if block, it will only return something if the condition is true. Not a good thing, is it? If that condition is false, then main won't return (an explicit) value. You should also get a warning (at least in C++) saying that not all control paths return a value. Move that return outside the if. I don't think you actually was intending to return 0 only if the condition was true, did you?
    Last edited by Elysia; 12-18-2007 at 03:43 PM.
    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.

  13. #58
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> You should also get a warning (at least in C++) saying that not all control paths return a value.
    Technically you should not get a warning if you don't include a return value at the end of main. There is a rule in C++ that allows you to leave out the return statement and the compiler will implement a return 0 automatically.

    Of course, using a return value inside the if and not after it is confusing even if it is technically correct, so the advice is still appropriate.

  14. #59
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Yes, but also, typically, if you return something within one possible path of the function and not another, you should also get a warning. Though I don't know if that applies to main since it's allowed to not explicitly return something.
    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.

  15. #60
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Thats right Elysia.. True, I will put return outside the if statement.

    And it&#180;s right about the if condition it should ofcourse be inside the while loop as it in realtime searching for the greatest timevalue.
    Now it works and I appreciate all help that I got. I will now experiment and learn all the logics with the code, and the indentations.. Take care...
    Last edited by Coding; 12-18-2007 at 03:54 PM.

Page 4 of 5 FirstFirst 12345 LastLast
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, 01:11 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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