Simple errors which shouldn't be happening

closed account (yCf9216C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<string>
#include<iostream>
#include<sstream>
#include<vector>
using namespace std;

int isnumber (char numb) {
	const int NoConstSt = 48;
	const int 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".
	else if  (newno == 45) return 2; //Means we do subtraction.
	else if (newno == 43) return 3; //Means we do addition.
	else if (newno == 42) return 4; //Means we do multiplication.
	else if (newno == 47) return 5; //Means we do division.
	else if (newno == 40) return 6; //Means we have an opening parantheses.
	else if (newno == 41) return 7; //Means we have a closing parantheses.
	else if (newno == 46) return 8; //Means we have a decimal.
		
	else return 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";
		}
		else if (isnumber(originalstr.at(h)== 6) {
				start = h;
			}
			else if (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";
			return true;
		}
		else if ( retno >= 1 && retno <= 5) { cout << "Number or normal operator found at: " << t << "\n"; }
		else if (t == thestr.length() - 1) { return false; }
		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) {
	const int NoConstSt = 48;
	const int 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".
	else if  (newno == 45) return 2; //Means we do subtraction.
	else if (newno == 43) return 3; //Means we do addition.
	else if (newno == 42) return 4; //Means we do multiplication.
	else if (newno == 47) return 5; //Means we do division.
	else if (newno == 40) return 6; //Means we have an opening parantheses.
	else if (newno == 41) return 7; //Means we have a closing parantheses.
	else if (newno == 46) return 8; //Means we have a decimal.
		
	else return 0; //...
}


Rather than putting ascii codes for the characters you're looking for.. just use the characters!

1
2
3
if(newno == '-') /* do subtraction */
else if(newno == '+') /* do addition */
else if(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.
Last edited on
else if (isnumber(originalstr.at(h)== 6) {

I see three of these "(" and only two of these ")"
Last edited on
closed account (yCf9216C)
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.
it is standard to define functions as such.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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.
closed account (yCf9216C)
Then how come it allows you to do what I did?
Then how come it allows you to do what I did?


It didn't. You had errors.
Topic archived. No new replies allowed.