Click here to Skip to main content
15,887,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi guys am coding a program calculating the mean of test scores.

the user must be enter integer numbers between 1-100 and when that is true the number stored when it is false to ignore it and not calculate it

here is my code to correct for me if there is another way to do this.

C#
#include <iostream>

using namespace std;

int main()
{
    int a,b,c,d;
    float mean,total;

    cout<<"please enter score between 1-100"<<endl;
    cin>>a;
    cout<<"please enter score between 1-100"<<endl;
    cin>>b;
    cout<<"please enter score between 1-100"<<endl;
    cin>>c;
    cout<<"please enter score between 1-100"<<endl;
    cin>>d;

    if(a<=100){
    cout<<"your score is = "<<a<<endl;

    }else{

    cout<<"ERROR INTEGER MUST BE BETWEEN 1-100 \n"<<a<<endl;

    }
    if(b<=100){
    cout<<"your score is = "<<b<<endl;

    }else{

    cout<<"ERROR INTEGER MUST BE BETWEEN 1-100 \n"<<b<<endl;

    }


if(c<=100){
    cout<<"your score is = "<<c<<endl;

    }else{

    cout<<"ERROR INTEGER MUST BE BETWEEN 1-100 \n"<<c<<endl;

    }

if(d<=100){
    cout<<"your score is = "<<d<<endl;

    }else{

    cout<<"ERROR INTEGER MUST BE BETWEEN 1-100 \n"<<d<<endl;

    }

    mean=(a+b+c+d)/4;
    cout<<"the mean number is "<<mean<<endl;
        return 0;
}
Posted

1) You should learn to modularize your code right away. If you find that you are repeating a block of similar code more than once you should move it into its own function.
C++
ValidateUserEntryValue(a);
ValidateUserEntryValue(b);
ValidateUserEntryValue(c);
ValidateUserEntryValue(d);

void ValidateUserEntryValue(int value)
{
   if( value <= 100 )
   {
      cout<< "your score is = "<<value<<endl;
   }
   else
   {
      cout<<"ERROR  INTEGER MUST BE BETWEEN 1-100 \n"<<value<<endl;
   }
}


2) You do check to ensure that the value entered is within the correct range, but you do not ensure that the value is actually an integer. I believe your program as it is written will simply crash if the user enters something that doesn't automatically convert to an integer. To do that you should run a make use of the TryParse[^] method provided for you by the dotNet framework.

3) At this point you just have to write the logic to calculate your mean while ignoring bad values. That part I'm leaving for you because you haven't written anything to do that yet.

Cheers.
 
Share this answer
 
v2
Comments
[no name] 19-Oct-12 16:16pm    
thank you marcus
am just beginner in the first step of coding journey as u realized my bad code.
and thats what i come up with all my efforts, the problem is calculating the mean while am ignoring the valid values.
fjdiewornncalwe 19-Oct-12 16:19pm    
Just break the logic down. You need to collect all the valid values together and then use those values to calculate a mean.
If you don't know what a mean is, you can get that from here
Nelek 19-Oct-12 18:39pm    
Nice answer. +5
fjdiewornncalwe 19-Oct-12 19:15pm    
Thanks, Nelek.
Sergey Alexandrovich Kryukov 19-Oct-12 20:07pm    
It needs patience. My 5.
--SA
Quote:
the user must be enter integer numbers between 1-100
This requirement is NOT well checked, e.g.
Quote:
if(a<=100){
checks only the upper range (for instance -10 would be accepted). It should be:
C++
if (a >= 1 && a <= 100)


Quote:
mean=(a+b+c+d)/4;
Suppose one or more inputs are rejected (not in range): then why are you still using all of them?

Instead you should count how many valid inputs are collected and, if the count is not 4 then either
  • Compute the mean value using only the valid inputs.
  • Abort computation because of invalid inputs.
 
Share this answer
 
Comments
[no name] 19-Oct-12 16:36pm    
i got it cPallini thanks alot for ur answer.
CPallini 19-Oct-12 18:04pm    
You are welcome.
Nelek 19-Oct-12 18:39pm    
Nice answer. +5
Sergey Alexandrovich Kryukov 19-Oct-12 20:08pm    
It took patience. I would probably noted only the first glitch caught my eyes. Well, a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900