Dollars BBS | Technology

feed-icon

Main

News

Animation

Art

Comics

Films

Food

Games

Literature

Music

Personal

Sports

Technology

Random

Error Checking in C++ (14)

1 Name: Shade : 2014-05-14 13:16 ID:tkSf+Pe5 [Del]

I figure this is the right place to ask this. I'm working on an assignment for my Programming class that accepts user input and will continuously ask the user for an input in a loop until 0 is entered. The program cannot accept letters or decimals. My teacher wants full error checking. The problem is, error checking is not covered in our textbook at all. He left it up to us to figure out how to do it. I've tried using Google, but that has gotten me absolutely nowhere. Then I remembered this website and figured I'd come here and ask for help. I just need to know how to do all kinds of error checking in C++. What is the code needed for that? Here's the assignment description: "Write a program that asks the user for a series of integers one at a time. When the user enters the integer 0, the program displays the following information: The number of integers in the series(not including zero), The average of the integers, The largest integer in the series, The smallest integer in the series, and The difference between the largest and smallest integer in the series." I have to do all of that in addition to full error checking. Can anyone help me out with this code?

2 Name: MaskSalesman : 2014-05-14 15:50 ID:jffTdr+Q [Del]

I find it very surprising that your textbook does not cover error checking/handling considering coding and debugging are akin to hammer and anvil. That aside, from the sounds of it what you need is a construct that checks if a user submitted wrong input, does not accept that input, and informs the user that the previous input they submitted is not correct (and maybe a reminder of what the correct input is) before continuing.

There are many ways to do this; if you would post the code you have written so far and any ideas you have in regards how to do this error checking, I can give suggestions based on your existing code.

3 Name: Anonymous : 2014-05-14 21:47 ID:3r6kinLy [Del]

Think of everything that the user could enter that is not 0. Make your code know what to do with it. I don't know much about C++, but I'll bet that you can accept a string, remove all whitespace, and check if the string matches the string "0".

4 Name: Inuhakka !u4InuhakKA : 2014-05-15 08:15 ID:uvJq3lm3 [Del]

I'm not sure how to catch exceptions in C++, but I'm sure it's at least similar to Java. You can either use if statements to manually catch everything, or use exception handling. You can find tutorials on exception handling in C++ online.

5 Name: Shade : 2014-05-15 12:33 ID:tkSf+Pe5 [Del]

>>2 Here's the current code I have. Sorry it took so long. I made this thread when I was at home and Programming is my last class of the day. The program is supposed to only accept integers(i.e. 1,4,7,9.0, etc.), and if 0 is entered it stops the loop and calculates everything mentioned in the program description, but does not count 0. Zero cannot be entered first, it cannot accept letters or symbols, and it can not accept floating point numbers(i.e. 9.2, 9.02, 9.00009, etc. along those lines). For each invalid entry, a message is displayed telling them that it is wrong and the loop continues asking them for another integer. The messages are easy enough to display, but its the actual code to catch those specific errors that I'm having trouble with.

