Question
I have a bug in my code I can't figure out. The bolded part is supposed to loop back to asking for the size of
I have a bug in my code I can't figure out. The bolded part is supposed to loop back to asking for the size of a vector if a non-integer is the input, and to the end if q or Q is for "quit". The code is working except for the q/Q part, when I input q/Q the output is the same as the other cin.fail() non-integer options where it asks for the vector size again, instead of jumping to the end of the main function and exiting. I have included the lab7functions.cpp below the main function if you want to test program but that is working fine.
#include
#include
#include
#include
#include "lab7functions.h"
using namespace std;
int main()
{
int size;
int item;
while (true)
{
cout << "Please enter the size of the sequence (type 'Q' or 'q' to quit): " << endl;
cin >> size;
cin.ignore(256, ' '); // get rid of either 256 characters or till reach the end of line, which comes first
// check if the user input is not valid
// your code starts here...
if (cin.fail())
{
char quit;
cin >> quit;
if ((quit == 'q')||(quit == 'Q'))
{
cout << "test" << endl;
break;
}
else
{
cin.clear();
cin.ignore(256, ' ');
cout << "Invalid input, please try again..." << endl;
continue;
}
}
// valid user input size, store into the vector
vector nums;
srand(time(0));
for (int i = 0; i < size; i++)
{
item = rand() % RANGE + 1; // each item is in the range of [1, RANGE]
cout << "Inserting..." << item << endl;
insertAtFront(nums, item);
cout << "The sequence: " << endl;
for (int i = 0; i < nums.size(); i++)
cout << nums[i] << " " ;
cout << endl;
}
// calculate the length of the longest run in the vector created above
int length = run_length(nums);
cout << "The run length of the sequence is: " << length << endl;
}
cout << "Thank you. Bye..." << endl;
return 0;
}
/* File: lab7function.cpp
* Course: CS216-00x
* Project: Lab 7
* Purpose: the implementation of two functions.
* Author: (your name)
*
*/
#include "lab7functions.h"
// insert the integer item from the second parametre into the vector
// and this item becomes the first data item in the vector
// call by reference so that the change is applied to the argument
void insertAtFront(vector& vecA, int item)
{
int pos = 0, newval = item;
vecA.push_back(0);
for (int i = vecA.size()-1; i > pos; i--)
{
vecA[i] = vecA[i-1];
}
vecA[pos] = newval;
}
// A run is a sequence of adjacent repeated data items.
// return the length of the longest run in a sequence stored in the vector nums, as the parameter
int run_length(vector nums)
{
int longest=0;
int length = 1;
for (int i = 1; i
{
if
(nums[i] == nums[i-1])
{
length ++;
}
else
{length =1; }
if
(length > longest)
{
longest = length;
}
}
return longest;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started