Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

Program that can take three lines input by the user running the code, and saves those lines of text in a text file

1 view (last 30 days)
Would someone be able to assist me in creating a rather simple program? It needs to take the user inputs of the following three lines from their input and then save it to a file t3.txt.
"Get started."
"Come on!"
"Way to go!"

Answers (1)

Bob Thompson
Bob Thompson on 2 Apr 2019
There are a couple of different ways of doing this, mostly based on how much you trust the user to put things in correctly.
The first option is to use multiple user inputs, one for each line. You can even make this variable so the user can put in more than three lines if you would like.
nl = input('How many lines would you like to input? ');
str = '';
for i = 1:nl;
tmp = input('Please enter a line: ','s'); % Double check that 's' is the appropriate flag
str = [str,'\n\n',tmp];
end
fout = fopen('t3.txt','w');
fprintf(fout,str);
fclose(fout);
Alternatively, you can simply have the user input their own new line characters and then the string can be entered as a single input.
str = ('Please input all lines: ','s');
fout = fopen('t3.txt','w');
fprintf(fout,str);
fclose(fout);

This question is closed.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!