{
float sum = 0;
float x;
int check;
int l;
int i;
string Integer;

do
{
cout << "Please enter an integer: ";
getline(cin, Integer);
l = Integer.length();
for (i = 0; i < l; i++)
(isdigit(l));
check = x;

if (check != x || isalpha(x))
{
cout << "You did not enter an integer\n";
}
}
while(check != x || isalpha(x));


while ((isalpha(x)) || (!isdigit(x)));
{
cout <> x;
((isdigit(x)) || (!isalpha(x));
}

sum = sum + x;

while (x != 0)
{
cout <> x;
((isalpha(x)) || (!isdigit(x));
}
sum = sum + x;
}
cout << sum << endl;
return 0;
}

I'm really hoping you can help. This is the most important program to understand have done before the Final as the Final itself is similar to this program, not the same, but similar as far as I've had. We're starting the Final next week and I can only work on this program during school, which is 1:17pm - 2:37pm EST.

6 Post deleted by user.

7 Name: MaskSalesman : 2014-05-15 18:03 ID:09psbg5B [Del]

>>5 this is not valid c++ code. I don't mean to be insulting at all, but did you ever attend this class?!! I'm going to help you as best I can, but seriously...you should be spending ALOT of time improving your knowledge of c++. I'd recommend my good man, Bucky Roberts. Here's the url to the first of his C++ tutorial: http://www.youtube.com/watch?v=tvC1WCdV1XU

8 Name: MaskSalesman : 2014-05-15 18:58 ID:09psbg5B [Del]

Also, just to be clear, you only want input that is a decimal integer (0 - 9)?

9 Name: Shade !8NBuQ4l6uQ : 2014-05-15 21:48 ID:iwJg5lD1 [Del]

>>7 Everything in that coding, minus the 'isalpha' and 'isdigit', comes straight out of our textbook. There are 11 Units for C++, and the program I'm working on is the last one for Unit 9. So up until this point, the code I've learned has been working just fine. This program works to an extent, but without the error checking it doesn't work right. And yes, the only input that the program is supposed to accept would, for example, 1, 2, 2.0, 10, 25, 100, etc. But it cannot accept 2.9, 3.5, 10.10, etc. or letters/symbols. When 0 is entered, the loop stops and it does the calculations.

10 Name: MaskSalesman : 2014-05-15 22:25 ID:09psbg5B [Del]

I misunderstood your requirements then because I thought you were restricted to input only integers (0-9). In truth the code can be simpler then. I wrote up a working program though, with the requirements I had in mind, that you can look at to get some idea of how such 'error checking' could go. And the code you provided above does not even compile, though I suppose that's due to typing mistakes.

#include
#include

using namespace std;

int main(){

int num = -1, sum = 0;
int min = 0, max = 0;
int count = 0;
string str = "";
char c = 'a';

cout << "This program will calculate the average, minimum, and maximum "
<< "of a series of integers(1-9). Enter zero to terminate.\n"
<< "Please enter integers one at a time:" <> str;

if(str.length() < 2){
c = str[0];
num = c - 48;//1
}

if(num == 0)
break;

if((num 9)){
cout << "That input was not correct. "
<< "Please supply an integer(1-9), 0 to cancel." << endl;
}
else{ //2

sum+= num; //3
count++; //4

//5

if(count == 0){
min = num;
max = num;
}
else{
if(num max) max = num;
}
}
num = -1; //6
str = "";
}while(num != 0);

cout << "The average of this set is: " << (float)sum/count << endl;
cout << "The minimum of this set is: " << min << " and max: " << max << endl;

return 0;

}

11 Post deleted by user.

12 Name: MaskSalesman : 2014-05-15 22:38 ID:09psbg5B [Del]

The includes should be iostream and string.

Comments::
1) characters read by cin are treated as ascii characters, which
can be interpreted by their numerical (byte) values. The character '0' is decimal 48 thus, in this line usage of the - operator tells the compiler to treat it by its decimal representation.

2) The if statement preceeding this else did not come out correctly when posting here. The correct if statement should be (in pseudocode) [if num is less than 0 /or/ greater than 9]

3) That's a shorthand way of saying sum = sum + num;
4) convenient way of incrememting a variable by 1

5) Here we will check for minimum or maximum. For the first time, we will simply store the first integer that comes to us

6) It is good (and in this example necessary) to re-initialize variables that will be used again to some default value.

13 Name: Shade !8NBuQ4l6uQ : 2014-05-15 23:36 ID:iwJg5lD1 [Del]

>>12 Wow, thanks! I've been struggling with this project for a little over a week now, and you just made it seem simple(At least, from my perspective. I am a noob at coding.). And actually, the program that I wrote did compile and run, however, it usually broke when you put in something other than an integer. So yeah, a lot of work was needed on that. Thanks again for the help. Hopefully now I can get a good idea of how to do error checking and hopefully pass the final exam for the class. I'll take a look at the code you put up in detail during class tomorrow.

14 Name: MaskSalesman : 2014-05-16 11:26 ID:09psbg5B [Del]

>>13 No problem, 'coding' itself is the easy part. Understanding and recogizing all the possiblities for error is the crux. As I believe, starting with a working example is always the best way to proceed. Good luck :)