### Problem in a very simple program if user inputs float in a variable that is defined as int.

Hello everyone!

So I recentry started to study computer science in a university. Hadnt had any background in c++ before or any other popular language.

In my programming course I have to write my first basic program by myself and I managed to write one that sort of works. But the problem is with some values that are possible to be input by the user.

The task is to creat a program where user inputs
q = common ratio and n = geometric progressions first number amounts.
It must be repeatable without exiting and it must display error messages if user types impossible values.
The only thing that is given is that the first number is 1.

So for example user inputs q=3, n=4. Program prints n1=1, n2=1*3=3, n3=3*3=9, n4=3*9=27 and asks if you want to repeat it.
The problem is for example if user inputs n as any float type number my program doesnt work correctly.
Is it possible to somehow fix it? Im sorry for my english its not my first language. Heres my code, i tryed to translate it from my native language.
 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````int main () { int ok; do { double a; double q; int n; int i; a = 1; i = 1; cout << "Type common ratio!" << endl; cout << "Common ratio: "; cin >> q; cout << endl; cout << "Geometric progressions first number amount!" << endl; cout << "First number amount: "; cin >> n; cout << endl; if ((n<1) || (q==0)) cout << "Error!!! Common ratio = 0 or first number amount <1!!!"; else if (!cin) return 0; else { while (i<=n) { cout << a << endl; i++; a = q * a; } cout << " Continue (1) or end (0)?" << endl; cin >> ok; }}while (ok==1); return 0; }``````
Last edited on
You could change `int n;` to `float n;`
If n is supposed to be an integer than the user of your program is just trying to break it.
Please use code tags - the <> button on the right.

Why long double - ordinary double should be plenty of precision. Do they have to be doubles anyway? Will an unsigned int do just as well?

Use a bool variable Quit along with a while loop:

 ``12345678`` ``````bool Quit = false; while(!Quit) { //your code here //user wants to quit Quit = true; } ``````

I absolutely hate do loops - they almost always be written as while or for loops. Just because a do loop always executes once, doesn't mean one should use them all the time.

Also, don't put multiple statements on 1 line, it makes things awkward to read.

Now, to your original question. You can test the fail bit of cin, to see whether it worked or not. Google it to find out how. Look at the reference section on this page at the top left.

It is one of the main pillars of coding, to be always aware of what can go wrong, and to test things for validity. Examples are - get input, test it is valid ; Open a file, see that it worked etc.

HTH
 If n is supposed to be an integer than the user of your program is just trying to break it.

Well yes it is supposed to be int because there cant be like 2.4 first number amounts or negative ones too. But im not really sure if my professor will test this or not.

 ThesIdeasMan
I dont really have much time now to check all of that right now because I will have to go to lecture soon but when im back home ill do that.

Thank you all for the great help and sorry for my awkward post!
Topic archived. No new replies allowed.