Change the variable inside a loop

Hello,

I'm making a utility for my school to generate a script for out teleprompter. I need to change a variable inside a loop (I will highlight the variable). Heres the code:

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
#include <iostream>
using namespace std;
int cday;
int Loop;
int TimesLooped;
char test;
char responce;
string Lunch = " ";
string Anchor1 = " ";
string Anchor2 = " ";
string Date = " ";
string anc1 = " ";
string anc2 = " ";
string anc3 = " ";
string anc4 = " ";
string anc5 = " ";
string anc6 = " ";
string anc7 = " ";
string anc8 = " ";
string anc9 = " ";
string anc10 = " ";
int main ()
{
	cout << "What is today's full date?" << endl;
	getline(cin, Date);
	cout << endl;
	cout << "Cycle day..." << endl;
	cin >> cday;
	cout << endl;
	getline(cin, Anchor1);
	cout << "First Anchor's Name?" << endl;
	getline(cin, Anchor1);
	cout << endl;
	cout << "Second Anchor's Name?" << endl;
	getline(cin, Anchor2);
	cout << endl;
	cout << "Today's lunch is..." << endl;
	getline(cin, Lunch);
	cout << endl;
anc:
	cout << "Are there any extra announcements? y/n" << endl << "-";
	cin >> responce;
	getline(cin, anc10)
	switch (responce) {
		case 'y':
			cout << "How many?" << endl;
			cin >> Loop;
			cout << endl;
			while (Loop != TimesLooped) {
				cout << "Type an announcement";
				getline(cin, anc1);
			}
			break;
		case 'n':
			cout << "Good morning DMS.  " << endl << "I'm " << Anchor1 << ", " << endl << "and I'm " << Anchor2 << "." << endl;
			cout << "Todays Date is " << Date;
			break;

		default:
			cout << "invalid responce";
			goto anc;
			break;
	}
	
}


Still working on it. If I left something out tell me. the variable is bolded and underlined in line 51. I need to change it from anc1 to anc2 - anc10 after each loop. The int Loop is the number of times it should loop. int TimesLooped is the amount of times the loop has repeated.

Thanks,
Adam
Last edited on
Hi Adam,

you could use an array for each of the ten strings. This would look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
// At the top, instead of anc1,...,anc10:
string anc[10];

// ...

// The loop:
int index = 0;
while (Loop != TimesLooped) {
    cout << "Type an announcement";
    getline(cin, anc[index]);
    index++;
}

This works of course for maximal 10 strings. You can also achieve having an (almost) arbitrary number of strings by dynamically allocating the array anc.

Greetings, TheBear

Last edited on
Hi TheBear,

I keep getting errors with the index++. Im am using xcode for mac. I have placed the errors in the lines of code where they occur.

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
#include <iostream>
using namespace std;
int cday;
int Loop;
int TimesLooped;
int index = 0; //'int index' redeclared as different kind of symbol
char test;
char responce;
string Lunch = " ";
string Anchor1 = " ";
string Anchor2 = " ";
string Date = " ";
string anc[10];
int main ()
{
	cout << "What is today's full date?" << endl;
	getline(cin, Date);
	cout << endl;
	cout << "Cycle day..." << endl;
	cin >> cday;
	cout << endl;
	getline(cin, Anchor1);
	cout << "First Anchor's Name?" << endl;
	getline(cin, Anchor1);
	cout << endl;
	cout << "Second Anchor's Name?" << endl;
	getline(cin, Anchor2);
	cout << endl;
	cout << "Today's lunch is..." << endl;
	getline(cin, Lunch);
	cout << endl;
anc:
	cout << "Are there any extra announcements? y/n" << endl << "-";
	cin >> responce;
	switch (responce) {
		case 'y':
			cout << "How many?" << endl;
			cin >> Loop;
			cout << endl;
			while (Loop != TimesLooped) {
				cout << "Type an announcement";
				getline(cin, anc[index]);  //invalid types 'std::string [10][char* ()(const char*, int)]' for array subscript
				index++;  //ISO C++ forbids incrementing a pointer of type 'char* (*)(const char*, int)' (2)
			}
			break;
		case 'n':
			cout << "Good morning DMS.  " << endl << "I'm " << Anchor1 << ", " << endl << "and I'm " << Anchor2 << "." << endl;
			cout << "Todays Date is " << Date;
			break;

		default:
			cout << "invalid responce";
			goto anc;
			break;
	}
}


Thanks,
Adam
Last edited on
I can't reproduce this error here, the program compiles and runs. I assume that there is a variable index already declared in iostream, so maybe changing the name of index to something else does the trick.
ok I changed index to index2 and it cleared up all of the issues except
1
2
3
4
while (Loop != TimesLooped) {
				cout << "Type an announcement";
				getline(cin, anc[index2]);
				index++;  //ISO C++ forbids incrementing a pointer of type 'char* (*)(const char*, int)' (2) 

im going to compile it in dev c++ not xcode and try it

thanks,
adam
You need to change the name 'index' every time you use it:

1
2
3
4
while (Loop != TimesLooped) {
				cout << "Type an announcement";
				getline(cin, anc[index2]);
				index2++;  // <== change it here too 
Topic archived. No new replies allowed.