Thread: do-while isn't working help

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    27

    Question do-while loop isn't working help

    please help me...the do-while loop will not work
    It will go through properly the first time and then the "Want to enter another comes up but if you enter "1" it will ask again "Want to enter another" it won't go through the program...but if you enter "2" it will exit the program
    Code:
    int main()
    {
    	int again;	//user input which determines if they want to enter another sentence
        char sentence[81];	//user inputs sentence
    	
    	do
    	{
    		system("cls");
    		cout << "Do you want to enter a word or sentence up to 80 characters: " << endl;
    		gets(sentence);
    
    		encrypt(sentence);		//calls function encrypt
    		decrypt(sentence);		//calls function decrypt
    	
    		cout << "Want to enter another? (press 1 - Yes, 2 - No)" << endl;
    		cin >> again;
    		
    	}
    	while (again == 1);
    	return 0;
    }
    
    void encrypt(char sentence[])
    {
    	for (int i = 0; i < strlen(sentence); i++)
    	{
    		sentence[i] = sentence[i] + 3;
    
    	}
    	cout << sentence << endl;
    }
    
      
    void decrypt(char sentence[])
    {
    	for (int i = 0; i < strlen(sentence); i++)
    	{
    		sentence[i] = sentence[i] - 3;
    	}
    	cout << sentence << endl;
    }
    Last edited by student2005; 01-19-2004 at 05:59 PM.

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    do
    {
       system("cls");
       cout << "Do you want to enter a word or sentence up to 80 characters: " << endl;
       cin.getline(sentence, 81);  // don't use gets() (see faq ) 
    
       encrypt(sentence);		//calls function encrypt
       decrypt(sentence);		//calls function decrypt
    	
       cout << "Want to enter another? (press 1 - Yes, 2 - No)" << endl;
       cin >> again;
       cin.ignore();       // this code seems to fix it
    		
    } while (again == 1);
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    27

    Thank you Thank you

    Thank you very much it works great now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM