1

So I took a C++ class in High School, but haven't done it in years, so I'm basically new. So I am wondering if there is a limit to the amount of cins you can do. It allows me to input 7 and then skips all the other inputs to go to the end. My guess is that there's a data limit. Is this correct?


#include <iostream>
using namespace std;

int main() {
int time1;
int time2;
int time3;
int time4;
int time5;
int time6;
int time7;
int time8;
int time9;
int time10;

cout<<"enter number";
cin>>time1;
cout<<"enter number";
cin>>time2;
cout<<"enter number 1";
cin>>time3;
cout<<"enter number 1";
cin>>time4;
cout<<"enter number 1";
cin>>time5;
cout<<"enter number 1";
cin>>time6;
cout<<"enter number 1";
cin>>time7;
cout<<"enter number 1";
cin>>time8;
cout<<"enter number 1";
cin>>time9;
cout<<"enter number 1";
cin>>time10;
cout<<"the end?";
}
Jhon Lewis
  • 19
  • 2
  • 2
    What have you been inputting? – Nathan Pierson Dec 18 '20 at 04:16
  • 2
    I cannot reproduce your question. Maybe you inputted something that is not a number and `cin` skipped all the other calls because of `cin.fail()`? – Gary Strivin' Dec 18 '20 at 04:49
  • A limit to the number of `cin <<` you can do would be very strange (especially a limit as low as seven). Something else is going on, but theres not enough information to say what that is. – john Dec 18 '20 at 06:07

2 Answers2

0

You can use cins unlimited and you need to use loops. Here I provided example of fillind array buy cin.

    #include <iosteam>
        using namespace std;
        int main(){
           cout<<"Enter number of cins\n";
           int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++){
cout<<"Enter "<<i<<" number\n";
        cin>>a[i];
}
         
        }
Pavlo
  • 166
  • 1
  • 11
  • 5
    Asker's code should run fine without _needing_ to use loops, so while they might be sensible it doesn't address the question. Your own code has the problem that it uses a [variable-length array](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) which isn't part of the C++ standard. – Nathan Pierson Dec 18 '20 at 04:22
0

Your code should work fine if you only enter numbers. There is no limit to the amount of cins you can do. My guess is that you enter something that isn't a number, e.g. a string. When that happens cin's error flag is set and future attempts to get input will fail.

What you can do is add some input validation if you want. A simple if statement will do:

int num{0};
if (cin >> num)
{
    ....
}

If the input is not valid, i.e. the if condition is false, you need to clear the error:

cin.clear();

and discard everything remaining in the input buffer and newline:

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

You could use a std::vector to store the numbers instead of separate integer variables and use a do...while loop. Putting this all together your code could look like this:

#include <iostream>
#include <vector>
#include <limits>

int main()
{
    std::vector<int> nums;
    int n{0};

    std::cout << "Enter total number to input" << std::endl;
    std::cin >> n;
    int i{0};

    do
    {
        std::cout << "Enter number " << i+1 << std::endl;

        int num{0};
        if (std::cin >> num)
        {
            nums.push_back(num);
            ++i;
        }
        else
        {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Invalid input" << std::endl;
        }
    } while (i < n);
}

Note that I'm not using namespace std in my code. That's considered bad practice.

jignatius
  • 6,304
  • 2
  • 15
  • 30