how to write a program combininb name and age?

6 views (last 30 days)
lior
lior on 13 Aug 2024
Commented: Steven Lord on 13 Aug 2024
In my HB I got an assigment on writing a program that asks for the name and age of a person, then runs the sentence Dear__your age is___, the blank spaces containing the input
I thought about using a switch, the cases being the name inputs, is that something I can do or is there an easier way?

Answers (1)

Fangjun Jiang
Fangjun Jiang on 13 Aug 2024
Moved: Fangjun Jiang on 13 Aug 2024
No Switch statement is needed. This practice is to get the input and put it in the right format.
help input
INPUT Prompt for user input. RESULT = INPUT(PROMPT) displays the PROMPT string on the screen, waits for input from the keyboard, evaluates any expressions in the input, and returns the value in RESULT. To evaluate expressions, INPUT accesses variables in the current workspace. If you press the return key without entering anything, INPUT returns an empty matrix. STR = INPUT(PROMPT,'s') returns the entered text without evaluating expressions. To create a prompt that spans several lines, use '\n' to indicate each new line. To include a backslash ('\') in the prompt, use '\\'. Example: reply = input('Do you want more? Y/N [Y]:','s'); if isempty(reply) reply = 'Y'; end See also KEYBOARD. Documentation for input doc input
help sprintf
sprintf - Format data into string or character vector This MATLAB function formats the data in arrays A1,...,An using the formatting operators specified by formatSpec and returns the resulting text in str. Syntax str = sprintf(formatSpec,A1,...,An) [str,errmsg] = sprintf(formatSpec,A1,...,An) str = sprintf(literalText) Input Arguments formatSpec - Format of output fields formatting operators A1,...,An - Numeric, character, or string arrays arrays literalText - Input text without formatting operators character vector | string scalar Output Arguments str - Formatted text character vector | string scalar errmsg - Error message character vector Examples openExample('matlab/FloatingPointFormatsExample') openExample('matlab/LiteralTextandArrayInputsExample') openExample('matlab/SpecifyFormattedTextAsStringArrayExample') openExample('matlab/IntegerFormatwithFloatingPointInputsExample') openExample('matlab/SpecifyFieldWidthofaPrintedValueExample') openExample('matlab/ReorderInputsUsingPositionIdentifiernExample') openExample('matlab/CreateaStringfromValuesinCellArrayExample') See also char, fprintf, int2str, num2str, sscanf, fscanf, compose, string Introduced in MATLAB before R2006a Documentation for sprintf doc sprintf Other uses of sprintf codistributed/sprintf dlarray/sprintf gpuArray/sprintf matlab.automation.internal.diagnostics.FormattableString/sprintf
  1 Comment
Steven Lord
Steven Lord on 13 Aug 2024
Another approach could be to build up a string that includes your data. This documentation page shows two ways to do that.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!