Home > Enterprise >  using cin to read in a random number of lines from the input stream c
using cin to read in a random number of lines from the input stream c

Time:11-20

I'm designing an algorithm, where the first number is going to tell me how many lines of codes the file will contain. So, it's like:

3      /// number of lines that need to be configured
56227
-19
322
int main(){
    int variable;

    cin >> variable;

    for (int i = 0; i < variable; i  ){
         cin >> variableTwo;

         /// Then algorithm with a bunch of code?
    }
}

Would this work?

CodePudding user response:

No it will not because you have an unclosed brace for the for loop structure and you have not declared the type of variableTwo. This will work instead....

int main(){
    int variable;
    int variableTwo;

    cin >> variable;

    for (int i = 0; i < variable; i  ){
         cin >> variableTwo;

         /// Then algorithm with a bunch of code?
     }
}

CodePudding user response:

Declaring the second variable can run your code without an error. So declare the variableTwo. And if you wanted to store the given lines value that you wanted to configure, for further use then please use an Array variable instead.

For example:

int variableTwo;

or

int variableTwo[size];
for(i=0; i<size;   i) {
    cin>>variableTwo[i];
    //your rest code
}
  • Related