I have a question: Here is my code: #include #include #include #include #include
ID: 3916777 • Letter: I
Question
I have a question:
Here is my code:
#include
#include
#include
#include
#include
using namespace std;
vector sttok(string str)
{
int i=0;
vector res;
string temp="";
for(i=0;i {
if(str[i]==' ')
{
res.push_back(temp);
temp="";
}
else
{
temp.push_back(str[i]);
}
}
res.push_back(temp);
return res;
}
string strip(string str)
{
string res;
int i=0;
while(i {
if(str[i]==' ')
{
i++;
continue;
}
res.push_back(str[i]);
i++;
}
return res;
}
int main(void)
{
char framechar;
vector phrasetok;
cout<<"press 1 if you want to enter phrase through keyboard and press 0 if you want program to read phrase from file"< int in;
cin>>in;
std::string phrase;
cout<<"enter the phrase you want to print"< cin.ignore();
cin.clear();
if(in==1) //reading from keyboard
{
getline(std::cin,phrase );
}
else//reading from a file
{
}
phrasetok = sttok(phrase);
cout<<"Enter the printable frame character:"< cin>> framechar;
int just;
cout<<"enter 1 for left justification, 2 for center justification and 3 for right justification"< cin>>just;
int i=0;
for(i=1;i<=10;i++)
{
cout< }
cout< i=0;
while(i {
string str= phrasetok[i];
cout< if(just ==1)
{
int sp = 10 - str.size()+1;
cout < for(int j=1;j {
cout<<" ";
}
cout< }
else if (just==2)
{
int sp = 10-str.size()+1;
sp=sp/2;
for(int j=1;j {
cout<<" ";
}
cout< for(int j=1;j {
cout<<" ";
}
cout< }
else
{
int sp = 10-str.size()+1;
for(int j=1;j {
cout<<" ";
}
cout< }
i++;
}
for(i=1;i<=10;i++)
{
cout< }
cout< return 0;
}
When I run the code, the word is not in the center. So center justification and right justification are the same, but when I do left justification it gives a weird output. Please fix all the justification, so center shows the word in center, left in left, and right in right.
Here is the program information:
Whereas the phrase "the sky is falling"might be:
Or:
Or even:
Depending on whether or not the user asked for left, centered, or right justification — respectively — of the phrase's words within the frame. Note how the frame exactly fits the phrase based on the longest word within. Neat, eh?
ALSO:
I would like to add the following to my code:
Add (Level 1.5) to add a choice of frame types: single line, double line, or shaded. For instance:
And:
Add (Level 3.5) to combine this with a templated linked list class. Hmm...where would you use such a data structure?
For full credit, your linked list must implement all insertion and removal functions that would be generally useful — not just those you'll use here.
This option is not combinable with the templated dynamic array option!
Explanation / Answer
If you have any doubts, please give me comment...
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
// #include
using namespace std;
vector<string> sttok(string str)
{
int i = 0;
vector<string> res;
string temp = "";
for (i = 0; i < str.size(); i++)
{
if (str[i] == ' ')
{
res.push_back(temp);
temp = "";
}
else
{
temp.push_back(str[i]);
}
}
res.push_back(temp);
return res;
}
string strip(string str)
{
string res;
int i = 0;
while (i < str.size())
{
if (str[i] == ' ')
{
i++;
continue;
}
res.push_back(str[i]);
i++;
}
return res;
}
int main(void)
{
char framechar;
vector<string> phrasetok;
cout << "press 1 if you want to enter phrase through keyboard and press 0 if "
"you want program to read phrase from file"
<< endl;
int in;
cin >> in;
std::string phrase;
cout << "enter the phrase you want to print"<<endl;
cin.ignore();
cin.clear();
if (in == 1) // reading from keyboard
{
getline(std::cin, phrase);
}
else // reading from a file
{
}
phrasetok = sttok(phrase);
cout << "Enter the printable frame character: ";
cin.clear();
cin >> framechar;
int just;
cout << "enter 1 for left justification, 2 for center justification and 3 "
"for right justification"
<< endl;
cin >> just;
int i = 0;
cout<<" ";
for (i = 1; i <=10; i++)
{
cout << framechar;
}
cout << endl;
i = 0;
while (i < phrasetok.size())
{
string str = phrasetok[i];
cout << framechar;
if (just == 1)
{
int sp = 10 - str.size()+1;
cout << str;
for (int j = 1; j < sp; j++)
{
cout << " ";
}
}
else if (just == 2)
{
int sp = 10 - str.size()+1;
sp = sp / 2;
for (int j = 1; j <= sp; j++)
{
cout << " ";
}
cout << str;
if(str.size()%2==0)
sp++;
for (int j = 1; j < sp; j++)
{
cout << " ";
}
}
else
{
int sp = 10 - str.size() +1;
for (int j = 1; j < sp; j++)
{
cout << " ";
}
cout << str;
}
cout<<framechar<<endl;
i++;
}
cout<<" ";
for (i = 1; i <= 10; i++)
{
cout << framechar;
}
cout << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.