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

The following code is for a program that reads user input. I would like to make

ID: 3860612 • Letter: T

Question

The following code is for a program that reads user input. I would like to make changes to the code so that "Q"  and "q" would be the input that would terminate input instead of "0". How do i go about accommodating these changes in Visual Studio?

#include <iostream>
#include <string>
using namespace std;

//create initial array of 10 size in heap
int *arr = new int[10];

//As this is function syntax is given so I used integer array but you are assigning double value in it
//if you want to store double value then change it double type array.
int* read_data(int& size)
{
//cout << size << endl;
int i=0;
double in;

//if array size is greater then we need to double the array size and copy elements in new array ans assigned
// it back to arr
if(size >= 10)
{
size = size*2;
//cout << "new size: " << size << endl;
int *arrNew = new int[size];
for(i=0;i<size/2;i++)
arrNew[i] = arr[i];
  
delete[] arr; //to remove memory leak we have to delete previous used arr
arr = arrNew; // allocate arr with new array
}
  
//to take input from user
while(1)
{
cout << "please enter number to insert in array or 0 to quit"<<endl;
cin >> in;
if(in == 0) // if input is 0 it will terminates
break;
else
arr[i++] = in;

}
size = i; //assign size to array
return arr;
}

int main()
{
int size=0;
int *a = read_data(size);
cout << "size of array :" << size << endl;
cout << "array elements :" ;
for(int i=0;i<size;i++)
cout << a[i] << " ";
cout << endl;
  
a = read_data(size);
cout << "New size of array size :" << size << endl;
cout << "array elements: " ;
for(int i=0;i<size;i++)
cout << a[i] << " ";
  
cout << endl;
return 0;
}

Explanation / Answer

NOTE: I have changed your code so that it terminates when user enters q or Q. Please check and let me know if you face any issues. I will revert back within 24 hours.

Code:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

//create initial array of 10 size in heap
int *arr = new int[10];

//As this is function syntax is given so I used integer array but you are assigning double value in it
//if you want to store double value then change it double type array.
int* read_data(int& size)
{
//cout << size << endl;
int i=0;
string in;

//if array size is greater then we need to double the array size and copy elements in new array ans assigned
// it back to arr
if(size >= 10)
{
size = size*2;
//cout << "new size: " << size << endl;
int *arrNew = new int[size];
for(i=0;i<size/2;i++)
arrNew[i] = arr[i];

delete[] arr; //to remove memory leak we have to delete previous used arr
arr = arrNew; // allocate arr with new array
}

//to take input from user
while(1)
{
cout << " please enter number to insert in array or Q/q to quit: ";
getline(cin, in);
if(in[0] == 'q' || in[0] == 'Q') // if input is 0 it will terminates
break;
else{
int val = stoi(in);
arr[i++] = val;
}
}
size = i; //assign size to array
return arr;
}

int main()
{
int size=0;
int *a = read_data(size);
cout << "size of array :" << size << endl;
cout << "array elements :" ;
for(int i=0;i<size;i++)
cout << a[i] << " ";
cout << endl;

a = read_data(size);
cout << "New size of array size :" << size << endl;
cout << "array elements: " ;
for(int i=0;i<size;i++)
cout << a[i] << " ";

cout << endl;
return 0;
}

Code output screenshot:
https://pasteboard.co/GCEtXl1.png

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