+2 votes
81 views

Hi, I am very new to c++ and i want some help in c++. Actually, I want a program that can run infinitly until user enters a specific number or character.
My Program is:

#include<iostream>
using namespace std;
main(){
    string name;
    cout<<"Please enter a name: ";
    cin>>name;

    if(name=="athar")
        {
            cout<<endl;
            cout<<"Name: Athar"<<endl;
            cout<<"Village: Kainth"<<endl;
            cout<<"Age: 18"<<endl;
        }
     else if(name=="Laiba")
        {
            cout<<endl;
            cout<<"Name: Laiba"<<endl;
            cout<<"Village: Lahore"<<endl;
            cout<<"Age: 17"<<endl;
        }
        else if(name=="sadia")
        {
            cout<<endl;
            cout<<"Name: Sadia"<<endl;
            cout<<"Village: Shakargarh"<<endl;
            cout<<"Age: 17"<<endl;
        }
    }

Thanks in advance...

by (3.3k points)   | 81 views

3 Answers

+1 vote

Dear Athar, You can use while loop in your program to make it infinite until user enters a specific character or number. Try this code:

#include<iostream>
using namespace std;
main(){
    bool isTrue = true;
    string name;
    while(isTrue)
    {
    cout<<"Please enter a name (x for exit): ";
    cin>>name;

    if(name=="athar")
        {
            cout<<endl;
            cout<<"Name: Athar"<<endl;
            cout<<"Village: Kainth"<<endl;
            cout<<"Age: 18"<<endl;
        }
     else if(name=="Laiba")
        {
            cout<<endl;
            cout<<"Name: Laiba"<<endl;
            cout<<"Village: Lahore"<<endl;
            cout<<"Age: 17"<<endl;
        }
        else if(name=="sadia")
        {
            cout<<endl;
            cout<<"Name: Sadia"<<endl;
            cout<<"Village: Shakargarh"<<endl;
            cout<<"Age: 17"<<endl;
        }
        else if(name=="x")
        {
            isTrue = false;
        }
      }
    }
by (1.3k points)  
+1 vote

#include<iostream>
using namespace std;
main(){
    string name;
    cout<<"Please enter a name: ";
    cin>>name;
    if(name=="athar")
        {
            cout<<endl;
            cout<<"Name: Athar"<<endl;
            cout<<"Village: Kainth"<<endl;
            cout<<"Age: 18"<<endl;
        }
     else if(name=="Laiba")
        {
            cout<<endl;
            cout<<"Name: Laiba"<<endl;
            cout<<"Village: Lahore"<<endl;
            cout<<"Age: 17"<<endl;
        }
        else if(name=="sadia")
        {
            cout<<endl;
            cout<<"Name: Sadia"<<endl;
            cout<<"Village: Shakargarh"<<endl;
            cout<<"Age: 17"<<endl;
        }
    }
by (160 points)  
edited by
Please try to understand first how does "Markdown Editor" work and how to post coding with it. Otherwise, we may have to remove bad formatted matetial!
Thanks!
+1 vote

try this

main(){

int op;

while(true){
  cout<<"Choose option 0-9: ";
  cin>>op;
  if(op==0)
      break;
  else
    {
     ..... do something....
    }
 }
by (3.3k points)  

Related questions

+1 vote
2 answers
+2 votes
1 answer
+2 votes
2 answers
asked Jun 4 by toheed (1.4k points) | 93 views
+2 votes
1 answer
asked Jun 5 by toheed (1.4k points) | 17 views
+3 votes
0 answers
+2 votes
1 answer
0 votes
1 answer




Welcome to Meansflow - Where Developers and Students Learn, Share, & Build Careers, where you can ask questions and receive answers from other members of the community.
118 questions
90 answers
43 comments
110 users