By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,435 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,435 IT Pros & Developers. It's quick & easy.

need some help with DEV C++

P: 15
hi guys, i have here my sample program, actually it compiles and run but some of the output does'nt come out on the screen.. can someone help me what's wrong with this...

here's my code:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. using std::cin;
  4. using std::cout;
  5.  
  6. class Grade
  7.  
  8. {
  9. public:
  10.  
  11.     Grade () {};
  12.     ~Grade(){};
  13.     int GradeTotalEntry ( int total);
  14.     int GradeFailed (int failed, int total );
  15.     int GradeD (int D, int total);
  16.     int GradeC (int C, int total);
  17.     int GradeB (int B, int total);
  18.     int GradeA (int A, int total);
  19.     int GradeDistinction (int Dis, int total);
  20.  
  21. };
  22.  
  23. int Grade::GradeTotalEntry (int total )
  24.  
  25. {
  26.     return total;
  27. }
  28.  
  29. int Grade::GradeFailed (int failed, int total)
  30.  
  31. {
  32.     return (failed/total)*100;
  33. }
  34.  
  35. int Grade::GradeD (int D, int total)
  36.  
  37. {
  38.     return (D/total)*100;
  39. }
  40.  
  41.  
  42. int Grade::GradeC (int C, int total)
  43.  
  44. {
  45.     return (C/total)*100;
  46. }
  47.  
  48.  
  49. int Grade::GradeB (int B, int total)
  50.  
  51. {
  52.     return (B/total)*100;
  53. }
  54.  
  55.  
  56. int Grade::GradeA (int A, int total)
  57.  
  58. {
  59.     return (A/total)*100;
  60. }
  61.  
  62. int Grade::GradeDistinction (int Dis, int total)
  63.  
  64. {
  65.     return (Dis/total)*100;
  66. }
  67.  
  68. int main ()
  69.  
  70. {
  71.  
  72.     int n[100],j,total=0;
  73.     int failed=0,A=0,B=0,C=0,D=0,Dis=0;
  74.     char answer[5] ="yes";
  75.     Grade *p = new Grade;
  76.  
  77.  
  78.  
  79.     cout <<"Welcome to your Grade Computation:\n";
  80.  
  81.     while (strcmp (answer, "no")!=0)
  82.  
  83.  
  84.       {
  85.         cout <<"Enter Mark:\t";
  86.         cin >> n[total];
  87.         total++;
  88.  
  89.           {
  90.  
  91.         if (n[j]>100)
  92.           {
  93.         cout <<"can't be more than 100, pls enter another number from 0 to 100:\n";
  94.           }
  95.         cout <<"Would you like to enter another mark? (yes), or (no)";
  96.         std::cin >> answer;
  97.           }
  98.  
  99.  
  100.         while ((strcmp (answer, "yes")!=0) && (strcmp (answer, "no")!=0))
  101.  
  102.           {
  103.         cout << "Please answer only by \"yes\" or \"no\"!";
  104.         std::cin >> answer;
  105.  
  106.           }
  107. }
  108.         cout <<"\n";
  109.         cout<<"Your total input:\t"<<p->GradeTotalEntry(total)<<"\n";
  110.  
  111.         for ( j=0; j<total; j++)
  112.     {
  113.          if (n[j] >=90)
  114.         Dis ++;
  115.          else if (n[j] >=80)
  116.         A++;
  117.          else if (n[j] >=70)
  118.         B++;
  119.          else if (n[j] >=60)
  120.         C++;
  121.          else if (n[j] >=50)
  122.         D++;
  123.          else if (n[j] < 50)
  124.         failed++;
  125.     }
  126.         cout <<"\n";
  127.         cout << "Percentage Distinction:\t"<<p->GradeDistinction(Dis,total) <<"%" <<"\n";
  128.         cout << "Percentage A:\t"<<p->GradeA (A,total)<<"%"  <<"\n";
  129.         cout << "Percentage B:\t"<<p->GradeB (B,total) <<"%" <<"\n";
  130.         cout << "Percentage C:\t"<<p->GradeC (C,total) <<"%" <<"\n";
  131.         cout << "Percentage D:\t"<<p->GradeD (D,total) <<"%" <<"\n";
  132.         cout << "Percentage failed:\t"<<p->GradeFailed (failed,total) <<"%" <<"\n";
  133.  
  134.      delete p;
  135.      system ( "pause");
  136.      return 0;
  137. }
  138.  
