473,379 Members | 1,245 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

need some help with DEV C++

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
2 2438
Banfa
9,065 Expert Mod 8TB
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
angelcd
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

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

Similar topics

6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
5
by: John Flynn | last post by:
hi all i'm going to be quick i have an assignment due which i have no idea how to do. i work full time so i dont have the time to learn it and its due date has crept up on me .. As follows:...
0
by: xunling | last post by:
i have a question about answering ..... this topic is "need help" what do i have to write at te topic line, !after i have klicked the "answer message" button ive tried many possibilities,...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.