C++ language This is the last answer I got but I would like to use a \"string\"
ID: 3745724 • Letter: C
Question
C++ language
This is the last answer I got but I would like to use a "string" instead of "char" please help me...
#include <iostream>
using namespace std;
//function declaration
void sort(int& a,int& b,int& c);
int main()
{
int a,b,c;
cout<<"Enter number 1:";
cin>>a;
cout<<"Enter number 2:";
cin>>b;
cout<<"Enter number 3:";
cin>>c;
sort(a,b,c);
cout<<"Sorted they are :"<<a<<" "<<b<<" "<<c<<endl;
char ch;
int num;
cout<<" A.Aleppo Pepper"<<endl;
cout<<"B.Banana Pepper"<<endl;
cout<<"C.Cayenne Pepper"<<endl;
cout<<"D.Dragons Breath"<<endl;
cout<<"Enter a letter to choose a pepper :";
cin>>ch;
if( ch == 'a' || ch == 'A' )
{
num=30000;
cout<<"That's "<<num<<" Scovilles of heat!"<<endl;
}
else if( ch == 'b' || ch == 'B' )
{
num=1000;
cout<<"That's "<<num<<" Scovilles of heat!"<<endl;
}
else if( ch == 'c' || ch == 'C' )
{
num=40000;
cout<<"That's "<<num<<" Scovilles of heat!"<<endl;
}
else if( ch == 'd' || ch == 'D' )
{
num=2480000;
cout<<"That's "<<num<<" Scovilles of heat!"<<endl;
}
else
cout<<"The pepper not on the list"<<endl;
system("pause");
return 0;
}
//Function implementation which sorts the 3 numbers in ascending order
void sort(int& a,int& b,int& c)
{
//Declaring variable
int temp;
/* Checking whether first number is greater
* than second number or not
* if yes, Swapping will happen
*/
if( a>b )
{
temp = a;
a = b;
b = temp;
}
/* Checking whether second number is greater
* than third number or not
* if yes, Swapping will happen
*/
if( b>c )
{
temp = b;
b = c;
c = temp;
}
/* Checking whether first number is greater
* than second number or not
* if yes, Swapping will happen
*/
if( a>b )
{
temp = a;
a = b;
b = temp;
}
}
CS 215- Fall 2018 Lab 2 Learning Objectives: More practice with user input/output, including string input. Use of if/else, possibly nested, to solve simple problems. General Description 1. Write a program that asks the user to enter 3 numbers, one at a time. Print the three numbers in sorted order. [Note: no need to use an array or similar structure!] Examples (user input in blue) Enter number 1: 45 Enter number 1: 55 Enter number 2: 45 Enter number 3: 35 Sorted they are 35 4555 Enter number 2: 35 Enter number 3: 55 Sorted they are 35 45 55 2. In the same program: Write code that will display the following menu A. Aleppo Pepper B. Banana Pepper C. Cayenne Pepper D. Dragons Breath Enter a letter to choose a pepper: Calculate the Scoville Units (measure of hotness) Aleppo 30,000 Banana 1,000 Cayenne40,000 Dragon's Breath 2,480,000 Print the Scoville Units for the chosen pepper, example That' s 1000 Scovilles of heat!Explanation / Answer
Answer:
This is the modified code as per your requirement. I used here String data type instead of Char its working properly. I hope your requirement is fulfilled.
#include <iostream>
#include<cstdlib>
using namespace std;
//function declaration
void sort(int& a,int& b,int& c);
int main()
{
int a,b,c;
cout<<"Enter number 1:";
cin>>a;
cout<<"Enter number 2:";
cin>>b;
cout<<"Enter number 3:";
cin>>c;
sort(a,b,c);
cout<<"Sorted they are :"<<a<<" "<<b<<" "<<c<<endl;
string st;
int num;
cout<<" A.Aleppo Pepper"<<endl;
cout<<"B.Banana Pepper"<<endl;
cout<<"C.Cayenne Pepper"<<endl;
cout<<"D.Dragons Breath"<<endl;
cout<<"Enter a letter to choose a pepper :";
cin>>st;
//string enclosed with inverted commas
if( st == "a" || st == "A" )
{
num=30000;
cout<<"That's "<<num<<" Scovilles of heat!"<<endl;
}
else if( st == "b" || st == "B" )
{
num=1000;
cout<<"That's "<<num<<" Scovilles of heat!"<<endl;
}
else if( st == "c" || st == "C" )
{
num=40000;
cout<<"That's "<<num<<" Scovilles of heat!"<<endl;
}
else if( st == "d" || st == "D" )
{
num=2480000;
cout<<"That's "<<num<<" Scovilles of heat!"<<endl;
}
else
cout<<"The pepper not on the list"<<endl;
system("pause");
return 0;
}
//Function implementation which sorts the 3 numbers in ascending order
void sort(int& a,int& b,int& c)
{
//Declaring variable
int temp;
/* Checking whether first number is greater
* than second number or not
* if yes, Swapping will happen
*/
if( a>b )
{
temp = a;
a = b;
b = temp;
}
/* Checking whether second number is greater
* than third number or not
* if yes, Swapping will happen
*/
if( b>c )
{
temp = b;
b = c;
c = temp;
}
/* Checking whether first number is greater
* than second number or not
* if yes, Swapping will happen
*/
if( a>b )
{
temp = a;
a = b;
b = temp;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.