Can anyone explain how to store a string as a variable?

In summary, the conversation discusses using strings instead of letters in a calculator program for neater output. The person wants to prompt for an entire string instead of individual letters and save them as variables. They are having trouble with a syntax error and need help.
  • #1
breebreebran
12
0
I don't have any big programs. I just write programs to get me through my math classes.
But I want to have the output look pretty.
Like one program I have is for my finance class.

Code:
prompt p
if p = 1
then
Prompt b,r,n,t
r/100→x
b/(1+x/n)^(n*t)→l
disp l
else
Prompt b,r,n,t  
r/100 → x  
b*(x/n)/((1+x/n)^(n*t)-1) → m  
disp m

But what I want to do is instead of prompting p, I want to prompt "present value?"
Then If present value = 1 then etc.
It just looks neater if it prompts an entire string instead of one letter.
So instead of prompting b,r,n,t it'll prompt
"Ending Balance?"
"Rate?"
"Number of compounds?"
"Time?"
And then I can save those strings as variables.
So ending balance → b
Rate → r
Number of compounds → n
And then just have the rest of the program work with those letters instead of having to type out (rate/numberofcompounds)
Every time I try to use strings instead of letters it always gives me a syntax error.
Any help?
 
Computer science news on Phys.org
  • #3
breebreebran, you didn't say what kind of calculator you have, which would be useful information. It's possible that the prompt command has an optional first parameter of a string, like this:
prompt "Present value? ", p

If not, do as jedishrfu suggests, and print the string before executing the prompt.
 

1. How do you store a string as a variable in programming?

To store a string as a variable in programming, you first need to declare the variable and assign a name to it. Then, use the assignment operator (=) to assign the string value to the variable. For example, in JavaScript: var name = "John"; Here, the variable name is declared and assigned the string value "John".

2. What is the purpose of storing a string as a variable?

Storing a string as a variable allows you to easily reference and manipulate the string throughout your code. Instead of typing out the string every time you need it, you can simply use the variable name. This can also make your code more readable and organized.

3. Can a string be stored as a variable in any programming language?

Yes, strings can be stored as variables in most programming languages. However, the syntax and methods for declaring and assigning variables may vary between languages.

4. How can I change the value of a string variable?

To change the value of a string variable, you can simply reassign it using the assignment operator (=). For example, in Python: name = "John" name = "Sarah" Here, the variable name is initially assigned the string value "John", but then is reassigned the string value "Sarah".

5. Is it possible to store multiple strings as one variable?

Yes, it is possible to store multiple strings as one variable. This can be done using data structures such as arrays or lists, which allow you to store multiple values in one variable. For example, in Java: String[] names = {"John", "Sarah", "David"}; Here, the variable names is an array that stores the strings "John", "Sarah", and "David".

Similar threads

  • Computing and Technology
Replies
2
Views
10K
Replies
12
Views
1K
Replies
5
Views
375
  • Linear and Abstract Algebra
Replies
7
Views
1K
  • Differential Equations
Replies
7
Views
387
  • Introductory Physics Homework Help
Replies
9
Views
1K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
1
Views
871
  • Set Theory, Logic, Probability, Statistics
Replies
0
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
6
Views
675
Back
Top