|
|
Main thing about streams: it is just provides a sequence of characters. They are all conceptually the same, only difference is their source: console window, files, sockets... Sometimes we need to handle a sequence of characters in memory as stream. This is what stringstreams do. So you want to get a line user inputs using getline and create a istringstream from that line. Now you have a stream which behaves exactly as cin, but is not "infinite" (when cin runs out of characters it just asks user for more), so you can actually reach the end and definetly say how many values are there. Now you just need to read n values. If at any time stream gets in failed state, then something is wrong in user input and you need to ask him again. That handles invald input. Now for too large input. For this you just need to attempt to read a single character after you get your numbers. If you succeed, it would mean that user entered something else after n numbers, if you fail, then there wasn't anything here and everything is correct. Ask if you need code examples. |
Jun 18, 2015 at 8:47am ne555 (7586) By the way, use a loop for line 26 |
|
|
.............. Last edited on Jun 17, 2015 at 10:36pm |
line 13 is an integer division. You need to cast at least one of those to float before the division. return (float) sum / n; Hope this helps. Jun 18, 2015 at 4:13am |
|
|
MiiNiPaa (7325) 1) std:: is a namespace. Mainly namespace where all standard library stuff reside. http://www.cplusplus.com/forum/beginner/61121/ Some bad teachers promote bad habit of dumping that namespace in global namespace by using using namespace std;. Which is considered bad practice: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice 2) This is a little tricky. First of all int stands for integer. It can accept only whole numbers. So to make it work with decimals you need to change it to something which can hold fractions: double. You need to change type of your array, type of temp variable and signature of your function. Do not change n or i, as they are not receiving your values but here to hold some natural number (which cannot be decimal) 3) This is a do-while loop http://www.learncpp.com/cpp-tutorial/56-do-while-statements/ http://www.cplusplus.com/doc/tutorial/control/#dowhile 4) Yes, this is pretty much that, see #3 |
as nobody else does it |