Vector push_back & pop_back
I'm working on an assignment. I have coded the assignment and everything is working as it should, except my output is missing a digit. I also attached the sample output. After inputting a a mathematical expression into a string, for example 35 * 4 - 6 / (9 + 3), a digit is dropped. In normal order, the 3 will be missing. In reverse order, the 9 will be missing. I am not understanding why this happening.
Any help, or I would prefer some guidance, would be appreciated. Am I using the push_back incorrectly? The code is as follows:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector<string> split(string);
vector<string> splitback(string);
int main()
{
vector<string> vectorExpression;
string expression;
cout << "Enter an expression :";
getline(cin, expression);
vectorExpression = split(expression);
for (int i = 0; i < vectorExpression.size(); i++)
{
cout << vectorExpression[i] << endl;
}
cout << endl;
vectorExpression = splitback(expression);
for (int i = 0; i < vectorExpression.size(); i++)
{
cout << vectorExpression[i] << endl;
}
system("pause");
return 0;
}
vector<string> split(string expression)
{
vector<string> splitExpression;
string digit = "",
x = "";
for (int i = 0; i < expression.size(); i++)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = digit + expression[i];
}
else if (expression[i] != ' ')
{
x = "";
x = x + expression[i];
splitExpression.push_back(x);
}
else
{
if (digit.size() > 0)
{
splitExpression.push_back(digit);
digit = "";
}
}
}
if (digit.size() > 0)
{
splitExpression.push_back(digit);
}
return splitExpression;
}
vector<string> splitback(string expression)
{
vector<string> splitBackExpression;
string number = "",
x = "";
for (int i = expression.size() - 1; i >= 0; i--)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
number = expression[i] + number;
}
else if (expression[i] != ' ')
{
x = "";
x = x + expression[i];
splitBackExpression.push_back(x);
}
if (number.size() > 0)
{
splitBackExpression.push_back(number);
number = "";
}
}
return splitBackExpression;
}

c++ vector
add a comment |
I'm working on an assignment. I have coded the assignment and everything is working as it should, except my output is missing a digit. I also attached the sample output. After inputting a a mathematical expression into a string, for example 35 * 4 - 6 / (9 + 3), a digit is dropped. In normal order, the 3 will be missing. In reverse order, the 9 will be missing. I am not understanding why this happening.
Any help, or I would prefer some guidance, would be appreciated. Am I using the push_back incorrectly? The code is as follows:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector<string> split(string);
vector<string> splitback(string);
int main()
{
vector<string> vectorExpression;
string expression;
cout << "Enter an expression :";
getline(cin, expression);
vectorExpression = split(expression);
for (int i = 0; i < vectorExpression.size(); i++)
{
cout << vectorExpression[i] << endl;
}
cout << endl;
vectorExpression = splitback(expression);
for (int i = 0; i < vectorExpression.size(); i++)
{
cout << vectorExpression[i] << endl;
}
system("pause");
return 0;
}
vector<string> split(string expression)
{
vector<string> splitExpression;
string digit = "",
x = "";
for (int i = 0; i < expression.size(); i++)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = digit + expression[i];
}
else if (expression[i] != ' ')
{
x = "";
x = x + expression[i];
splitExpression.push_back(x);
}
else
{
if (digit.size() > 0)
{
splitExpression.push_back(digit);
digit = "";
}
}
}
if (digit.size() > 0)
{
splitExpression.push_back(digit);
}
return splitExpression;
}
vector<string> splitback(string expression)
{
vector<string> splitBackExpression;
string number = "",
x = "";
for (int i = expression.size() - 1; i >= 0; i--)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
number = expression[i] + number;
}
else if (expression[i] != ' ')
{
x = "";
x = x + expression[i];
splitBackExpression.push_back(x);
}
if (number.size() > 0)
{
splitBackExpression.push_back(number);
number = "";
}
}
return splitBackExpression;
}

