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");
}