while() loop isn't working right..

This is a discussion on while() loop isn't working right.. within the C++ Programming forums, part of the General Programming Boards category; simple question methinks, but its perplexing me neverthe less: Code: // By Will Herrick // takes two numbers and sees ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    244

    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?
    Last edited by Captain Penguin; 09-29-2002 at 08:55 AM.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Try using "getch()" and see if the problem persists.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    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.

  4. #4
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    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.
    PHP and XML
    Let's talk about SAX

  5. #5
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    also, does getNumber(ai) work as well? i believe that using the equality operator may be throwing you off. give it a whirl.
    PHP and XML
    Let's talk about SAX

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    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..

  7. #7
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Hey, your loop:

    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;
    
    }
    change your logic!!

    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

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    ?1/4?~?i]Originally posted by Mister C [/i]
    Hey, your loop:

    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;
    
    }
    change your logic!!

    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...

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    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. #10
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    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.
    PHP and XML
    Let's talk about SAX

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    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.

  12. #12
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    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.
    PHP and XML
    Let's talk about SAX

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    while(!false)
    Souldn't that be:

    while(!exit)
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  15. #15
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Originally posted by Sebastiani
    Souldn't that be:

    while(!exit)
    same result..

    do my getNumber, checkSize, and checkNumeric functions just suck or something?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "try again" loop not working
    By scwizzo in forum Game Programming
    Replies: 5
    Last Post: 04-01-2007, 09:56 PM
  2. my do while loop not working
    By rodrigorules in forum C Programming
    Replies: 12
    Last Post: 09-07-2005, 06:52 PM
  3. Replies: 6
    Last Post: 07-19-2005, 01:03 PM
  4. EOF not working in a loop
    By Malabux in forum C++ Programming
    Replies: 3
    Last Post: 10-12-2003, 06:28 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21