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 :(
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/
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:
#include <iostream>
#include <string>
#include <sstream>
usingnamespace 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";
}
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?