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

C++ How can I make the my code continue if the user types in Y for Yes and N for

ID: 3807710 • Letter: C

Question

C++

How can I make the my code continue if the user types in Y for Yes and N for no?

#include<iostream>

#include<cmath>

#include <conio.h>

#include "BubbleSort.h"

using namespace std;

int main ();

char ans;

bool IsInteger(float num){

//Floor function gives the largest integer value present in the number

   //Floor is from cmath library

   //floor(num)!=num if num is not integer

return std::floor(num) == num;

}

void BubbleSort(int arr[], int n){

int i,temp;

bool IsSorted;

do{

IsSorted = true;

n--;

for(i = 0; i < n; i++)

//Checking for ascending order

if(arr[i] > arr[i + 1]){

temp = arr[i];

arr[i] = arr[i + 1];

arr[i + 1] = temp;

IsSorted = false;

}

else;

}while(!IsSorted);

}

int main(){

int n = 25,i;

float num;

while(n > 20){

cout<<" Please Enter in how many numbers you wish to use: ";

cin>>n;

}

int arr[n];

//Reading integers

i = 0;

while(i < n){

cout<<" Please enter in integer" <<i<<endl;

//Using float variable because if user inputs floating point, C++ will not accept with integer variables

cin>>num;

//Checking for integer

if(!IsInteger((float)num))

{

cout<<" Sorry, not an integer. Enter again. ";

continue;

}

//Checking for whole number

else if(num < 0)

{

cout<<" Sorry, not a whole number. Enter again. ";

continue;

}

arr[i] = num;

i++;

}

//Displaying the array in actual order

cout<<" The integers you entered are in the order: ";

for(i = 0; i < n; i++)

cout<<arr[i]<<endl;

//Calling method for sorting

BubbleSort(arr, n);

//Displaying the sorted array

cout<<" The integers you entered in ascending order: ";

for(i = 0; i < n; i++)

cout<<arr[i]<<endl;

cout <<" Do you wish to continue? Y/y N/n ";

cin >> ans;

  

cout << " Would you like to continue? Type in (Y/y) for yes and (N/n) for no ";

  

  

  

  

  

  

return 0;

  

}

Explanation / Answer

Hi

I have modified the code and highlighted the code change below.


#include<iostream>
#include<cmath>
//#include <conio.h>
//#include "BubbleSort.h"
using namespace std;
int main ();






bool IsInteger(float num){

//Floor function gives the largest integer value present in the number
//Floor is from cmath library
//floor(num)!=num if num is not integer
return std::floor(num) == num;
}

void BubbleSort(int arr[], int n){
int i,temp;
bool IsSorted;
do{
IsSorted = true;
n--;
for(i = 0; i < n; i++)
//Checking for ascending order
if(arr[i] > arr[i + 1]){
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
IsSorted = false;
}
else;
}while(!IsSorted);
}

int main(){
int n = 25,i;
float num;
char ans='y';
while(ans=='y' || ans=='Y') {
n=25;

while(n > 20){
cout<<" Please Enter in how many numbers you wish to use: ";
cin>>n;
}
int arr[n];
//Reading integers
i = 0;
while(i < n){
cout<<" Please enter in integer" <<i<<endl;
//Using float variable because if user inputs floating point, C++ will not accept with integer variables
cin>>num;
//Checking for integer
if(!IsInteger((float)num))
{
cout<<" Sorry, not an integer. Enter again. ";
continue;
}
//Checking for whole number
else if(num < 0)
{
cout<<" Sorry, not a whole number. Enter again. ";
continue;
}
arr[i] = num;
i++;
}
//Displaying the array in actual order
cout<<" The integers you entered are in the order: ";
for(i = 0; i < n; i++)
cout<<arr[i]<<endl;
//Calling method for sorting
BubbleSort(arr, n);
//Displaying the sorted array
cout<<" The integers you entered in ascending order: ";
for(i = 0; i < n; i++)
cout<<arr[i]<<endl;
cout << " Would you like to continue? Type in (Y/y) for yes and (N/n) for no ";
cin >> ans;
}   
  
  
  
return 0;
  

}
Output:

sh-4.2$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                                                                     

sh-4.2$ main                                                                                                                                                                                                                                                             

                                                                                                                                                                                                                                                                         

Please Enter in how many numbers you wish to use:                                                                                                                                                                                                                        

2                                                                                                                                                                                                                                                                        

                                                                                                                                                                                                                                                                         

Please enter in integer0                                                                                                                                                                                                                                                 

1                                                                                                                                                                                                                                                                        

                                                                                                                                                                                                                                                                         

Please enter in integer1                                                                                                                                                                                                                                                 

2                                                                                                                                                                                                                                                                        

                                                                                                                                                                                                                                                                         

The integers you entered are in the order:                                                                                                                                                                                                                               

1                                                                                                                                                                                                                                                                        

2                                                                                                                                                                                                                                                                        

                                                                                                                                                                                                                                                                         

The integers you entered in ascending order:                                                                                                                                                                                                                             

1                                                                                                                                                                                                                                                                        

2                                                                                                                                                                                                                                                                        

Would you like to continue? Type in (Y/y) for yes and (N/n) for no y                                                                                                                                                                                                    

                                                                                                                                                                                                                                                                         

Please Enter in how many numbers you wish to use:                                                                                                                                                                                                                        

1                                                                                                                                                                                                                                                                        

                                                                                                                                                                                                                                                                         

Please enter in integer0                                                                                                                                                                                                                                                 

4                                                                                                                                                                                                                                                                        

                                                                                                                                                                                                                                                                         

The integers you entered are in the order:                                                                                                                                                                                                                               

4                                                                                                                                                                                                                                                                        

                                                                                                                                                                                                                                                                         

The integers you entered in ascending order:                                                                                                                                                                                                                             

4                                                                                                                                                                                                                                                                        

Would you like to continue? Type in (Y/y) for yes and (N/n) for no n

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