C++/loop

Advertisement


Question
i am doing a program to average grades that is ended by a sentinal. how do i remove an unwanted imput. if i enter a number > 100 it will tell me it is an invalid grade but it still counts it in the average....i cant get around it.

Answer
Note that as this is almost certainly homework or an assignment of some sort I am not going to show full C++ code. I shall use pseudo code instead - I shall leave it to you to re-phrase what I suggest as C++ in the context of your program.

I presume you have something like the following (in pseudo code):

   LOOP
       READ value

       IF value not in range
         OUTPUT message to user
       END IF
       
       UPDATE average calculation data
       
   WHILE not the sentinel value
         
So how about using something like the following in the loop:

   IF value not in range
       OUTPUT message to user
   ELSE
       UPDATE average calculation data
   ENDIF

That is either the value is bad - in which case you request the user to enter a value within the correct range as you are doing at the moment or the value is OK in which case it counts towards the average. Remember if statements can have an associated else clause (this is true of most - if not all - procedural programming languages).

If fact you should only count values towards the average if the value is in range and it is not the sentinel value. Also you should only output the message to the user about invalid input if the entered value is not the sentinel value:

   IF value is not the sentinel value
       IF value not in range
         OUTPUT message to user
       ELSE
         UPDATE average calculation data
       END IF
   ENDIF

Of course if we place the logic to read a value into a function that returns the value then we could re-phrase the logic like so:

   WHILE ReadValue(value) is not sentinel value
       IF value not in range
         OUTPUT message to user
       ELSE
         UPDATE average calculation data
       END IF
   END WHILE

Note that the ReadValue function above both returns the read value and updates the passed in argument with the read value.
I do not know if you have covered writing your own functions yet. Do not worry if you have not - I show the above just as another possibility.

In C and C++ if ... else looks like so:

   if ( condition )
       if-statement
   else
       else-statement
       
Where the statements are either single statements such as
   
   std::cout << "Invalid input...\n";

or
   ++count;

Or are compound statement blocks enclosed in { and } such as:

   {
       ++count;
       sum += value;
   }

I tend to use the compound statement block forms even for single statements with if and else (and other places such as for, while, do etc) as it is quite likely extra statements will need to be added at some point so it is best to have put the { and } in place to start with rather than forget them later and have the program behave in unexpected ways! However this is a very personal sort of thing.

By the way what happens if the user enters a negative value? Are these valid for your program? Ditto for non-numeric input or floating point values.

You might like to consider how to check for such invalid input data - and how many ways a user could enter data in a bad or unexpected way.

Hope this moves you forward.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.