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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
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;
}
|