Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,115 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,009 people online right now. Registration is fast and FREE... Join Now!




error check in c++

 
Reply to this topicStart new topic

error check in c++

Katecbu
post 6 Oct, 2008 - 12:14 PM
Post #1


New D.I.C Head

*
Joined: 6 Oct, 2008
Posts: 2





Probably a really simple question but i can't get it...
I need to right a program which determines if any integer is odd or even, and I got that part, but the integer has to be between 1 an 32767. If it's not the program needs to output "number not acceptable" and the program needs to terminate.... I can make the program run where it does say "number not acceptable" but it still displays if the number is odd or ever... so this is what i have

CODE

int main ()
{
    int x;
    
    cout << "Enter any integer: \n";
    cin >> x;
    {
    if (1 > x)
    cout << "Number is outside the acceptable range of values.\n\n";
    if (x > 32767)
    cout << "Number is outside the acceptable range of values.\n\n";

    if(x % 2 == 0)
        cout << "The number is even.\n\n";
    else
        cout << "The number is odd.\n\n";
    }

    return 0;
}
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 6 Oct, 2008 - 12:31 PM
Post #2


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


First off, you don't include anything! ohmy.gif

Secondly, try the exit() function to exit within your if statement (if the number is outside of the bounds)

NOTE: This method uses the exit() function, which (I believe) is often found in <cstdlib> but my compiler (MinGW) doesn't require it. It's best to include the libraries as good practice though. smile.gif
cpp
#include <iostream>
#include <cstdlib> // for the exit() function
using namespace std;

int main () {
int x;

cout << "Enter any integer: \n";
cin >> x;

if (x < 1 || x > 32767) {
cout << "Number is outside the acceptable range of values.\n\n";
exit(EXIT_FAILURE);
}

if(x % 2 == 0)
cout << "The number is even.\n\n";
else
cout << "The number is odd.\n\n";

cin.get(); // you wasn't pausing! how is the user supposed to read before exiting?!
return EXIT_SUCCESS;
}

Hope this helps smile.gif

EDIT:
Alternatively, you could simply use if/else if/else structure, like so:
cpp
#include <iostream>
using namespace std;

int main () {
int x;

cout << "Enter any integer: \n";
cin >> x;

if (x < 1 || x > 32767)
cout << "Number is outside the acceptable range of values.\n\n";

else if(x % 2 == 0)
cout << "The number is even.\n\n";
else
cout << "The number is odd.\n\n";

cin.get(); // you wasn't pausing! how is the user supposed to read before exiting?!
return EXIT_SUCCESS;
}


I think that's what your instructor is looking for, but exit() might make you look like you read into it a bit cool.gif
User is offlineProfile CardPM

Go to the top of the page

Katecbu
post 7 Oct, 2008 - 12:12 PM
Post #3


New D.I.C Head

*
Joined: 6 Oct, 2008
Posts: 2

thank you very much smile.gif the second one was great, i was just getin my if, else, else if's all wrong....and your right, we haven't covered the end thing yet, might make it look bad lol...
about the includes, had them in my program, just didn't paste them, sorry
but yes smile.gif thanks a million!
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 10:14AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month