c++ vector
I am missing something here......I'm still losing a 1 digit. IF you view the attached picture, you'll see what I am speaking of.
– Nalaedge
Nov 24 '18 at 6:33
Do not add a solution in the question itself: I've rollbacked your edit. Please find your solution in the revision history and post it as an answer of its own.
– Cœur
Dec 1 '18 at 10:33
add a comment |
I'm working on an assignment. I have coded the assignment and everything is working as it should, except my output is missing a digit. I also attached the sample output. After inputting a a mathematical expression into a string, for example 35 * 4 - 6 / (9 + 3), a digit is dropped. In normal order, the 3 will be missing. In reverse order, the 9 will be missing. I am not understanding why this happening.
Any help, or I would prefer some guidance, would be appreciated. Am I using the push_back incorrectly? The code is as follows:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector<string> split(string);
vector<string> splitback(string);
int main()
{
vector<string> vectorExpression;
string expression;
cout << "Enter an expression :";
getline(cin, expression);
vectorExpression = split(expression);
for (int i = 0; i < vectorExpression.size(); i++)
{
cout << vectorExpression[i] << endl;
}
cout << endl;
vectorExpression = splitback(expression);
for (int i = 0; i < vectorExpression.size(); i++)
{
cout << vectorExpression[i] << endl;
}
system("pause");
return 0;
}
vector<string> split(string expression)
{
vector<string> splitExpression;
string digit = "",
x = "";
for (int i = 0; i < expression.size(); i++)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = digit + expression[i];
}
else if (expression[i] != ' ')
{
x = "";
x = x + expression[i];
splitExpression.push_back(x);
}
else
{
if (digit.size() > 0)
{
splitExpression.push_back(digit);
digit = "";
}
}
}
if (digit.size() > 0)
{
splitExpression.push_back(digit);
}
return splitExpression;
}
vector<string> splitback(string expression)
{
vector<string> splitBackExpression;
string number = "",
x = "";
for (int i = expression.size() - 1; i >= 0; i--)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
number = expression[i] + number;
}
else if (expression[i] != ' ')
{
x = "";
x = x + expression[i];
splitBackExpression.push_back(x);
}
if (number.size() > 0)
{
splitBackExpression.push_back(number);
number = "";
}
}
return splitBackExpression;
}

c++ vector
I'm working on an assignment. I have coded the assignment and everything is working as it should, except my output is missing a digit. I also attached the sample output. After inputting a a mathematical expression into a string, for example 35 * 4 - 6 / (9 + 3), a digit is dropped. In normal order, the 3 will be missing. In reverse order, the 9 will be missing. I am not understanding why this happening.
Any help, or I would prefer some guidance, would be appreciated. Am I using the push_back incorrectly? The code is as follows:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector<string> split(string);
vector<string> splitback(string);
int main()
{
vector<string> vectorExpression;
string expression;
cout << "Enter an expression :";
getline(cin, expression);
vectorExpression = split(expression);
for (int i = 0; i < vectorExpression.size(); i++)
{
cout << vectorExpression[i] << endl;
}
cout << endl;
vectorExpression = splitback(expression);
for (int i = 0; i < vectorExpression.size(); i++)
{
cout << vectorExpression[i] << endl;
}
system("pause");
return 0;
}
vector<string> split(string expression)
{
vector<string> splitExpression;
string digit = "",
x = "";
for (int i = 0; i < expression.size(); i++)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = digit + expression[i];
}
else if (expression[i] != ' ')
{
x = "";
x = x + expression[i];
splitExpression.push_back(x);
}
else
{
if (digit.size() > 0)
{
splitExpression.push_back(digit);
digit = "";
}
}
}
if (digit.size() > 0)
{
splitExpression.push_back(digit);
}
return splitExpression;
}
vector<string> splitback(string expression)
{
vector<string> splitBackExpression;
string number = "",
x = "";
for (int i = expression.size() - 1; i >= 0; i--)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
number = expression[i] + number;
}
else if (expression[i] != ' ')
{
x = "";
x = x + expression[i];
splitBackExpression.push_back(x);
}
if (number.size() > 0)
{
splitBackExpression.push_back(number);
number = "";
}
}
return splitBackExpression;
}

