# while() loop isn't working right..

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-29-2002
Captain Penguin
while() loop isn't working right..
simple question methinks, but its perplexing me neverthe less:

Code:

```// By Will Herrick // takes two numbers and sees if they are factors of each other. // also some practice on error-handling. // last updated 9-27-02 (doesn't work quite yet!) #include <iostream> #include <cstdlib> #include <cstring> using namespace std; int checkSize(int); int checkNumeric(char[256]); void factor(int,int); int getNumber(); int main() {         int ai;         int bi;         int exit;         char quit;   exit = 0;                 while(exit == 0)         {           cout << "Enter a:";           ai = getNumber();           cout << endl << "Enter b:";           bi = getNumber();           cout << endl;           factor(ai,bi);                   cout << "Again? (Y/N) ";           cin >> quit;     if(quit == 'y' || quit == 'Y')     {       exit = 0;     }     else exit = 1;   }     return 0; } int checkSize(int a) {         if(a > 32766)         {                 return 0;         }         if(a < 32766) return 1; } int checkNumeric(char a[6]) {         int x;         int i;         int marker;         int length;         length = strlen(a);                 for(x = 0; x < length; x++)         {                 i = a[x];                 if((i < 48 || i > 57) && i != '\0')                 {                         marker = 1;                 }         }         if(marker == 1) return 0;         if(marker != 1) return 1; } void factor(int a, int b) {                 if(a % b == 0) cout << b << " is a factor of " << a << endl;                 if(b % a == 0) cout << a << " is a factor of " << b << endl;                 if(a % b != 0 && b % a != 0) cout << "Neither number is a factor of the other." << endl; } int getNumber() {         char a[256];         char *p;         int r;         int isNum;         int isSize;                 cout << endl << "Enter the NUMBER: ";         cin.get(a,256);         cin.ignore(80, '\n');         isNum = checkNumeric(a);         if(isNum == 1)         {                 p = &a[0];                 r = atoi(p);                 isSize = checkSize(r);                 if(isSize == 1) return r;                 else                 {                         r = getNumber();                         return r;                 }         }         if(isNum == 0)         {                 r = getNumber();                 return r;         } }```
The problem lie in the program loop. If the user presses y or Y to do it "again" it just does this:

Code:

```Enter a: Enter the NUMBER: Enter b: Enter the NUMBER: Press Enter to continue!```
without even taking anymore user input.

Also, if I use "cin.get(quit); cin.ignore(80,'\0');", the if(quit == 'y' || quit == 'Y') statement doesn't work.

Any tips?
• 09-29-2002
Sebastiani
Try using "getch()" and see if the problem persists.
• 09-29-2002
Mister C
Why don't you use the bool data type instead of using integers like c

Code:

`bool  exit;`
Since you are writing C++ -use C++ not C (Although it should still work).
Mr. C.
• 09-29-2002
Waldo2k2
try cin.clear()...clears the input buffer.
also, should be
Code:

```if (quit[0]=='y'||quit[0]=='Y') { ... }```
this way even if your user types yes or yeah or yack it will still accept it as y. in your method if the user was trying to type y and his finger slipped and he type yy or something, it wouldn't work.
• 09-29-2002
Waldo2k2
also, does getNumber(ai) work as well? i believe that using the equality operator may be throwing you off. give it a whirl.
• 09-29-2002
Captain Penguin
Hey those tips helped with another goal of the program, but it still doesn't loop back properly.

What exactly do you mean by the assignment operator might be screwing it up, Waldo2k2?
I can't do getNumber(ai) since getNumber returns an integer and doesn't take any input. I could probably redesign it so it works more elegantly, if you think that would help.

I made a couple changes to improve it, but it still doesn't loop back properly. Here are the changes:

Code:

```int main() {         int ai;         int bi;         bool exit;         char quit[256];   exit = 0;                 while(exit == 0)         {           cout << "Enter a:";           ai = getNumber();           cout << endl << "Enter b:";           bi = getNumber();           cout << endl;           factor(ai,bi);                   cout << "Again? (Y/N) ";           cin.get(quit,256);     cin.clear();     if(quit[0] == 'y' || quit[0] == 'Y')     {       exit = 0;     }     else exit = 1;   }     return 0; }```
here is the output:

Code:

```Enter a: Enter the NUMBER: y Enter the NUMBER: 4 Enter b: Enter the NUMBER: 8 4 is a factor of 8 Again? (Y/N) yeasurewhynot Enter a: Enter the NUMBER: Enter b: Enter the NUMBER: Press Enter to continue!```
anything else? Its odd that a simple loop isn't working properly..
• 09-29-2002
Mister C

Code:

```  exit = 0;                 while(exit == 0)         {           cout << "Enter a:";           ai = getNumber();           cout << endl << "Enter b:";           bi = getNumber();           cout << endl;           factor(ai,bi);                   cout << "Again? (Y/N) ";           cin.get(quit,256);     cin.clear();     if(quit[0] == 'y' || quit[0] == 'Y')     {       exit = 0;     }     else exit = 1;   }```
with the while statement you loop while the loop is true so try.

Code:

``` exit = false; while (!false) { ... if (....)   exit = false; else   exit = true; }```

also why dont you use the built in c++ functions toupper/tolower and there is one I believe that checks to see if a character is numeric or not.

Mr. C
• 09-29-2002
Captain Penguin
?1/4?~?i]Originally posted by Mister C [/i]

Code:

```  exit = 0;                 while(exit == 0)         {           cout << "Enter a:";           ai = getNumber();           cout << endl << "Enter b:";           bi = getNumber();           cout << endl;           factor(ai,bi);                   cout << "Again? (Y/N) ";           cin.get(quit,256);     cin.clear();     if(quit[0] == 'y' || quit[0] == 'Y')     {       exit = 0;     }     else exit = 1;   }```
with the while statement you loop while the loop is true so try.

Code:

``` exit = false; while (!false) { ... if (....)   exit = false; else   exit = true; }```

also why dont you use the built in c++ functions toupper/tolower and there is one I believe that checks to see if a character is numeric or not.

Mr. C
[/QUOTE]

hey thanks I'll try that, but I'm pretty sure its doing the exact same thing, just with different notation.

As for why I didn't use a C++ function to check if a char string is numeric.. well I didn't know about it (and I still don't). Plus I wanted to try it myself...
• 10-01-2002
Captain Penguin
OK well still not working. This is freaking weird.. its just a simple loop, yet its not working. there must be something wrong with my functions!

here's the current state :

Code:

```// By Will Herrick // takes two numbers and sees if they are factors of each other. // also some practice on error-handling. // last updated 9-29-02 (doesn't work quite yet!) #include <iostream> #include <cstdlib> #include <cstring> using namespace std; int checkSize(int); int checkNumeric(char[256]); void factor(int,int); void getNumber(int &i); int main() {                 bool exit;         char quit[256];   exit = false;                 while(!false)         {     int ai;           int bi;         cout << "Enter a:";     getNumber(ai);           cout << endl << "Enter b:";     getNumber(bi);           cout << endl;           factor(ai,bi);                   cout << "Again? (Y/N) ";           cin.get(quit,256);     cin.clear();     if(quit[0] == 'y' || quit[0] == 'Y')     {       exit = false;     }     else exit = true;   }       return 0; } int checkSize(int a) {         if(a > 32766)         {                 return 0;         }         if(a < 32766) return 1; } int checkNumeric(char a[6]) {         int x;         int i;         int marker;         int length;         length = strlen(a);                 for(x = 0; x < length; x++)         {                 i = a[x];                 if((i < 48 || i > 57) && i != '\0')                 {                         marker = 1;                 }         }         if(marker == 1) return 0;         if(marker != 1) return 1; } void factor(int a, int b) {                 if(a % b == 0) cout << b << " is a factor of " << a << endl;                 if(b % a == 0) cout << a << " is a factor of " << b << endl;                 if(a % b != 0 && b % a != 0) cout << "Neither number is a factor of the other." << endl; } void getNumber(int &ri) {         char a[256];         char *p;         int isNum;         int isSize;                 cout << endl << "Enter the NUMBER: ";         cin.get(a,256);         cin.ignore(80, '\n');         isNum = checkNumeric(a);         if(isNum == 1)         {                 p = &a[0];                 ri = atoi(p);                 isSize = checkSize(ri);                 if(isSize != 1)                 {                         getNumber(ri);                 }         }         if(isNum == 0)         {                 getNumber(ri);         } }```
The output is the same as before.
Can anyone suggest something? Thanks
• 10-01-2002
Waldo2k2
ok first of all when i compiled it i received the warnings that not all the logic paths you take in checkSize and checkNumeric return a value, that might mess you up....
and one fatal error i found is that since the loop doesn't re-execute it attempts to divide by zero since you have no values for the integers. Also, make your variables global, i;m not sure since you're logic is kind of funky but that may be messing you up.
• 10-01-2002
Captain Penguin
Quote:

Originally posted by Waldo2k2
ok first of all when i compiled it i received the warnings that not all the logic paths you take in checkSize and checkNumeric return a value, that might mess you up....
and one fatal error i found is that since the loop doesn't re-execute it attempts to divide by zero since you have no values for the integers. Also, make your variables global, i;m not sure since you're logic is kind of funky but that may be messing you up.

I fixed the return paths, but those shouldn't make a difference.

And what do you mean the loop doesn't re-execute? It says while(!false) go through the loop until the person does say quit. if someone doesn't say quit, exit is !false and thus it goes through the loop again. Or at least thats what its SUPPOSED to be doing...

I bet I'm just an idiot and their is some major flaw in my program that is really obvious.. but I just don't see it.
• 10-01-2002
Waldo2k2
ok, here's my suggestion:
START OVER FROM SCRATCH

rethink your loops, i believe a do-while would be a better loop to use, and also, change exit (exit is a variable used in stdlib.h) i personally like bool goAgain;
Code:

```do { ... } while (goAgain==TRUE);```
remove all your functions and just work in main, make all the variables global, just make something that's simple, it'll be much easier to debug a simple program than a hard one...i personally never make ANY functions until the program works without bugs, then i just have to split up main, no real headaches. good luck, let us know what you come up with.
• 10-01-2002
Sebastiani
Quote:

while(!false)

Souldn't that be:

while(!exit)
• 10-01-2002
Sebastiani
I should have been more clear earlier. I suggested using getch() for "quit". The reason is that cin can mess with the input stream at times, inserting line feeds and such. Also, if you don't enter anything, cin will often crash the computer! So stick to getch() for chars, and fgets() for char arrays. I inserted fgets() into your code and it worked fine, by the way...
• 10-01-2002
Captain Penguin
Quote:

Originally posted by Sebastiani
Souldn't that be:

while(!exit)

same result..

do my getNumber, checkSize, and checkNumeric functions just suck or something?
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last