String

Hello!

Why would I write

1
2
  movieTitle = "Wheels of Fury";
  cout << "My favorite movie is  " << movieTitle << endl;


Instead of writing

 
  cout << "My favorite movie is Wheels of Fury";


Can you give me an example of when each would be used and for what...

Thank you!
Hi,

Well this is central to the whole idea of programming. Without variables, we couldn't do any meaningful programming.

I think you question comes about because you have assigned a constant value to the variable, so you don't see the point of having a variable at all. The usefulness of a variable comes from it perhaps NOT containing a constant value, in other words a value that can vary. For example, the value comes from the user typing something on the keyboard, or it is read from a file.

Imagine you have a program that asks someone for their name, and a welcome message is printed with their name in it.
To add on to that, a basic intrinsic value of having variables, as @TheIdeasMan said, is
from the user typing something on the keyboard

Think of it this way: Why do we use credit cards instead of carrying around with us loads of bills, and a crapload of pennies, nickels, dimes, and quarters?

To address your examples, @OP, one of the few reasons why someone would choose to print out a statement as it is, such that cout << "My favorite movie is Wheels of Fury"; is because they just want a one time message, and/or they don't really care for it.

Now, to address the first way. Think of a simple numbers calculation, for example:
1
2
3
int foo = 5;
int bar = 10;
int fooBar = foo*bar;

Now pretend you're a NASA scientist whose sole job is to calculate when the universe is going to shit on itself. You wrote a calculation program that's thousands of lines long. How many variables would you need? What if new research comes out and completely changes your formula around? Would you retype every number to the decimal? HELL NO! You're going to go in, and adjust the values you set your variables to, so the functions they go in will still be in the same places, just with different numbers used to calculate things.
Last edited on
Topic archived. No new replies allowed.