Thread: While statement with getline and strcmp to break

  1. #1
    Registered User
    Join Date
    Jul 2011

    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 , "@@@"))

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

    If i change the statement to

    if(strlen(input_text) == 0)

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

    Code below

    #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);
    		cout << "Invalid file" << endl;
    		return -1;
    	cout << "You are now editing the document" << endl;
    		cout << "Please type <@@@> to exit";
    		cin.getline(input_text, 199);
    		if(strcmp(input_text, "@@@"))
    			fout << input_text << endl;

  2. #2
    Registered User
    Join Date
    Jun 2005
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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
    ofstream fout(file.c_str());
    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.

  4. #4
    Registered User
    Join Date
    Jul 2011
    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, 06: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, 02: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