Question

In: Computer Science

using this code under, I want when the user input only letter q an error message...

using this code under, I want when the user input only letter q an error message appears that there is no second smallest, how I can do that?

#include <iostream>

#include <sstream>

#include <vector>

#include <algorithm>

using namespace std;

int secondSmallestNumber(vector<int> &I);

int main() {

   cout << "Enter the numbers in random order: (close by entering q)" << endl;

   vector<int> I;

   int input;

   stringstream ss;

   string str;

   bool flag = false;

   while (!flag) {

       getline(cin, str);

       ss.clear();

       ss << str;

       while (ss >> input || !ss.eof()) {

           if (ss.fail()) {

               flag = true;

               break;

           }

           I.push_back(input);

       }

   }

   int result;

   try {

       result = secondSmallestNumber(I);

       cout << "The second smallest number is: " << result << endl;

   }

   catch (const runtime_error& e ) {

       cout << "error: no second smallest" << endl;

   }

   return 0;

}

int secondSmallestNumber(vector<int> &I){

   std::sort(I.begin(), I.end());

   int number = I.at(0);

   for (int i = 0; i < I.size() - 1; i++) {

       if (I.at(i + 1) > number) {

           return I.at(i + 1);

       }

   }

   throw runtime_error("error");

}


Solutions

Expert Solution

#include <iostream>

#include <sstream>

#include <vector>

#include <algorithm>
#include<stdexcept>

using namespace std;

int secondSmallestNumber(vector<int> &I);

int main() {

cout << "Enter the numbers in random order: (close by entering q)" << endl;

vector<int> I;

int input;

stringstream ss;

string str;

bool flag = false;

getline(cin, str);//ask outside while loop

if(str == "q"){// check if starting character is q
       cout<<"there is no second smallest";
       return 0;  
}

while (!flag) {

getline(cin, str);
      
ss.clear();

ss << str;
      
while (ss >> input || !ss.eof()) {

if (ss.fail()) {

flag = true;

break;

}

I.push_back(input);

}

}

int result;

try {

result = secondSmallestNumber(I);

cout << "The second smallest number is: " << result << endl;

}catch(const runtime_error& e ) {

cout << "error: no second smallest" << endl;

}

return 0;

}

int secondSmallestNumber(vector<int> &I){

std::sort(I.begin(), I.end());

int number = I.at(0);

for (int i = 0; i < I.size() - 1; i++) {

if (I.at(i + 1) > number) {

return I.at(i + 1);

}

}

throw runtime_error("error");

}

/* OUTPUT */

/* PLEASE UPVOTE */


Related Solutions

(CODE IN PYTHON) Program Input: Your program will display a welcome message to the user and...
(CODE IN PYTHON) Program Input: Your program will display a welcome message to the user and a menu of options for the user to choose from. Welcome to the Email Analyzer program. Please choose from the following options: Upload text data Find by Receiver Download statistics Exit the program Program Options Option 1: Upload Text Data If the user chooses this option, the program will Prompt the user for the file that contains the data. Read in the records in...
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input...
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input system for a university student, where they put the season and year of when they started their uni course. For example the system will ask "What year did you start your degree?", the user will input "Autumn/2022" as a string. Now from a string format as shown, it should take that user input and calculate for example +2 or +3 years to the date....
I have the following python code. I get the following message when running it using a...
I have the following python code. I get the following message when running it using a test script which I cannot send here: while textstring[iterator].isspace(): # loop until we get other than space character IndexError: string index out of range. Please find out why and correct the code. def createWords(textstrings): createdWords = [] # empty list for textstring in textstrings: # iterate through each string in trxtstrings iterator = 0 begin = iterator # new begin variable while (iterator <...
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
What caused the following error message to appear in Microsoft access do not merely state the name of the error indicate why the error occurred and what the user was doing when this message appeared?
What caused the following error message to appear in Microsoft access do not merely state the name of the error indicate why the error occurred and what the user was doing when this message appeared?
What is the error message that occurs when the manager sends Set_Request to a read-only object?...
What is the error message that occurs when the manager sends Set_Request to a read-only object? Also, list out any three error messages in SNMP v1.
C++ please show the error mesg if user enter others letter insated of I and O...
C++ please show the error mesg if user enter others letter insated of I and O and also numbers must be greater than 0 if user input -, show the error mesg untill user out the correct...thanks please Overloaded Hospital Write a program that computes and displays the charges for a patient’s hospital stay. First, the program should ask if the patient was admitted as an in-patient or an out-patient. Please keep asking the user until the user enters the...
In python can you fix the error in the line where it says message = input("Input...
In python can you fix the error in the line where it says message = input("Input a lowercase sentence: ") this is the error message I get after running the program and inputing a lowercase sentence Input a lowercase sentence:hello hi MM§MTraceback (most recent call last): MM§M File "client.py", line 14, in <module> MM§M message = input("Input a lowercase sentence:") MM§M File "<string>", line 1 MM§M hello hi MM§M ^ MM§MSyntaxError: unexpected EOF while parsing from socket import * #...
The following code has some syntax error. Please fixed the error. Besides, I want the output...
The following code has some syntax error. Please fixed the error. Besides, I want the output in ASCII characters. Please give me the corrected code along with the screenshot of the output. def cbc_dec(ys): int xs = [] int iv = ("0XAA", 16) #in decimal int key = ("0X08", 16) int x0 = chr(((163 * (int (ys[0], 16) - key)) % 256) ^ iv) xs.append(x0) for i in range (1, len(ys)): int xi = chr((( 163 * (int (ys[i], 16)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT