terminating a script with a quit comand

2 views (last 30 days)
I have been told to make a script that will evaluate a string, and at the end it must ask the user if they are sure they want to quit if yes then quit if no then rerun the switch.
this is an example of what i need it to look like for choice 7:
Please choose an option:
1. Length
2. First letter
3. Last letter
4. Reverse
5. All
6. Enter a new string
7. Quit
Choice: 7
Are you sure you would like to quit (Yes/No)? n
Please choose an option:
1. Length
2. First letter
3. Last letter
4. Reverse
5. All
6. Enter a new string
7. Quit
Choice: 7
Are you sure you would like to quit (Yes/No)? y
Quitting program
>>
my code looks like:
clear; clc; close all;
choice = input('please give a "string"(PHRASE) to be evaluated: ', 's');
%this is a function that i created that evaluates the string.
[length firstLetter lastLetter reverse all] = singletary6(choice);
%function [stringLength, firstLetter, lastLetter, reverse, all] = singletary6(str)
%if isempty(str)
% stringLength = 0;
% firstLetter = '';
% lastLetter = '';
% reverse = '';
% all = '';
%else ~isempty(str);
% stringLength = length(str);
% firstLetter = str(1);
% lastLetter = str(length(str));
% reverse = str(end:-1:1);
% all = str;
%end
%end
disp(choice)
menu = sprintf('1. length\n2. First letter\n3. Last Letter\n4. Reverse\n5. All\n6. Enter a new String\n7. Quit\n');
fprintf('1. length\n2. First letter\n3. Last Letter\n4. Reverse\n5. All\n6. Enter a new String\n7. Quit\n');
choose = input('Choose a comand from 1-7:');
while choose <= 7
switch choose
case 1
length
disp(menu)
choose = input('Choose a comand from 1-7:');
case 2
firstLetter
disp(menu)
choose = input('Choose a comand from 1-7:');
case 3
lastLetter
disp(menu)
choose = input('Choose a comand from 1-7:');
case 4
reverse
disp(menu)
choose = input('Choose a comand from 1-7:');
case 5
all
disp(menu)
choose = input('Choose a comand from 1-7:');
case 6
choice = input('Enter a new String(phrase)', 's');
[length firstLetter lastLetter reverse all] = singletary6(choice);
fprintf('\n')
disp(menu)
choose = input('Choose a comand from 1-7:');
fprintf('\n')
case 7
disp('Quit')
button = sprintf('Ready to quit?\nYes\nNo\n');
switch button
case 'Yes',
disp('Exiting MATLAB');
%Save variables to matlab.mat
save
case 'No',
quit cancel;
end
end
end
%i know there is a lot of stuff here but wanted to give as much as possible to help with an answer.
  1 Comment
SonOfAFather
SonOfAFather on 4 Oct 2012
while i have continued to work this i have found that case 7 which now looks like this:
case 7
disp('Quit')
terminate = input('Are you sure you want to quit(yes/no)', 's');
switch terminate
case 'no'
disp(menu)
choose = input('Choose a comand from 1-7:');
case 'yes'
terminate = 0;
end
no works, but yes is greating a restatement of case 7. any ideas?

Sign in to comment.

Accepted Answer

Matt Fig
Matt Fig on 4 Oct 2012
Edited: Matt Fig on 4 Oct 2012
Since the SWITCH is in the loop, why not just set choose to 10 if the user wants to quit? Then the loop condition will fail and the program will cease...
case 7
disp('Quit??')
terminate = input('Are you sure?(yes/no)', 's');
if strcmp(terminate,'no')
disp(menu)
choose = input('Choose a comand from 1-7:');
else
choose = 10; % Now the WHILE condition will fail.
disp('bye-bye!')
end
end

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!