looping problems

This is a discussion on looping problems within the C++ Programming forums, part of the General Programming Boards category; hi guys i am having a problem with the following bit of code - i am quite new to programming. ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    3

    looping problems

    hi guys

    i am having a problem with the following bit of code - i am quite new to programming. It is supposed to read a file line by line and compare the resultant string against another string that has been imputed by the user. (the end of file bit has been taken out cos it seems to screw it up even futher)
    Code:
    //while (! vehicles.eof() )
    	//{
        do 
    	{
    		vehicles.getline(buffer,9); 
    		if (!strcmp(buffer,index)) cout << "Vehicle on hire\n";
    		else cout << "Vehicle not on hire\n";
    	}
    	while (strcmp (buffer,index) !=0);
    		//if (!strcmp(buffer,index)) cout << "Vehicle on hire\n";
    		//else cout << "Vehicle not on hire\n";	
    		//vehicles.seekg(pos);
    	//}
    basically if i leave string comparison and output in the do section it will return "Vehicle not on hire" for every string bar the one that matches (where it outputs "Vehicle on hire") but will just output "Vehicle not on hire" in a continuous loop if no strings match. if however, i leave the code in the while section it will output "Vehicle on hire" for a matching (string which is better) but will just lock up if you input a string that is not on the text file. this seems to happen no matter what i put in the while section and if i try it using an if else statement.

    any ideas what i am doing wrong?

    thanks in advance

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    What are buffer and index?

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    3
    Originally posted by ygfperson
    What are buffer and index?
    index is a char array that is taken from the user and buffer is a line taken from a text file (also a char array)thay are decalred earlier in the code (not posted)

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Here is one solution.

    Code:
    std::string sInput;
    ostream file;
    file.open(...);
    
    while (std::getline(file, sInput))
    {
       if (strcmp(sInput.c_str(), index) == 0)
          cout << "Vehicle on hire\n";
    
       else
          cout << "Vehicle not on hire\n";
    }
    Kuphryn

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    3
    thank you very much - i will give that a go...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 07:03 AM
  3. looping and condition problems
    By liukinhei in forum C Programming
    Replies: 1
    Last Post: 03-11-2008, 03:40 AM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 01:17 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM

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