Solved

Needs help in loop

Posted on 2011-02-19
7
350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.

737 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