i'm still trying to solve the problem i came here for .. not grading my style |
Consistent indentation will help you find the bugs. Put another way, if you can't easily see the block structure by looking at the code, you're practically guaranteed to screw it up. As an aside, the Python language takes this to an extreme: the indentation
is the block structure - there are no {} or begin/end keywords.
Here is your code with consistent indentation:
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include <iostream>
#include <math.h>
using namespace std;
int
main()
{
int terms, i, k, fact, signChanger = -1, choose, student, Nmark, mark,
flag = 1, number, count = 0, max = 0;
float x, pwr, sum = 0, avg = 0, passAvg = 0;
cout << " 1- Print number of failed students and average marks of passed students.\n 2- Print max prime number. \n ";
cout << "3- print out the result of sin(x) \n 4- exit \n";
cin >> choose;
if (choose == 1) {
cout << " How many students " << endl;
cin >> student;
for (i = 1; i <= student; i++) {
cout << "How many marks " << endl;
cout << "Please enter the same number of marks for all students " << endl; // to get a reasonable avarege
cin >> Nmark;
for (int a = 1; a <= Nmark; a++) {
cout << "enter the mark" << endl;
cin >> mark;
sum = sum + mark;
avg = sum / Nmark;
}
if (avg > 60)
passAvg = passAvg + sum / Nmark;
else
count++;
sum = 0;
}
cout << "the avarege for passed students is : " << passAvg << endl;
cout << "the number of failed students is : " << count << endl;
}
else if (choose == 2) {
cout << " Enter positive numbers then enter -1 to show greatest prime number " <<
endl;
cin >> number;
if (number < 9 || number > 99999) {
cout << "number is not in the correct range\n";
return 1;
} else
while (number > 9 && number <= 99999) {
if (number < 0)
cout << "enter only positive numbers ";
cin >> number;
for (i = 2; i <= number; i++) {
if (number % i == 0) {
flag = 0;
break;
} else if (flag == 1) {
if (number > max)
max = number;
}
}
}
cout << "the max prime number \n " << max << endl;
}
else if (choose == 3) {
cout << "enter the value of x " << endl;
cin >> x;
cout << " enter the value of n terms " << endl;
cin >> terms;
for (i = 1; i <= terms; i = i + 2) {
pwr = 1;
fact = 1;
for (k = 1; k <= 1; k++) {
pwr = pwr * x;
fact = fact * k;
}
signChanger = signChanger * -1;
sum = sum + signChanger * pwr / fact;
}
cout << " Sum is :" << sin(sum) << endl;
} else if (choose == 4) { //exits when user presses 4
return 1;
} else
return 1; // exits if user presses a number or character other than the given options
return 0;
}
| |
Indented this way, some problems leap to my eyes:
- Line 56 sets
flag=0
and
it's never reset to 1. Since
flag
indicates whether
number
is prime, it should be set to 1 before you start checking for primality. If you had defined
flag
closer to where it's used, you might not have had this bug.
- Lines 58-62 are inside the
for
loop, so you're setting
max
before you even know if
number
is prime. I suspect that you thought this code was outside the loop, where it belongs. So here's a bug that was hidden by inconsistent indentation.
- Line 42 prompts for a number which is entered at line 44. But if you enter a valid number, line 52 forces you to enter it again, this time without any sort of prompt. As a result, the first number entered is never checked. If it turns out to be the maximum prime, the output will be incorrect.
The program exits at line 91 or 93. There is no loop, for more input. Again, you can see this from the indentation.
Have you learned about functions? It would be easier to keep the logic straight if you used functions for this program.