Using a Word as a Variable Value

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.

here is an example from this websites tutorial.
http://www.cplusplus.com/doc/tutorial/variables/

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>
using namespace 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>
using namespace std;

int main ()
{
  string mystring = "This is a string";
  cout << mystring << "\n";
  mystring = "This is the second string";
  cout << mystring;
  return 0;
}
Alright, so I played around a bit and I'm having trouble with the underlined sections

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string mystring = GOOG;
    string yourstring = APPL;

   cout<<"Please enter desired stock name:";
   cin>>GOOG; APPL;

   if (GOOG) {
       cout<<"GOOG = $1026.87";
   }

           else if (APPL) {
               cout<<"APPL = $567.98";
           }


    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!
Last edited on
Here is a working example that shows the user a list of available companies to choose from:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

using namespace std;

int main() {
  string mystring;

  cout << "Possible stocks:\nGoogle\nApple\n\n";
  cout << "Please enter desired stock name: ";
  cin >> mystring;

  if (mystring == "Google") {
    cout << "\nGoogle's stock price is $1026.87\n";
  }

  else if (mystring == "Apple") {
    cout << "\nApple's stock price is $567.98\n";
  }

  else {
    cout << "\nUnknown company!\n";
  }

  cin.get();
  return 0;
}


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.
Last edited on
Topic archived. No new replies allowed.