when i input something it works and it ask me the question if i want to enter another mark and it continues up to the time i answers no, it actually reads my total number, but it doesnt print the percentage of the grades, he always read zero for all grades even i enter all the inputs, and when i enter 101 for example because it exceed my limit, and i enter the number again the out put of

"can't be more than 100, pls enter another number from 0 to 100:\n"

does'nt erase on the screen even i enter smaller number, what happen to this

i need some help

thanks in advance'

angel
Dec 21 '06 #1
Share this Question
Share on Google+
2 Replies


Banfa
Expert Mod 5K+
P: 8,916
Ok here are a couple of problems you have, in this section of code

Expand|Select|Wrap|Line Numbers
  1.     cout <<"Enter Mark:\t";
  2.     cin >> n[total];
  3.     total++;
  4.  
  5.     {
  6.         if (n[j]>100)
  7.         {
  8.             cout <<"can't be more than 100, pls enter another number from 0 to 100:\n";
  9.         }
  10.  
  11.         cout <<"Would you like to enter another mark? (yes), or (no)";
  12.         std::cin >> answer;
  13.     }
  14.  
  15.  
The { and } in bold are unrequired
You check n[j] in the if statement but j is not initalised yet
Even if the if statement was working correctly you have already incremented total so the erroneous mark gets included into your list of marks anyway.


The following error and explaination applies to GradeFailed, GradeD, GradeC, GradeB, GradeA and GradeDistinction but I will only consider GradeA

Expand|Select|Wrap|Line Numbers
  1. int Grade::GradeA (int A, int total)
  2.  
  3. {
  4.     return (A/total)*100;
  5. }
  6.  
This is supposed to calculate the % of grade As from the total number of grades, so if A is 23 and total is 46 you would expect an answer of 50% right?

Wrong, this is done in integer arithmatic, so no fractions and you specifically do A/total first. Puting the numbers into the calculation

(23 / 46) * 100

So we do 23 / 46 first. What is 23 / 46? Well 0.5 in real arithmatic but in integer arithmatic it is 0.

0 * 100 = 0

You need to perform the multiplication first, this is generally true for integer arithmatic except where performing the multiplication first would result in an overflow (i.e. the resulting number is too large to be held in the variable type).

So a re-write like this should cure the problem

Expand|Select|Wrap|Line Numbers
  1. int Grade::GradeA (int A, int total)
  2.  
  3. {
  4.     return A * 100 / total;
  5. }
  6.  
Dec 22 '06 #2

P: 15
Ok here are a couple of problems you have, in this section of code

Expand|Select|Wrap|Line Numbers
  1.     cout <<"Enter Mark:\t";
  2.     cin >> n[total];
  3.     total++;
  4.  
  5.     {
  6.         if (n[j]>100)
  7.         {
  8.             cout <<"can't be more than 100, pls enter another number from 0 to 100:\n";
  9.         }
  10.  
  11.         cout <<"Would you like to enter another mark? (yes), or (no)";
  12.         std::cin >> answer;
  13.     }
  14.  
  15.  
The { and } in bold are unrequired
You check n[j] in the if statement but j is not initalised yet
Even if the if statement was working correctly you have already incremented total so the erroneous mark gets included into your list of marks anyway.


The following error and explaination applies to GradeFailed, GradeD, GradeC, GradeB, GradeA and GradeDistinction but I will only consider GradeA

Expand|Select|Wrap|Line Numbers
  1. int Grade::GradeA (int A, int total)
  2.  
  3. {
  4.     return (A/total)*100;
  5. }
  6.  
This is supposed to calculate the % of grade As from the total number of grades, so if A is 23 and total is 46 you would expect an answer of 50% right?

Wrong, this is done in integer arithmatic, so no fractions and you specifically do A/total first. Puting the numbers into the calculation

(23 / 46) * 100

So we do 23 / 46 first. What is 23 / 46? Well 0.5 in real arithmatic but in integer arithmatic it is 0.

0 * 100 = 0

You need to perform the multiplication first, this is generally true for integer arithmatic except where performing the multiplication first would result in an overflow (i.e. the resulting number is too large to be held in the variable type).

So a re-write like this should cure the problem

Expand|Select|Wrap|Line Numbers
  1. int Grade::GradeA (int A, int total)
  2.  
  3. {
  4.     return A * 100 / total;
  5. }
  6.  

hi sir, thanks for your help i think your surely right, i will try this one, maybe thats the real problem because all aswers in percentage came out zero, so in your explanation it was really true....

thanks again,

regards,
angel
Dec 27 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.