Writing a Program that Can be Run Multiple times

How would you get a basic program to accept multiple entries? Right now I only know how to make a program that

1. Accepts input
2. Prints something

...and then it closes.
I would like to make it so that the user can repeatedly enter new inputs and receive new outputs without having to close and reopen the program. I'm going to guess it has something to do with loops, but I honestly have no idea.
You would be correct that it involves loops.

Read here.

http://www.cplusplus.com/doc/tutorial/control/


This would be a simple example from that tutorial

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// number echoer

#include <iostream>
using namespace std;

int main ()
{
  unsigned long n;
  do {
    cout << "Enter number (0 to end): ";
    cin >> n;
    cout << "You entered: " << n << "\n";
  } while (n != 0);
  return 0;
}
Alright, so here's what I've got.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <string>
#include <stdio.h>
void PressEnterToContinue()
  {
  int c;
  printf( "Press ENTER to continue... " );
  fflush( stdout );
  do c = getchar(); while ((c != '\n') && (c != EOF));
  }
using namespace std;
int main()
{
    string stockname;

    cout<<"Please enter desired stock name:"<<endl<<endl;
    cin>>stockname;

    if (stockname == "GOOG" || stockname == "goog") {
        cout<<"GOOG = 1056.44"<<endl<<endl;
    }
    else if (stockname == "APPL" || stockname == "appl") {
    cout<<"APPL = 546.22"<<endl<<endl;
}
    else if (stockname == "FB" || stockname == "fb") {
    cout<<"FB = 74.68"<<endl<<endl;
}
    else if (stockname == "AOL" || stockname == "aol") {
        cout<<"AOL = 44.46"<<endl<<endl;
    }
    else if (stockname == "YHOO" || stockname == "yhoo") {
        cout<<"YHOO = 36.29"<<endl<<endl;
    }
    else if (stockname == "YIPI" || stockname == "yipi") {
        cout<<"YIPI = 0.47"<<endl<<endl;
    }
    else if (stockname == "COKE" || stockname == "coke") {
        cout<<"COKE = 67.94"<<endl<<endl;
    }
    else if (stockname == "MSFT" || stockname == "msft") {
        cout<<"MSFT = 37.35"<<endl<<endl;
    }
    else if (stockname == "T" || stockname == "t") {
        cout<<"T = 35.28"<<endl<<endl;
    }
    else if (stockname == "PEP" || stockname == "pep") {
        cout<<"PEP = 84.38"<<endl<<endl;
    }
    else if (stockname == "WMT" || stockname == "wmt") {
        cout<<"WMT = 80.68"<<endl<<endl;
    }
    else if (stockname == "TGT" || stockname == "TGT") {
        cout<<"TGT = 63.87"<<endl<<endl;
    }
    else if (stockname == "SNE" || stockname == "sne") {
        cout<<"SNE = 18.16"<<endl<<endl;
    }
    else if (stockname == "INTC" || stockname == "intc") {
        cout<<"INTC = 23.65"<<endl<<endl;
    }
    else if (stockname == "IBM" || stockname == "ibm") {
        cout<<"IBM = 177.31"<<endl<<endl;
    }
    else if (stockname == "SBUX" || stockname == "sbux") {
        cout<<"SBUX = 81.51"<<endl<<endl;
    }
    else if (stockname == "MCD" || stockname == "mcd") {
        cout<<"MCD = 98.20"<<endl<<endl;
    }
    else if (stockname == "XOM" || stockname == "xom") {
        cout<<"XOM = 98.20"<<endl<<endl;
    }
    else if (stockname == "TWX" || stockname == "twx") {
        cout<<"TWX = 65.74"<<endl<<endl;
    }
    else if (stockname == "AXOM" || stockname == "axom") {
        cout<<"AXOM = 102.44"<<endl<<endl;
    }
    else if (stockname == "MGM" || stockname == "mgm") {
        cout<<"MGM = 18.83"<<endl<<endl;
    }
    else if (stockname == "BRK.A" || stockname == "brk.a") {
        cout<<"BRK.A = 174,840"<<endl<<endl;
    }
    else if (stockname == "BRK.B" || stockname == "brk.b") {
        cout<<"BRK.B = 116.58"<<endl<<endl;
    }
    else if (stockname == "TXN" || stockname == "txn") {
        cout<<"TXN = 42.70"<<endl<<endl;
    }
    else if (stockname == "ATVI" || stockname == "atvi") {
        cout<<"ATVI = 17.10"<<endl<<endl;
    }

cin.get();
PressEnterToContinue();
return 0;

    }


so if my variable is a string, then how would I do the 'while' statement? Or is there a completely different syntax?
You could add one more statement at the end to check for an exit input.

maybe something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main ()
{
  string stockname;
  do {
    cout << "Enter exit to end program ";
    cin >> stockname;
    cout << "You entered: " << stockname << "\n";
  } while (stockname != "exit");
  return 0;
}
The outcome is really close to what I was looking for, but instead of outputting the price of the stock, it just ouputs the name again.
Last edited on
Just replace the line:
cout << "You entered: " << stockname << "\n";

With the jumble of if statements you have in your previous code and you're done.
Nevermind, I replaced the "cout << "You entered: " << stockname << "\n";"
with main() and it more or less does what I had planned. Thank you very much!
Topic archived. No new replies allowed.