#include<string>
#include<iostream>
#include<sstream>
#include<vector>
usingnamespace std;
int isnumber (char numb) {
constint NoConstSt = 48;
constint NoConstEn = 58;
int newno = numb;
if (newno >= NoConstSt && newno <= NoConstEn) return 1; //This returns one, which to the program will mean "this container holds a number".
elseif (newno == 45) return 2; //Means we do subtraction.
elseif (newno == 43) return 3; //Means we do addition.
elseif (newno == 42) return 4; //Means we do multiplication.
elseif (newno == 47) return 5; //Means we do division.
elseif (newno == 40) return 6; //Means we have an opening parantheses.
elseif (newno == 41) return 7; //Means we have a closing parantheses.
elseif (newno == 46) return 8; //Means we have a decimal.
elsereturn 0; //Means we have found something which isn't a number or an operator, ie a character or a space or something. Since I don't know what space is, I'm going to leave it in the area of "this causes an error, DON'T DO THAT!"
}
string findinner(string & originalstr) { //Call this to find innermost section of the string. To do this we find out what kind of character each position in the string is, and store only the innermost opening and closing braces.
string retstr;
int copylimit = 0 ;
int start = -1 ;
int close = -1 ;
for (int h = 0; h <= originalstr.length() - 1; h++) {
if (findbraces(originalstr) == false) {
return"empty";
}
elseif (isnumber(originalstr.at(h)== 6) {
start = h;
}
elseif (isnumber(originalstr.at(h)) == 7) {
close = h;
break;
}
}
copylimit = close - start;
retstr = originalstr.substr(start,copylimit);
originalstr.erase(start,copylimit);
return retstr;
}
bool findbraces (string thestr) {
for (int t = 0; t <= thestr.length() - 1; t++) {
int retno = isnumber(thestr.at(t));
if ( retno == 6 || retno == 7) {
cout << "Returning true in findbraces at: " << t << "\n";
returntrue;
}
elseif ( retno >= 1 && retno <= 5) { cout << "Number or normal operator found at: " << t << "\n"; }
elseif (t == thestr.length() - 1) { returnfalse; }
else {cout << "There was a problem with findbraces!\n"; }
}
}
int main ()
{
vector <string> mystringvect;
string newstringsect;
string mystrin = "51-50(81/9(81/9))+63";
cout << "Mystrin is: " << mystrin <<" Length is: " << mystrin.length() -1 << "\n";
while (findbraces(mystrin) == true) {
newstringsect = findinner(mystrin);
mystringvect.push_back(newstringsect);
cout << "Found a set of parantheses in the string! They contain: " << newstringsect << "\n";
}
//Carry out the order of operations, no paranthese remain or exist.
//cout << "Enter a series of mathematical operations: \n";
/* getline (cin, mystrin); //Gets the input from the user. */
//cout<< "Mystrin contains: " << mysectionvect[0].mystring << "\n";
cin >> mystrin;
return 0;
}
1st Highlighted code gives an error on build time saying the "findbraces" identifier doesn't exist.
Shortly after that, the second error is given, saying there is a missing parantheses. I'm so frustrated!
findbraces doesn't exist (yet). You define it below that line, so the compiler doesn't know that it exists yet.
You'll either need to move findbraces so it's defined above findinner, or you'll need to put a prototype of findbraces above findinner:
bool findbraces(string); // a prototype
As for your second error, would help to know which line it's on.
EDIT:
Also, for the love of god... use identifiers instead of magic numbers. Instead of doing this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int isnumber (char numb) {
constint NoConstSt = 48;
constint NoConstEn = 58;
int newno = numb;
if (newno >= NoConstSt && newno <= NoConstEn) return 1; //This returns one, which to the program will mean "this container holds a number".
elseif (newno == 45) return 2; //Means we do subtraction.
elseif (newno == 43) return 3; //Means we do addition.
elseif (newno == 42) return 4; //Means we do multiplication.
elseif (newno == 47) return 5; //Means we do division.
elseif (newno == 40) return 6; //Means we have an opening parantheses.
elseif (newno == 41) return 7; //Means we have a closing parantheses.
elseif (newno == 46) return 8; //Means we have a decimal.
elsereturn 0; //...
}
Rather than putting ascii codes for the characters you're looking for.. just use the characters!
1 2 3
if(newno == '-') /* do subtraction */
elseif(newno == '+') /* do addition */
elseif(newno == '.') /* have a decimal */
I would also even recommend making an enum instead of having magic numbers for the return function of isnumber. Also the name 'isnumber' is misleading because it checks for a lot more than numbers. Something like GetCharType or something would make more sense.
findbraces doesn't exist (yet). You define it below that line, so the compiler doesn't know that it exists yet.
I didn't know C++ functioned like that. I knew it went sequentially from top to bottom, main() to function call, but I didn't know that if you used a function before it was defined, it wouldn't work. Good point.
I've also never heard of a magic number, but thanks for pointing that out.
#include <iostream>
int addition(int, int);
int main(){
int i, j;
std::cout << "enter in your first number:";
std::cin >> i;
std::cout << "Enter in your seconds number:";
std::cin >> j;
std::cout << "Your lucky number is:" << addition(i, j);
return EXIT_SUCCESS; //same as return 0
}
int addition(int i, int j){
return i+j;
}
declaring the prototype on top of main and the executable code on the bottom means that all functions can reference each other regardless of the order you declare them in.