So i am trying to have the user only receive integers when they are choosing an array size but the numbers don't add up if I were to to type in a floating number. If they type in letters then the right error message pops up but I can't get right if they type in 4.4, 1.21, etc. the way I currently have it. Am I doing this the hard way? Would I be better off using stringstream? Is it possible to do it this way?
You could try using a float auxiliary variable then cast it into int in order to don't mess up the program if you type rational numbers.
look at this for example, number1 is float, so if you introduce a rational number (4.5 for example), the program won't do crazy stuff. Then we cast it as an int into number2 and this will give use 4 instead, the integer value you need. Of course if you type an int value from the start (as number1) it won't make a difference.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
int main()
{
float number1;
int number2;
std::cout<<"Introduce an integer number\n";
std::cin>>number1;
number2=static_cast<int>(number1);
std::cout<<number2;
return 0;
}