I am very new to c++ programming, and I have what I assume is a very basic issue, but I can't seem to find a direct answer. I have a profect for school coming up, and in order to impress my teacher I would like to write a simple program that allows the user to input a stock name, probably only two or three, maybe google, apple, and, say, Nike. The main issue is that I don't know how to make the variable "StockName" equal a word. I'm sure there is something very basic that is required, but fo the life of me I cannot find in answer on any FAQs, in my manual, or in any online tutorials.
mystring is the "variable" but you can set it equal to other things also
1 2 3 4 5 6 7 8 9 10 11
// my first string
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string mystring = "This is a string";
cout << mystring;
return 0;
}
An example here is I set mystring equal to another sequence of characters.
1 2 3 4 5 6 7 8 9 10 11 12 13
// my first string
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string mystring = "This is a string";
cout << mystring << "\n";
mystring = "This is the second string";
cout << mystring;
return 0;
}
Would I need to even declare the value of mystring and yourstring? Also, how do I write that there are two possible names that the user could put in, as with the third underline. And for the if and else statements, how would I say "if the input value is GOOG, then..." or "if the input value is APPL, then..."? Any critisism helps, I'm really trying to learn!
To explain a couple of things... The "\n" characters in the strings are newline characters. They will print a newline to the console before continuing to print the rest of the string on the next line (it's like pressing enter on the keyboard).
Also, notice the if statements. If you want to check equality on a string, you have to put the string in quotes. You can't just put GOOG like this:
if (mystring == GOOG) { }
because the compiler will think you are referring to a variable.
You have to put it in quotes:
if (mystring == "GOOG") { }
And to answer your question of declaring variables... You would have to declare the variable mystring to store the user's input. However, you would not have to immediately define, or give a value to, it. The variable can be left undefined until you store user input in it.