Read input numbers separated by spaces

前端 未结 4 1789
迷失自我
迷失自我 2021-02-01 16:49

This may be a total beginner\'s question, but I have yet to find an answer that works for me.

Currently, I\'m writing a program for a class that takes in a user\'s input

4条回答
  •  北荒
    北荒 (楼主)
    2021-02-01 17:34

    By default, cin reads from the input discarding any spaces. So, all you have to do is to use a do while loop to read the input more than one time:

    do {
       cout<<"Enter a number, or numbers separated by a space, between 1 and 1000."<> num;
    
       // reset your variables
    
       // your function stuff (calculations)
    }
    while (true); // or some condition
    

提交回复
热议问题