Hi all , I have successfully written a simple program to calculate the total points scored by backs and forwards against one another in a 18 person team, but I need to safe proof it against a person entering anything other than F/f for forwards or B/b for backs , if something else is input I need the program to ask the user the (Enter F for forwards and B for Backs: ) question again untill an appropriate input is recieved, I thought of a do while statment in my (void inputAndValidate) section around that question but it doesnt seem to work, could anybody help ? Here is my code bellow

//Assignment 3 Question 5A
#include <iostream>
#include <string>
using namespace std;

void updateCorrectTotal (char & position, int & points, int & totForward, int & totBack)
{

if (position == 70 || position == 102)
totForward = (totForward + points);
if (position == 66 || position == 98)
totBack = (totBack + points);


}


void inputAndValidate (char & position, int & points)
{

cout << " This program will calculate the total amount of points scored by forwards" << endl;
cout << " Against the total number of points scored by Backs." << endl;
cout << " " << endl;
cout << " Please Enter your 18 players positions followed by there points scored." << endl;
cout << " " << endl;

cout << " Enter F for forwards and B for Backs: ";
cin >> position;


cout << " Enter the number of points scored by the player: " ;
cin >> points;


}


int main() 
{

char position;
int points, totForward, totBack;  

totForward = 0;
totBack = 0;

for (int i = 1; i <= 18; i++)
{
inputAndValidate (position, points);

updateCorrectTotal (position, points, totForward, totBack);
}
cout << " Total number of points scored by forwards: "
 << totForward << endl;
cout << " Total number of points scored by backs: "
 << totBack << endl; 
  
  
  return 0;
}
]

Recommended Answers

All 2 Replies

just put lines 28 and 29 in a loop. After line 29 add an if statement to validate data imput. If its ok then exit the loop.

Thanks very much dont know why I couldnt get that must be because its friday, will make sure code is input correctly next time , Thanks

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.