Error: expected initializer before '<<' token, and more.

Hello guys,

I'm new to C++ programming, so now, I got the first errors which I can't solve at my own :o.

So, I got Error: expected initializer before '<<' token, at line 15. And the error: 'choice_' was not declared in this scope, at line 17.

This is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <stdlib.h>

using namespace std;

int main() {
    int choices = 0, n, result;
    cout << "Welcome! I'll help you to make a decision.\n";
    goto amount_choices;
    amount_choices:
    cout << "How many choices are there? (Numeric): ";
    cin >> choices;
    n = 1;
    while (choices <= n) {
        string choice_ << n;
        cout << "\nPlease insert a choice: ";
        cin >> choice_ << n;
        n++;
    }
    result = rand() % choices + 1;
    cout << result;
}


I got also another question: what is possible for me to make, to improve my C++ skills? Aaandd, at what point of my C++ carier is it possible for me to start developing programs for Windows and Linux?

Thanks in advance,
iRoot121.
What are you meaning to do at lines 15 and 17?

Well, I've n = 1. n is the amount of answers a user can insert. So I want to define the variable choice_ << n where n is a variable number. Ex: choice_5
You can't define a variable with a stream operator (<< or >>) like you're trying to do.

It sounds like you might want to use an array or vector here? So for example if a person wants to enter three choices, you'll let them enter three different statements to store as a string? Then the program will randomly choose one of the statements to give as the answer?

So if the array name is choices and there are three elements, the individual elements would be as follows: (arrays always start numbering at 0)
choices[0] holds the first choice entered
choices[1] holds the second choice entered
choices[2] holds the third choice entered

Here's an example using a dynamically allocated array - it's dynamically allocated since you won't know the number of elements the array needs until the person enters a number.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

int main() 
{
    int userChoice, result;
    srand(time(0));//seed random function with current time
    
    cout << "Welcome! I'll help you to make a decision.\n";
    cout << "How many choices are there? (Numeric): ";
    cin >> userChoice; //get number from user
    cout << "\nYou said you have " << userChoice << " choices.";
    cin.ignore();
    
    string * choices; //create pointer to string type
    choices = new string [userChoice];//create dynamic array of length given by user
    
    for (int i = 0; i < userChoice; i++)
    {
    	cout << "\nPlease enter a choice";
    	getline(cin,choices[i]); // use getline to get multiple words from input 
    	cout << "\nYou entered " << choices[i];
    }
    
    result = rand()%userChoice;
    cout << "\nYou should choose " << choices[result];
    
    delete [] choices;//delete array to prevent memory leak
    
    return 0;
}
Welcome! I'll help you to make a decision.
How many choices are there? (Numeric): 
You said you have 3 choices.
Please enter a choice
You entered My first choice
Please enter a choice
You entered My second choice
Please enter a choice
You entered My third choice
You should choose My second choice
Yes, that was what I meant to do. Thanks for your help!
Topic archived. No new replies allowed.