easy question

hey, ive programmed for years using qb, delphi, php, vb and various other scripting languages however it occoured to me that if im going to be serious about programming as my occupational choice (none yet still in highschool) i need to learn c++. From what ive learned so far all languages are fairly similar in how they are written with a few syntax variations however c++ seems to be the strictest ive encountered so far, to get familiar with c++ way of things my first program is pretty simple - the idea is that you can enter any adding calculation to the input line (for example 11+2), it finds the + by itself, seperates the numbers and outputs their sum however ive ran into an error which im unable to debug by myself :(

heres the code:
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
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void main() {
	
	int a;
	int b;
	int pos;
	int result;
	int i;
	string input;

	cout << "Gief calculation! \n> ";
	getline (cin, input);
	for (i=0; i<=20; i++) {
		if ( input[i] == '+' ) {
			pos = i;
		}
	}
	//a = mid(input, 0, pos-1);
	//b = mid(input, pos+1, length(input)-(pos+1));
	//result = addnumbers(a, b);
	cout << "\n" << result;




}

int addnumbers(int a, int b) {
	return a+b;
}
string mid(string text, int start, int lengths) {
	string output;
	int dist=lengths-start;
	int i;
	for (i=start;i<=dist;i++) {
		output = output + text[i];
	}
	return output;
}


the 3 lines that ive commented out are the ones that give me an error, why or what it is i have no idea so please help.
The function returns a string, but you're trying to store it as an int.
http://www.cplusplus.com/reference/clibrary/cstdlib/atoi.html
Last edited on
it still gives an error

well i found it - apparently functions used somewhere else need to be created before use, funny how there is no mention of that anywhere :p
Last edited on
Yes, function prototypes should appear before main. I think you might still have trouble with this code, though,
because in some parts you treat input as a string and in other parts (line 19 for example) you treat it as a c-string.
Instead of searching through an array of characters, you could use a string operation like find().
http://www.cplusplus.com/reference/string/string/
Last edited on
yea i had a whole bunch of problems there but figured them all out by now, what bothers me the most tho is that most of c++ documentation is really obscure about how to use different commands and such.. anyway heres the working version:

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
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int addnumbers(int a, int b) {
	return a+b;
}
int mida(string texts, int start, int lengths) {
	int i;
	int j=start+lengths;
	string num;
	int num2;
	for (i=start; i <= j; i++) {
		num=num+ texts[i];
	}
	num2=atoi(num.c_str());
	return num2;
}
void main() {
	
	int a=5;
	int b=5;
	int pos;
	int result;
	int i;
	string test;
	string input;

	cout << "Gief calculation! \n> ";
	getline (cin, input);
	for (i=0; i<=input.length(); i++) {
		if ( input[i] == '+' ) {
			pos = i;
		}
	}
	a = mida(input, 0, pos-1);
	b = mida(input, pos, input.length()-pos);
	result = addnumbers(a, b);
	cout << "\n" << result << "\n";
}
ok, moving on im now checking out arrays

1
2
3
4
5
6
7
8
void main() {
	int myarray[2];

	myarray[0]=12;
	myarray[1]=13;
	myarray[2]=myarray[0]+myarray[1];
	cout << myarray[2];
}


even tho it works and displays 25 in dos window an error message pops up and says stack around myarray was corrupted, why is that and what does that mean?
myarray[2] is an array containing 2 elements.

You are accessing 3 elements -- indexes 0, 1, and 2.

myarray[2] does not exist inside the array.

int myarray[3];
Topic archived. No new replies allowed.