Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Okay so I made a c++ program where the program prompts a user to enter a series

ID: 3544790 • Letter: O

Question

Okay so I made a c++ program where the program prompts a user to enter a series of words (strings) representing binary numbers (0s and 1s) outputting how many 1s there are. Everything works perfectly, displaying everything I want to be displayed. The program then terminates right after, and I don't want it to terminate . I want the program to keep asking the question until the user types " *** ", meaning they want to stop, terminating the program. How do I do this? Here is my program:


#include <iostream>
#include <string>
#include<conio.h>
using namespace std;

int main()
{
string inString; //user input string
//to count number of 1s
int count=0;
//to check if number is invalid
int check=0;

int i=0;
//banner
//"Welcome to Bob's Binary Number Evaluator" welcome message
cout<<"1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1"<<endl<<endl;
cout<<"Welcome to Bob's Binary Number Evaluator"<<endl<<endl;
cout<<"1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1"<<endl<<endl;
//getting user input
cout<<endl<<"Please enter a binary number and I will tell you how many 1s there are: ";
cin>>inString;
//loop until the length of string
while(i<inString.length())
{
//if the current bit is not 1 or 0
if(inString[i]!='1' && inString[i]!='0')
{
cout<<endl<<"That is not a valid binary number"<<endl;
check=1; //invalid number
break; //break loop
}
else if(inString[i]=='1')
{
count++; //counting 1s
}
i++;
}
//displaying result
if(check==0)
cout<<endl<<"There are "<<count<<" 1s in binary number "<<inString<<endl;
cout<<"Thank you for using Bob's Binary Number Evaluator"<<endl<<endl;
getch();
return 0;
}


Explanation / Answer


#include <iostream>

#include <string>

#include<conio.h>

using namespace std;


int main()

{

string inString; //user input string

//to count number of 1s

int count=0;

//to check if number is invalid

int check=0;


int i=0;

//banner

//"Welcome to Bob's Binary Number Evaluator" welcome message

cout<<"1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1"<<endl<<endl;

cout<<"Welcome to Bob's Binary Number Evaluator"<<endl<<endl;

cout<<"1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1"<<endl<<endl;

//getting user input

while(1)

{

cout<<endl<<"Please enter a binary number and I will tell you how many 1s there are: ";

cin>>inString;

//loop until the length of string


int len=inString.length();

if((len==3)&&(inString[0]=='*')&&(inString[1]=='*')&&(inString[2]=='*'))

break;

while(i<len)

{

//if the current bit is not 1 or 0

if(inString[i]!='1' && inString[i]!='0')

{

cout<<endl<<"That is not a valid binary number"<<endl;

check=1; //invalid number

break; //break loop

}

else if(inString[i]=='1')

{

count++; //counting 1s

}

i++;

}

//displaying result

if(check==0)

cout<<endl<<"There are "<<count<<" 1s in binary number "<<inString<<endl;


}

cout<<"Thank you for using Bob's Binary Number Evaluator"<<endl<<endl;

getch();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote