We’d love to hear any feedback or comment from you!

###### Engineering · Computer Science
Question details

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<<"\nPlease Enter in how many numbers you wish to use: \n";

cin>>n;

}

int arr[n];

i = 0;

while(i < n){

//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<<"\nSorry, not an integer. Enter again.\n";

continue;

}

//Checking for whole number

else if(num < 0)

{

cout<<"\nSorry, not a whole number. Enter again.\n";

continue;

}

arr[i] = num;

i++;

}

//Displaying the array in actual order

cout<<"\nThe integers you entered are in the order: \n";

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

cout<<arr[i]<<endl;

//Calling method for sorting

BubbleSort(arr, n);

//Displaying the sorted array

cout<<"\nThe integers you entered in ascending order: \n";

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

cout<<arr[i]<<endl;

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

cin >> ans;

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

return 0;

}