i will explain again, my program will create a file in the desktop called test.txt so it will create it, and will tell me what text i want inside so when i type the text is perfect but if i press enter, it just close, but i want it still open even if i press ENTER, i know and i saw many examples with the library <string>, but i wanted to know how can it be possible with <cstring> library, that's why i wrote a while loop thinking on if you writte or press @@@ it would close, but it doesn't it close pressing ENTER
#include <iostream>
#include <fstream>
#include <cstring>
usingnamespace std;
#define MAX 1000
int main(){
char writting[MAX + 1];
char location[MAX + 1];
char filename[MAX + 1];
char pathname[MAX + 1];
cout << "Wich location would you like to create you file?: ";
cin.getline(location, MAX);
cout << "Now choose the name of your file: ";
cin.getline(filename, MAX);
strcpy(pathname, "c:\\users\\username\\");
strcat(pathname, location);
strcat(pathname, "\\");
strcat(pathname, filename);
strcat(pathname, ".txt");
ofstream filee(pathname);
if(!filee){
cout << pathname << " was not found. Thus, it was not opened.";
return -1;
}
cout << "Now enter the text you would like to write (ENTER @@@ to exit): ";
do{
cin.getline(writting, MAX);
//the write way to do it, was to compare to 0.
if (strcmp(writting, "@@@")==0)
break;
strcat(writting, "\n");
filee << writting;
}while(!strlen(writting)==0);
filee.close();
return 0;
}
You could use std::getline(), and simply test the string to see if the string is empty or only contains a newline character. Here is an example, though there are many other ways, some just as good or better:
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> strvec; // to store the strings
// infinite loop.
for (;;) {
std::string tempstr;
std::getline(tempstr);
// If there is no contents in the string
if (tempstr.empty() || tempstr[0] == '\n')
break; // end the loop
// Otherwise, add it to the vector
strvec.push_back(tempstr);
}
// do something with the strings, here I'll just loop and print them out
for (const std::string& s : strvec)
std::cout << s << '\n';
return 0;
}
NT3 I haven't done vector yet, but I wanna use multiple lines on a program that creates a file, here is my code and please tell me what I am doing wrong.
#include <iostream>
#include <fstream>
#include <cstring>
usingnamespace std;
#define MAX 1000
int main(){
char writting[MAX + 1];
char location[MAX + 1];
char filename[MAX + 1];
char pathname[MAX + 1];
cout << "Wich location would you like to create you file?: ";
cin.getline(location, MAX);
cout << "Now choose the name of your file: ";
cin.getline(filename, MAX);
strcpy(pathname, "c:\\users\\username\\");
strcat(pathname, location);
strcat(pathname, "\\");
strcat(pathname, filename);
strcat(pathname, ".txt");
cout << "Now enter the text you would like to write (ENTER @@@ to exit): ";
//this loop is supposed to make multi line
//if I press enter it shouldn't exit but it does D:
while(true){
cin.getline(writting, MAX);
if(strcmp(writting, "@@@"))
break;
}
//creates the text file on the path name we indicated
ofstream filee(pathname);
if(!filee){
cout << pathname << " was not found. Thus, it was not opened.";
return -1;
}
filee << writting;
filee.close();
return 0;
}
when I press enter to my text, it closes the program, why? my while loop is suppose to be working until I write @@@. help me out please
Returns an integral value indicating the relationship between the strings:
A zero value indicates that both strings are equal.
A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.
but the problem is not that, the problem is that it doesn't let me to press enter to make another line of text, it just break the while loop after entering the first line, why?
Dude, it is exactly what i'm saying. The only possible way to stop the "while" loop is what is inside the "if" scope, which is "break". So, what can we conclude of that? YOUR way of use IS wrong.
This CODE will be true if "strcmp" returns 0,otherwise the words AREN'T EQUAL.
1 2
if(strcmp(writting, "@@@") == 0)
break;
If i understand right, you are trying to get what the user types and then will write down into a file. Right now, your code will only take the latest input(in this case, always, "@@@"). Of course,that will only happen after you fix what i've said.