c++ vector
c++ vector
edited Dec 1 '18 at 10:31
Cœur
18.4k9109148
18.4k9109148
asked Nov 23 '18 at 23:38
NalaedgeNalaedge
34
34
I am missing something here......I'm still losing a 1 digit. IF you view the attached picture, you'll see what I am speaking of.
– Nalaedge
Nov 24 '18 at 6:33
Do not add a solution in the question itself: I've rollbacked your edit. Please find your solution in the revision history and post it as an answer of its own.
– Cœur
Dec 1 '18 at 10:33
add a comment |
I am missing something here......I'm still losing a 1 digit. IF you view the attached picture, you'll see what I am speaking of.
– Nalaedge
Nov 24 '18 at 6:33
Do not add a solution in the question itself: I've rollbacked your edit. Please find your solution in the revision history and post it as an answer of its own.
– Cœur
Dec 1 '18 at 10:33
I am missing something here......I'm still losing a 1 digit. IF you view the attached picture, you'll see what I am speaking of.
– Nalaedge
Nov 24 '18 at 6:33
I am missing something here......I'm still losing a 1 digit. IF you view the attached picture, you'll see what I am speaking of.
– Nalaedge
Nov 24 '18 at 6:33
Do not add a solution in the question itself: I've rollbacked your edit. Please find your solution in the revision history and post it as an answer of its own.
– Cœur
Dec 1 '18 at 10:33
Do not add a solution in the question itself: I've rollbacked your edit. Please find your solution in the revision history and post it as an answer of its own.
– Cœur
Dec 1 '18 at 10:33
add a comment |
2 Answers
2
active
oldest
votes
Your split function is wrong, you forget to add digits, correct one would be
vector<string> split(string expression)
{
vector<string> splitExpression;
string digit = "",
x = "";
for (int i = 0; i < expression.size(); i++)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = digit + expression[i];
}
else
{
// don't forget digit if there was one
if (digit.size() > 0)
{
splitExpression.push_back(digit);
digit = "";
}
// also add what comes next if not a whitespace
if (expression[i] != ' ')
{
x = expression[i];
splitExpression.push_back(x);
}
}
}
if (digit.size() > 0)
{
splitExpression.push_back(digit);
}
return splitExpression;
}
splitExpression.push_back(expression[i]) does not accept the expression[i].
– Nalaedge
Nov 24 '18 at 14:01
Wrap it in to_string or assign it to x as before
– Silmathoron
Nov 24 '18 at 14:07
add a comment |
Here is the final answer to my own question, or at least the solution I came up with the help from here.
#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector<string> split(const string&);
vector<string> splitback(const string&);
int main()
{
vector<string> forwardExpression,
reverseExpression;
string expression;
cout << "Enter an expression :";
getline(cin, expression);
//////////////////////////////////////////////////
// FORWARD DISPLAY OF EXPRESSION //
//////////////////////////////////////////////////
forwardExpression = split(expression);
cout << "'" << expression << "'" << " split into individual entities forwards
yields:" << endl;
cout << endl;
for (int i = 0; i < forwardExpression.size(); i++)
{
cout << forwardExpression[i] << endl;
}
cout << endl;
//////////////////////////////////////////////////
// BACKWARD DISPLAY OF EXPRESSION //
//////////////////////////////////////////////////
reverseExpression = splitback(expression);
cout << "'" << expression << "'" << " split into individual entities
backwards yields:" << endl;
cout << endl;
for (int i = 0; i < reverseExpression.size(); i++)
{
cout << reverseExpression[i] << endl;
}
cout << endl;
system("pause");
return 0;
}
//////////////////////////////////////////////////
// FORWARD FUNCTION OF EXPRESSION //
//////////////////////////////////////////////////
vector<string> split(const string &expression)
{
vector<string> splitExpression;
string digit = "",
x = "";
for (int i = 0; i < expression.size(); i++)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = digit + expression[i];
}
else
{
if (digit.size() > 0)
{
splitExpression.push_back(digit);
digit = "";
}
if (expression[i] != ' ')
{
x = expression[i];
splitExpression.push_back(x);
}
}
}
if (digit.size() > 0)
{
splitExpression.push_back(digit);
}
return splitExpression;
}
//////////////////////////////////////////////////
// BACKWARD FUNCTION OF EXPRESSION //
//////////////////////////////////////////////////
vector<string> splitback(const string &expression)
{
vector<string> splitBackExpression;
string digit = "",
x = "";
for (int i = expression.size() - 1; i >= 0; i--)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = expression[i] + digit;
}
else
{
if (digit.size() > 0)
{
splitBackExpression.push_back(digit);
digit = "";
}
if (expression[i] != ' ')
{
x = expression[i];
splitBackExpression.push_back(x);
}
}
}
if (digit.size() > 0)
{
splitBackExpression.push_back(digit);
}
return splitBackExpression;
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453912%2fvector-push-back-pop-back%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your split function is wrong, you forget to add digits, correct one would be
vector<string> split(string expression)
{
vector<string> splitExpression;
string digit = "",
x = "";
for (int i = 0; i < expression.size(); i++)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = digit + expression[i];
}
else
{
// don't forget digit if there was one
if (digit.size() > 0)
{
splitExpression.push_back(digit);
digit = "";
}
// also add what comes next if not a whitespace
if (expression[i] != ' ')
{
x = expression[i];
splitExpression.push_back(x);
}
}
}
if (digit.size() > 0)
{
splitExpression.push_back(digit);
}
return splitExpression;
}
splitExpression.push_back(expression[i]) does not accept the expression[i].
– Nalaedge
Nov 24 '18 at 14:01
Wrap it in to_string or assign it to x as before
– Silmathoron
Nov 24 '18 at 14:07
add a comment |
Your split function is wrong, you forget to add digits, correct one would be
vector<string> split(string expression)
{
vector<string> splitExpression;
string digit = "",
x = "";
for (int i = 0; i < expression.size(); i++)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = digit + expression[i];
}
else
{
// don't forget digit if there was one
if (digit.size() > 0)
{
splitExpression.push_back(digit);
digit = "";
}
// also add what comes next if not a whitespace
if (expression[i] != ' ')
{
x = expression[i];
splitExpression.push_back(x);
}
}
}
if (digit.size() > 0)
{
splitExpression.push_back(digit);
}
return splitExpression;
}
splitExpression.push_back(expression[i]) does not accept the expression[i].
– Nalaedge
Nov 24 '18 at 14:01
Wrap it in to_string or assign it to x as before
– Silmathoron
Nov 24 '18 at 14:07
add a comment |
Your split function is wrong, you forget to add digits, correct one would be
vector<string> split(string expression)
{
vector<string> splitExpression;
string digit = "",
x = "";
for (int i = 0; i < expression.size(); i++)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = digit + expression[i];
}
else
{
// don't forget digit if there was one
if (digit.size() > 0)
{
splitExpression.push_back(digit);
digit = "";
}
// also add what comes next if not a whitespace
if (expression[i] != ' ')
{
x = expression[i];
splitExpression.push_back(x);
}
}
}
if (digit.size() > 0)
{
splitExpression.push_back(digit);
}
return splitExpression;
}
Your split function is wrong, you forget to add digits, correct one would be
vector<string> split(string expression)
{
vector<string> splitExpression;
string digit = "",
x = "";
for (int i = 0; i < expression.size(); i++)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = digit + expression[i];
}
else
{
// don't forget digit if there was one
if (digit.size() > 0)
{
splitExpression.push_back(digit);
digit = "";
}
// also add what comes next if not a whitespace
if (expression[i] != ' ')
{
x = expression[i];
splitExpression.push_back(x);
}
}
}
if (digit.size() > 0)
{
splitExpression.push_back(digit);
}
return splitExpression;
}
edited Nov 24 '18 at 14:11
answered Nov 24 '18 at 6:50
SilmathoronSilmathoron
1,0461921
1,0461921
splitExpression.push_back(expression[i]) does not accept the expression[i].
– Nalaedge
Nov 24 '18 at 14:01
Wrap it in to_string or assign it to x as before
– Silmathoron
Nov 24 '18 at 14:07
add a comment |
splitExpression.push_back(expression[i]) does not accept the expression[i].
– Nalaedge
Nov 24 '18 at 14:01
Wrap it in to_string or assign it to x as before
– Silmathoron
Nov 24 '18 at 14:07
splitExpression.push_back(expression[i]) does not accept the expression[i].
– Nalaedge
Nov 24 '18 at 14:01
splitExpression.push_back(expression[i]) does not accept the expression[i].
– Nalaedge
Nov 24 '18 at 14:01
Wrap it in to_string or assign it to x as before
– Silmathoron
Nov 24 '18 at 14:07
Wrap it in to_string or assign it to x as before
– Silmathoron
Nov 24 '18 at 14:07
add a comment |
Here is the final answer to my own question, or at least the solution I came up with the help from here.
#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector<string> split(const string&);
vector<string> splitback(const string&);
int main()
{
vector<string> forwardExpression,
reverseExpression;
string expression;
cout << "Enter an expression :";
getline(cin, expression);
//////////////////////////////////////////////////
// FORWARD DISPLAY OF EXPRESSION //
//////////////////////////////////////////////////
forwardExpression = split(expression);
cout << "'" << expression << "'" << " split into individual entities forwards
yields:" << endl;
cout << endl;
for (int i = 0; i < forwardExpression.size(); i++)
{
cout << forwardExpression[i] << endl;
}
cout << endl;
//////////////////////////////////////////////////
// BACKWARD DISPLAY OF EXPRESSION //
//////////////////////////////////////////////////
reverseExpression = splitback(expression);
cout << "'" << expression << "'" << " split into individual entities
backwards yields:" << endl;
cout << endl;
for (int i = 0; i < reverseExpression.size(); i++)
{
cout << reverseExpression[i] << endl;
}
cout << endl;
system("pause");
return 0;
}
//////////////////////////////////////////////////
// FORWARD FUNCTION OF EXPRESSION //
//////////////////////////////////////////////////
vector<string> split(const string &expression)
{
vector<string> splitExpression;
string digit = "",
x = "";
for (int i = 0; i < expression.size(); i++)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = digit + expression[i];
}
else
{
if (digit.size() > 0)
{
splitExpression.push_back(digit);
digit = "";
}
if (expression[i] != ' ')
{
x = expression[i];
splitExpression.push_back(x);
}
}
}
if (digit.size() > 0)
{
splitExpression.push_back(digit);
}
return splitExpression;
}
//////////////////////////////////////////////////
// BACKWARD FUNCTION OF EXPRESSION //
//////////////////////////////////////////////////
vector<string> splitback(const string &expression)
{
vector<string> splitBackExpression;
string digit = "",
x = "";
for (int i = expression.size() - 1; i >= 0; i--)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = expression[i] + digit;
}
else
{
if (digit.size() > 0)
{
splitBackExpression.push_back(digit);
digit = "";
}
if (expression[i] != ' ')
{
x = expression[i];
splitBackExpression.push_back(x);
}
}
}
if (digit.size() > 0)
{
splitBackExpression.push_back(digit);
}
return splitBackExpression;
}
add a comment |
Here is the final answer to my own question, or at least the solution I came up with the help from here.
#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector<string> split(const string&);
vector<string> splitback(const string&);
int main()
{
vector<string> forwardExpression,
reverseExpression;
string expression;
cout << "Enter an expression :";
getline(cin, expression);
//////////////////////////////////////////////////
// FORWARD DISPLAY OF EXPRESSION //
//////////////////////////////////////////////////
forwardExpression = split(expression);
cout << "'" << expression << "'" << " split into individual entities forwards
yields:" << endl;
cout << endl;
for (int i = 0; i < forwardExpression.size(); i++)
{
cout << forwardExpression[i] << endl;
}
cout << endl;
//////////////////////////////////////////////////
// BACKWARD DISPLAY OF EXPRESSION //
//////////////////////////////////////////////////
reverseExpression = splitback(expression);
cout << "'" << expression << "'" << " split into individual entities
backwards yields:" << endl;
cout << endl;
for (int i = 0; i < reverseExpression.size(); i++)
{
cout << reverseExpression[i] << endl;
}
cout << endl;
system("pause");
return 0;
}
//////////////////////////////////////////////////
// FORWARD FUNCTION OF EXPRESSION //
//////////////////////////////////////////////////
vector<string> split(const string &expression)
{
vector<string> splitExpression;
string digit = "",
x = "";
for (int i = 0; i < expression.size(); i++)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = digit + expression[i];
}
else
{
if (digit.size() > 0)
{
splitExpression.push_back(digit);
digit = "";
}
if (expression[i] != ' ')
{
x = expression[i];
splitExpression.push_back(x);
}
}
}
if (digit.size() > 0)
{
splitExpression.push_back(digit);
}
return splitExpression;
}
//////////////////////////////////////////////////
// BACKWARD FUNCTION OF EXPRESSION //
//////////////////////////////////////////////////
vector<string> splitback(const string &expression)
{
vector<string> splitBackExpression;
string digit = "",
x = "";
for (int i = expression.size() - 1; i >= 0; i--)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = expression[i] + digit;
}
else
{
if (digit.size() > 0)
{
splitBackExpression.push_back(digit);
digit = "";
}
if (expression[i] != ' ')
{
x = expression[i];
splitBackExpression.push_back(x);
}
}
}
if (digit.size() > 0)
{
splitBackExpression.push_back(digit);
}
return splitBackExpression;
}
add a comment |
Here is the final answer to my own question, or at least the solution I came up with the help from here.
#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector<string> split(const string&);
vector<string> splitback(const string&);
int main()
{
vector<string> forwardExpression,
reverseExpression;
string expression;
cout << "Enter an expression :";
getline(cin, expression);
//////////////////////////////////////////////////
// FORWARD DISPLAY OF EXPRESSION //
//////////////////////////////////////////////////
forwardExpression = split(expression);
cout << "'" << expression << "'" << " split into individual entities forwards
yields:" << endl;
cout << endl;
for (int i = 0; i < forwardExpression.size(); i++)
{
cout << forwardExpression[i] << endl;
}
cout << endl;
//////////////////////////////////////////////////
// BACKWARD DISPLAY OF EXPRESSION //
//////////////////////////////////////////////////
reverseExpression = splitback(expression);
cout << "'" << expression << "'" << " split into individual entities
backwards yields:" << endl;
cout << endl;
for (int i = 0; i < reverseExpression.size(); i++)
{
cout << reverseExpression[i] << endl;
}
cout << endl;
system("pause");
return 0;
}
//////////////////////////////////////////////////
// FORWARD FUNCTION OF EXPRESSION //
//////////////////////////////////////////////////
vector<string> split(const string &expression)
{
vector<string> splitExpression;
string digit = "",
x = "";
for (int i = 0; i < expression.size(); i++)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = digit + expression[i];
}
else
{
if (digit.size() > 0)
{
splitExpression.push_back(digit);
digit = "";
}
if (expression[i] != ' ')
{
x = expression[i];
splitExpression.push_back(x);
}
}
}
if (digit.size() > 0)
{
splitExpression.push_back(digit);
}
return splitExpression;
}
//////////////////////////////////////////////////
// BACKWARD FUNCTION OF EXPRESSION //
//////////////////////////////////////////////////
vector<string> splitback(const string &expression)
{
vector<string> splitBackExpression;
string digit = "",
x = "";
for (int i = expression.size() - 1; i >= 0; i--)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = expression[i] + digit;
}
else
{
if (digit.size() > 0)
{
splitBackExpression.push_back(digit);
digit = "";
}
if (expression[i] != ' ')
{
x = expression[i];
splitBackExpression.push_back(x);
}
}
}
if (digit.size() > 0)
{
splitBackExpression.push_back(digit);
}
return splitBackExpression;
}
Here is the final answer to my own question, or at least the solution I came up with the help from here.
#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector<string> split(const string&);
vector<string> splitback(const string&);
int main()
{
vector<string> forwardExpression,
reverseExpression;
string expression;
cout << "Enter an expression :";
getline(cin, expression);
//////////////////////////////////////////////////
// FORWARD DISPLAY OF EXPRESSION //
//////////////////////////////////////////////////
forwardExpression = split(expression);
cout << "'" << expression << "'" << " split into individual entities forwards
yields:" << endl;
cout << endl;
for (int i = 0; i < forwardExpression.size(); i++)
{
cout << forwardExpression[i] << endl;
}
cout << endl;
//////////////////////////////////////////////////
// BACKWARD DISPLAY OF EXPRESSION //
//////////////////////////////////////////////////
reverseExpression = splitback(expression);
cout << "'" << expression << "'" << " split into individual entities
backwards yields:" << endl;
cout << endl;
for (int i = 0; i < reverseExpression.size(); i++)
{
cout << reverseExpression[i] << endl;
}
cout << endl;
system("pause");
return 0;
}
//////////////////////////////////////////////////
// FORWARD FUNCTION OF EXPRESSION //
//////////////////////////////////////////////////
vector<string> split(const string &expression)
{
vector<string> splitExpression;
string digit = "",
x = "";
for (int i = 0; i < expression.size(); i++)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = digit + expression[i];
}
else
{
if (digit.size() > 0)
{
splitExpression.push_back(digit);
digit = "";
}
if (expression[i] != ' ')
{
x = expression[i];
splitExpression.push_back(x);
}
}
}
if (digit.size() > 0)
{
splitExpression.push_back(digit);
}
return splitExpression;
}
//////////////////////////////////////////////////
// BACKWARD FUNCTION OF EXPRESSION //
//////////////////////////////////////////////////
vector<string> splitback(const string &expression)
{
vector<string> splitBackExpression;
string digit = "",
x = "";
for (int i = expression.size() - 1; i >= 0; i--)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = expression[i] + digit;
}
else
{
if (digit.size() > 0)
{
splitBackExpression.push_back(digit);
digit = "";
}
if (expression[i] != ' ')
{
x = expression[i];
splitBackExpression.push_back(x);
}
}
}
if (digit.size() > 0)
{
splitBackExpression.push_back(digit);
}
return splitBackExpression;
}
answered Dec 2 '18 at 13:20
NalaedgeNalaedge
34
34
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453912%2fvector-push-back-pop-back%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I am missing something here......I'm still losing a 1 digit. IF you view the attached picture, you'll see what I am speaking of.
– Nalaedge
Nov 24 '18 at 6:33
Do not add a solution in the question itself: I've rollbacked your edit. Please find your solution in the revision history and post it as an answer of its own.
– Cœur
Dec 1 '18 at 10:33