First real program

I have been doing c++ for about 10 days now and I made this little calculator
Sorry, but I think there are alot of spelling mistakes, im from Norway and im only like 14 so sorry :3
Have some error errors (o.O) hope one of you can help me fix it :)!
Anyways:

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
// Made by: Hippi_jump 
// Date: 24.04.2013
#include <iostream>
using namespace std;

float a;
float b;
char methode;
int main() {
	// Removing the +e001 +e002 etc (think +e001 means 10^2 or something...)
	cout.precision(100);
	
cout << "Enter two numbers and species (hope it's 'species' in englsh)"<<endl<<"Like (4 + 5 or 1 / 4)"<<endl;
		cout << "> ";
			cin >> a;
			cin >> methode;
			cin >> b;
			if(methode == '+') {
				cout <<endl<<a+b<<endl;
				main();
			}
			if(methode == '-') {
				cout <<endl<<a-b<<endl;
				main();
			}
			if(methode == '*') {
				cout <<endl<<a*b<<endl;
				main();
			}
			if(methode == '/') {
				cout <<endl<<a/b<<endl;
				main();
			}
			
			// Error Codes
			// Still errors, trying to fix it..
			if(!(cin>>a||cin>>methode||cin>>b)) {
				cout << "Error: You have not used the rigth format"<<endl;
				int main();
			}
			if(methode != '+' || methode != '+' || methode != '+' || methode != '+') {
				cout << "Error: The second subsection should be +,-,* or /"<<endl;
				int main();
			}
			else {
				cout << "Error: Can't not recognize the error"<<endl;
				int main();
			}
	cin.get();
	return 0;
}
Last edited on
What are the errors that you are getting?

The only thing the compiler complains about for me is that you call main().
warning: ISO C++ forbids taking address of function ‘::main’ [-pedantic]

A program is not really allowed to call main(). It's better to use a loop to repeat the program.
btw if you wish to use a loop you could do this:

1
2
3
4
5
6
7
8
9
10
11
while(1) {
  //code goes in hear
  //if you want to skip to the next loop part if you detect an error do this:

  continue;
  //otherwise just use

  break;

  //to exit the loop if everything went ok.  
}


The other thing you could do is put this line before the code you want to jump back to like this:

1
2
3
int main() {
  label_name_here:
  ... //rest of code 


then when you want to go back there, like on an error detection, just do this:

goto label_name_here;
The error seems to be when adding numbers with decimals (i.e. 1.1 +1), something to do with adding of the float type numbers. If I'm correct, removing the cout.precision(100); will fix the problem
Last edited on
I am not getting any debugging errors, but the errors act's strange. Sometimes they make an everlasting loop, and other times they don't output anything. I have nooo idea whats going on (running win7 and using msVisual2010express ass IDE)

And by the main(), thanks for telling me this, loops would be a much smarter solution. But why is calling the main() function so bad? (just wondering)
Calling main() is bad because the C++ standard (the document that specifies everything about C++) says so.
§3.6.1/3
The function main shall not be used within a program.


1
2
3
4
if(!(cin>>a||cin>>methode||cin>>b)) {
	cout << "Error: You have not used the rigth format"<<endl;
	int main();
}
This will try to read a, methode and b again. You probably want to put this where you have line 15-17. If you want it to print the error message when at least one of the inputs failed you could change || to &&. You can chain many >> after each other so a better way is probably to write if(!(cin >> a >> methode >> b)) {.

If you want to be able to read new input after an input operation has failed you will have to clear the error flags by doing cin.clear(); and also get rid of the invalid input. You can use the ignore function to do that.
1
2
// Ignores one character.
cin.ignore();
1
2
 // Ignores the whole line.
cin.ignore(numeric_limits<streamsize>::max(), '\n');
Last edited on
Topic archived. No new replies allowed.