C++ questions in regard to some code that I have below. #include<iostream> #incl
ID: 3860052 • Letter: C
Question
C++ questions in regard to some code that I have below.
#include<iostream>
#include<string>
#include<algorithm>
#include<sstream>
using namespace std;
int main()
{
cout<<" Welcome to the point menu program ";
string choice;
/* looping the menu */
do{
string line1,line2;
double firstPointStart,firstPointEnd;
double secondPointStart,secondPointEnd;
/*displaying the menu*/
cout<<"1) Calculate distance between two points "<<endl;
cout<<"2) Calculate Midpoint of two points "<<endl;
cout<<"3) Quit"<<endl;
cin>>choice;
/*converting choice to lower case*/
transform(choice.begin(), choice.end(), choice.begin(), ::tolower);
/* if choice equal to
1 or mid or MID or MiD mID */
if(choice=="1"||choice=="mid")
{
//asking the input
/*input in form of <first<space>second> 34 32*/
cout<<"What is your first point ? ";
cin.ignore(256, ' ');
getline(cin,line1);
cout<<"What is your second point ? ";
getline(cin,line2);
stringstream ss1(line1);
stringstream ss2(line2);
string temp;
int i=0,j=0;
//manupulatin the line
while(getline(ss1,temp,' '))
{
if(i==0)
firstPointStart=stod(temp);
if(i==1)
{
firstPointEnd=stod(temp);
}
i++;
}
while(getline(ss2,temp,' '))
{
if(j==0)
secondPointStart=stod(temp);
if(j==1)
{
secondPointEnd=stod(temp);
}
j++;
}
//calculating the midpoint
cout<<" Midpoint of the line segment from ("<<line1<<") to ("<<line2<<") is ("<<(firstPointStart+secondPointStart)/2
<<" "<<(firstPointEnd+secondPointEnd)/2<<") "<<endl;
}
/* if choice equal to
2 or dis or DIS or Dis dIS */
else if(choice=="2"||choice=="dis")
{
cout<<"What is your first point ? ";
cin.ignore(256, ' ');
getline(cin,line1);
cout<<"What is your second point ? ";
getline(cin,line2);
stringstream ss1(line1);
stringstream ss2(line2);
string temp;
int i=0,j=0;
while(getline(ss1,temp,' '))
{
if(i==0)
firstPointStart=stod(temp);
if(i==1)
{
firstPointEnd=stod(temp);
}
i++;
}
while(getline(ss2,temp,' '))
{
if(j==0)
secondPointStart=stod(temp);
if(j==1)
{
secondPointEnd=stod(temp);
}
j++;
}
cout<<" Distance of the line segment from ("<<line2<<") to ("<<line1<<") is ("<<secondPointStart-firstPointStart
<<" "<<secondPointEnd-firstPointEnd<<") "<<endl;
}
/* if choice equal to
3 or quit or QUIT or QuiT quIT */
else if(choice=="3"||choice=="quit")
{
cout<<"Thanks for using point menu program "<<endl;
break;
}
//any invalid input comes here
else{
cout<<"I'm sorry , that choice is invalid!"<<endl;
}
}
while(true);
return 0;
}
Explanation / Answer
//Dear Student,
//The numbering was worng for mid and dis
//I have bolded the parts needed for answering the questions
//Ques 1 :- I have declared a int variable
//named it a and initialised it to 1
//whenever the quit condition is satisfied, it gets to 0
//Hence putting 'a' directly in the do while loop
//integer a is a flag here
//Ques 2 :- If every option fails
//Then its an invalid option
//Ques 3 :- If everything is converted into either lower or upper case
//Comparison will be easy and one step
//String variable is ummutable.
//using toupper() function we have to convert each character
//Ques 4 :- This part is already done by you.
//An alternate method is to use fflush()
//Ques 5 :- 9 + 9 + 17 + 1 = 25 inputs willbe required
//9 for 2/mid/Mid/Mid.... and so on
//9 for 1/dis/DIS/Dis.... and so on
//17 for 3/QUIt/quit......and so on
//1 to check an invalid input
#include<iostream>
#include<string>
#include<algorithm>
#include<sstream>
using namespace std;
int main()
{
cout<<" Welcome to the point menu program ";
string choice;
/* looping the menu */
int a;
a = 1;
do{
string line1,line2;
double firstPointStart,firstPointEnd;
double secondPointStart,secondPointEnd;
/*displaying the menu*/
cout<<"1) Calculate distance between two points "<<endl;
cout<<"2) Calculate Midpoint of two points "<<endl;
cout<<"3) Quit"<<endl;
cin>>choice;
string choicecopy;
char choice1[456];
int i;
i = 0;
while(choice[i]){
choice1[i]=(toupper(choice[i]));
i++;
}
choicecopy.assign(choice1);
/*converting choice to lower case*/
//transform(choice.begin(), choice.end(), choice.begin(), ::tolower);
/* if choice equal to
1 or mid or MID or MiD mID */
if(choice=="2" || choicecopy == "MID")
{
//asking the input
/*input in form of <first<space>second> 34 32*/
cout<<"What is your first point ? ";
cin.ignore(256, ' ');
getline(cin,line1);
cout<<"What is your second point ? ";
getline(cin,line2);
stringstream ss1(line1);
stringstream ss2(line2);
string temp;
int i=0,j=0;
//manupulatin the line
while(getline(ss1,temp,' '))
{
if(i==0)
firstPointStart=stod(temp);
if(i==1)
{
firstPointEnd=stod(temp);
}
i++;
}
while(getline(ss2,temp,' '))
{
if(j==0)
secondPointStart=stod(temp);
if(j==1)
{
secondPointEnd=stod(temp);
}
j++;
}
//calculating the midpoint
cout<<" Midpoint of the line segment from ("<<line1<<") to ("<<line2<<") is ("<<(firstPointStart+secondPointStart)/2
<<" "<<(firstPointEnd+secondPointEnd)/2<<") "<<endl;
}
/* if choice equal to
2 or dis or DIS or Dis dIS */
else if(choice=="1" || choicecopy == "DIS")
{
cout<<"What is your first point ? ";
cin.ignore(256, ' ');
getline(cin,line1);
cout<<"What is your second point ? ";
getline(cin,line2);
stringstream ss1(line1);
stringstream ss2(line2);
string temp;
int i=0,j=0;
while(getline(ss1,temp,' '))
{
if(i==0)
firstPointStart=stod(temp);
if(i==1)
{
firstPointEnd=stod(temp);
}
i++;
}
while(getline(ss2,temp,' '))
{
if(j==0)
secondPointStart=stod(temp);
if(j==1)
{
secondPointEnd=stod(temp);
}
j++;
}
cout<<" Distance of the line segment from ("<<line2<<") to ("<<line1<<") is ("<<secondPointStart-firstPointStart
<<" "<<secondPointEnd-firstPointEnd<<") "<<endl;
}
/* if choice equal to
3 or quit or QUIT or QuiT quIT */
else if(choice=="3"|| choicecopy == "QUIT")
{
a = 0;
cout<<"Thanks for using point menu program "<<endl;
break;
}
//any invalid input comes here
else{
cout<<"I'm sorry , that choice is invalid!"<<endl;
}
fflush(stdin);
fflush(stdout);
}
//Question 1
while(a);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.