Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 10th, 2006, 01:03 AM
Registered User
 
Join Date: Jun 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Pleeeeeeeeeeeeeez fix it!!!

Hi guys, i m having a problem in the following source code. Please see if you can help.Thanks a lot.

#include<iostream>
#include<iomanip>
#include<cctype>
using namespace std;
int main()
{
    char response='y';
    int num,sum=0;
    do{
        cout<<"Enter a number: ";
        cin>>num;
        sum+=num;
        cout<<"Do you want to enter another number?(y/n) ";
        cin>>response;
        response=tolower(response);
        while((response!='y')||(response!='n'))
        {
            cout<<endl<<"Please enter y to continue or n to end. ";
            cin>>response;
        }
    }while(tolower(response=='y'));

    cout<<endl<<"The sum of numbers that you entered is "<<sum<<"."<<endl;

    return 0;
}

When executed, the program enters an infinite loop (i m clueless about that!!).
This was compiled on VC++ 6.0. Make appropriate changes for your compiler.
Pleeeeeeeeeeeeeeeeeeeeeeez help!

Reply With Quote
  #2 (permalink)  
Old June 11th, 2006, 12:23 AM
Registered User
 
Join Date: Jun 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this:

while((response!='y')&&(response!='n'))
Reply With Quote
  #3 (permalink)  
Old June 11th, 2006, 03:52 AM
Registered User
 
Join Date: Nov 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i beleive that the problem is in the outer while loop, the condition
while(tolower(response=='y')) will always remain true.
coz response=='y' is a logical statement that would return 0 or 1 to the tolower function and that would always remain true may be it will work if you make it

while(tolower(response)=='y');

Reply With Quote
  #4 (permalink)  
Old June 12th, 2006, 01:41 AM
Authorized User
 
Join Date: Mar 2005
Posts: 58
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Alan-LB Send a message via Yahoo to Alan-LB
Default

Your statement
"while((response!='y')||(response!='n'))"
says while response is not equal to 'y' OR response is not equal to 'n' - this will cause an infinite loop since the condition will always be true!

Change the OR (||) tp AND (&&)

After the second "cin >> response;" put "response = tolower(response); then the second while statement becomes "while (response == 'y');"

Alan




Reply With Quote
  #5 (permalink)  
Old August 22nd, 2006, 01:55 AM
Registered User
 
Join Date: Aug 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

simply write while(response == 'y') in the outer while loop, it should work..

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Code fix HemaRaj Javascript 5 January 31st, 2008 04:41 PM
please fix this function keyvanjan ASP.NET 2.0 Professional 4 September 18th, 2007 08:01 AM
Fix Error sgafar ASP.NET 1.0 and 1.1 Basics 0 May 30th, 2007 10:22 AM
Need xref Fix ~Bean~ SQL Language 2 October 5th, 2005 02:45 PM
Help! How do I fix this? missusfinz Crystal Reports 1 April 1st, 2005 12:27 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.