Info
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Program that can take three lines input by the user running the code, and saves those lines of text in a text file
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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!"
0 commentaires
Réponses (1)
Bob Thompson
le 2 Avr 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);
0 commentaires
Cette question est clôturée.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!