Solved

Needs help in loop

Posted on 2011-02-19
7
345 Views
Last Modified: 2012-05-11
Hi guys: Can any one plesae help me how to i stop the loop when the value in a char variable named anyletter is anything other than the letter Y (in either upper or lower case).

I try it first but dont know how to stop the loop. Thanks.

#include <iostream>
using namespace std;

int main()
{
      int number = 0;
      char anyletter = ' ';
      do
      {
            cout << number << endl;
            number +=1;
      }      while(number <=100 );
            system("pause");
            return 0;
}

0
Comment
Question by:mustish1
  • 4
  • 3
7 Comments
 
LVL 4

Expert Comment

by:andreizz
ID: 34934492
#include <iostream>
#include <ctype>
using namespace std;

int main()
{
      int number = 0;
      char anyletter = ' ';
      do
      {
            cout << number << endl;
            cout<<"Letter:";cin>>anyletter;
            if(isalpha(anyletter)&&(anyletter!='Y'||anyletter!='y'))
                 break;
            number +=1;
      }      while(number <=100 );
            system("pause");
            return 0;
} 

Open in new window


you need ctype for the isalpha test, u can remove that if you don't want to check it.
0
 

Author Comment

by:mustish1
ID: 34934501
I am using visual c++ 2010 it gives error on this line
#include <ctype>
0
 
LVL 4

Expert Comment

by:andreizz
ID: 34934515
#include <iostream>
#include <ctype.h>
using namespace std;

int main()
{
      int number = 0;
      char anyletter = ' ';
      do
      {
            cout << number << endl;
            cout<<"Letter:";cin>>anyletter;
            if(isalpha(anyletter)&&(anyletter!='Y'||anyletter!='y'))
                 break;
            number +=1;
      }      while(number <=100 );
            system("pause");
            return 0;
} 

Open in new window


or

#include <iostream>
using namespace std;

int main()
{
      int number = 0;
      char anyletter = ' ';
      do
      {
            cout << number << endl;
            cout<<"Letter:";cin>>anyletter;
            if(anyletter!='Y'||anyletter!='y')
                 break;
            number +=1;
      }      while(number <=100 );
            system("pause");
            return 0;
} 

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:mustish1
ID: 34934517
its not showing any thing.

errorcp.jpg
0
 
LVL 4

Expert Comment

by:andreizz
ID: 34934559
#include <iostream>
using namespace std;

int main()
{
      int number = 0;
      char anyletter = ' ';
      do
      {
            cout << number << endl;
            cout<<"Letter:";cin>>anyletter;
            if(anyletter!='Y'&&anyletter!='y')
                 break;
            number +=1;
      }      while(number <=100 );
            system("pause");
            return 0;
} 

Open in new window


sorry, there wasn't the || operator but &&
try this and renember to print numbers you have to input Y or y else it stops.
0
 
LVL 4

Accepted Solution

by:
andreizz earned 500 total points
ID: 34934566
#include <iostream>
using namespace std;

int main()
{
      int number = 0;
      char anyletter = ' ';
      do
      {
            cout << number << endl;
            cout<<"Letter:";cin>>anyletter;
            if(isalpha(anyletter)&&anyletter!='Y'&&anyletter!='y')
                 break;
            number +=1;
      }      while(number <=100 );
            system("pause");
            return 0;
} 

Open in new window


this is final try it.
0
 

Author Closing Comment

by:mustish1
ID: 34934588
Thanks a lot.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now