Like Tree2Likes
  • 1 Post By grumpy
  • 1 Post By Salem

While statement with getline and strcmp to break

This is a discussion on While statement with getline and strcmp to break within the C++ Programming forums, part of the General Programming Boards category; Hey All, Can someone please have a look at my code below. its the while statement in particular that i'm ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    22

    While statement with getline and strcmp to break

    Hey All,

    Can someone please have a look at my code below. its the while statement in particular that i'm a little confused about. i've used strcmp to compare the text retrieved from getline to break out the while loop.

    if(strcmp(input_text , "@@@"))
    break;

    However, regardless of what i type it breaks out of the while loop.

    If i change the statement to

    if(strlen(input_text) == 0)
    break;

    It works a treat and breaks out when i dont type anything.

    Code below

    Code:
    #include "file.h"
    using namespace std;
    #define MAX_PATH 99
    int main()
    {
    	char file_path[99];
    	char file_name[99];
    	char file_extension[9] = ".txt";
    	char file[99];
    	char input_text[199];
    
    
    
    	cout << "Where would you like to store your file: ";
    	cin.getline(file_path, MAX_PATH);
    	cout << "What would you like to call the file: ";
    	cin.getline(file_name, MAX_PATH);
    
    	strcpy(file, file_path);
    	strcat(file, file_name);
    	strcat(file, file_extension);
    
    	ofstream fout(file);
    
    	if(!fout)
    	{
    		cout << "Invalid file" << endl;
    		return -1;
    	}
    	cout << "You are now editing the document" << endl;
    	while(true)
    	{
    		cout << "Please type <@@@> to exit";
    		cin.getline(input_text, 199);
    		if(strcmp(input_text, "@@@"))
    			break;
    		else
    			fout << input_text << endl;
    	}
    	system("pause");
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,195
    strcmp() returns zero if the strings compare equal. Your code is breaking out if they are not equal.

    It would probably be a good idea to check the state of cin, in case an error occurs.

    Also, it would be a good idea if the length of file was > 99: strcat()ing strings of length 99 potentially yields a string with length more than 99. There are generally also delimiters in paths (for examples, slashes) that your code should enter if the user does not.
    Last edited by grumpy; 07-08-2011 at 06:35 AM.
    Freem likes this.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Perhaps you should consider using std::string for all your input. It would avoid all the buffer overflow issues you have at the moment.

    Then at the point you NEED a char array of some sort, you do this
    Code:
    ofstream fout(file.c_str());
    Elysia likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    22
    Yeah, i much prefer std::string to c-string. But the book i'm following is being persistent with the old c style strings. So i'll stick with it for the moment, i think.

    Grumpy, some good tips. thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem w/break statement
    By Babs21 in forum C Programming
    Replies: 1
    Last Post: 11-10-2007, 05:50 PM
  2. Break Up Getline
    By pandadc in forum C++ Programming
    Replies: 8
    Last Post: 09-13-2006, 12:15 PM
  3. Break in If statement
    By plain in forum C++ Programming
    Replies: 10
    Last Post: 09-07-2006, 01:53 PM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 01:33 PM
  5. break statement
    By Motisa in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2002, 04:52 PM

Tags for this Thread


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