Solved

Needs help in loop

Posted on 2011-02-19
7
347 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

809 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