Hello.

I have created another homework assignment in C++. Program is working correctly, however there are 2 flaws I need help with.

Description: User enters the name of sportsman, and 7 marks given to this sportsman by referees. Marks must belong to interval [0.0 : 10.0] .
Float sum is the sum of all marks, excluding the highest and the lowest.

What does the program do?
User can enter up to 10 sportsmen (more are not allowed.). User must enter 7 marks for each sportsman. If the mark entered is higher than 10 or lower than 0, user is asked to enter the same mark again. Marks must have 1 number after the comma.

When mark No 7 is added, user is asked, if he wants to add another sportsman. If he types "j", another sportsman is added. If user types "n"(or anything else), all sportsmen (who were added before) and their marks are shown to the user.

7 marks of each sportsman are put into an array which is sorted in ascending order.

decimal() is the function which converts the inputted numbers, so they have only 1 number after comma. I know this method is very crude, however it does it's job. (1.13 becomes 1.1; 7.67 becomes 7.6)


Problems:
1. I have got 7 do-while loops in the beginning of the code. Is it possible to optimize that part of the code, to make it shorter and more efficient?

2. User is currently able to enter marks 10.1, 10.55 etc, even though they are higher than 10. Why is this happening?

I translated the exercise from my language into English. I'm also new to C++. This is why the code will seem very sloppy :)

Thanks to everyone who'll try to help.

#include <conio.h>
#include <iostream.h>
#include <stdio.h>

struct vertejumi
{
         float t1;
         float t2;
         float t3;
         float t4;
         float t5;
         float t6;
         float t7;
         float sum;
};

struct dati
{
			char sports[40];
			vertejumi vert;
};

int n=0,k,x=0;
float b[7],pag,g;

int input(dati a[10],int &n);
void output(dati a[10],int &n);
void swap(float& a, float& b);
float decimal(float m);

void main(){
			dati a[10];

         input(a,n);
         output(a,n);

			getch();
}

int input(dati a[10],int &n)
{
			char q;
         do{

         			cout<<"Sportsman: "; 		cin>>a[n].sports;

                  do{
                           cout<<"T1: ";
                  	   cin>>a[n].vert.t1;
                           decimal(a[n].vert.t1);
                           a[n].vert.t1=g;
                  }while(a[n].vert.t1<0 || a[n].vert.t1 > 10);
                  do{
                  	   cout<<"T2: ";
                  	   cin>>a[n].vert.t2;
                           decimal(a[n].vert.t2);
                           a[n].vert.t2=g;
                  }while(a[n].vert.t2<0 || a[n].vert.t2 > 10);
                  do{
                  	   cout<<"T3: ";
                  	   cin>>a[n].vert.t3;
                           decimal(a[n].vert.t3);
                           a[n].vert.t3=g;
                  }while(a[n].vert.t3<0 || a[n].vert.t3 > 10);
                  do{
                  	   cout<<"T4: ";
                  	   cin>>a[n].vert.t4;
                           decimal(a[n].vert.t4);
                           a[n].vert.t4=g;
                  }while(a[n].vert.t4<0 || a[n].vert.t4 > 10);
                  do{
                  	   cout<<"T5: ";
                  	   cin>>a[n].vert.t5;
                           decimal(a[n].vert.t5);
                           a[n].vert.t5=g;
                  }while(a[n].vert.t5<0 || a[n].vert.t5 > 10);
                  do{
                  	   cout<<"T6: ";
                  	   cin>>a[n].vert.t6;
                           decimal(a[n].vert.t6);
                           a[n].vert.t6=g;
                  }while(a[n].vert.t6<0 || a[n].vert.t6 > 10);
                  do{
                  	   cout<<"T7: ";
                  	   cin>>a[n].vert.t7;
                           decimal(a[n].vert.t7);
                           a[n].vert.t7=g;
                  }while(a[n].vert.t7<0 || a[n].vert.t7 > 10);

                  cout<<a[n].sports<<"\t";
                  cout<<a[n].vert.t1<<"\t";
                  cout<<a[n].vert.t2<<"\t";
                  cout<<a[n].vert.t3<<"\t";
                  cout<<a[n].vert.t4<<"\t";
                  cout<<a[n].vert.t5<<"\t";
                  cout<<a[n].vert.t6<<"\t";
                  cout<<a[n].vert.t7<<"\n"<<n<<"\n";

                  n++;
                  cout<<"\n Will you continue inputting data? (j/n) ";
                  cin>>q;

         }while((q=='j' || q=='J') && n<10 );
         return n;
}

void output(dati a[10],int &n)
{
			for(int i=0; i<n; i++)
         {
                  printf("%s\t",a[i].sports);
                  printf("%2.1f\t",a[i].vert.t1);
                  printf("%2.1f\t",a[i].vert.t2);
                  printf("%2.1f\t",a[i].vert.t3);
                  printf("%2.1f\t",a[i].vert.t4);
                  printf("%2.1f\t",a[i].vert.t5);
                  printf("%2.1f\t",a[i].vert.t6);
                  printf("%2.1f\t\n",a[i].vert.t7);

                  b[0]=a[i].vert.t1;
         			b[1]=a[i].vert.t2;
         			b[2]=a[i].vert.t3;
         			b[3]=a[i].vert.t4;
         			b[4]=a[i].vert.t5;
         			b[5]=a[i].vert.t6;
         			b[6]=a[i].vert.t7;

                  // burbula algoritms
						for(k=0;k<=7-2;k++)
                  {
                           for(int l=0;l<=7-2-k;l++)
                  			{
   											x=x+1;
												if(b[l]>b[l+1])
                           			{
      													pag=b[l];
         												b[l]=b[l+1];
                           						b[l+1]=pag;
												}
   								}
						}

                  printf("%s\t",a[i].sports);
                  printf("%2.1f\t",b[0]);
                  printf("%2.1f\t",b[1]);
                  printf("%2.1f\t",b[2]);
                  printf("%2.1f\t",b[3]);
                  printf("%2.1f\t",b[4]);
                  printf("%2.1f\t",b[5]);
                  printf("%2.1f\n",b[6]);

                  a[i].vert.sum=b[1]+b[2]+b[3]+b[4]+b[5];

                  printf("Sportists %s gained %5.1f points.\n\n",a[i].sports,a[i].vert.sum);

         }
}

void swap(float& a, float& b){
			float pag;
   		pag=a;
   		a=b;
   		b=pag;
}

float decimal(float m){
float s=m*10.0;
int c=s;
g=c/10.0;
return g;
}

replace all 7 loops with something a function

void GetMartks(std::string prompt, float& mark)
{
    cout << prompt << '\n';
    cin >> mark;
    mark = decimal(mark);
}

And call it in main() like this:

GetMark("T1:", a[n].vert.t1);
          GetMark("T2:", a[n].vert.t2);
          GetMark("T3:", a[n].vert.t3);
          GetMark("T4:", a[n].vert.t4);
          GetMark("T5:", a[n].vert.t5);
          GetMark("T6:", a[n].vert.t6;
          GetMark("T7:", a[n].vert.t7);

2) Because your program does not validate input. The decimal() function is broken when I enter a value > 99 or < 10. There are standard floating point functions to extract the decimal value of a float. This is a list of all the standard C/C++ math functions.

I did think about what you suggested, made some changes and it seems to work very well now. Both problems I described earlier have been solved.
I'll mark the topic as solved later today or tomorrow if I don't come across any other problems :)

Thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.