First script could I make it any better

Pages: 12
Hey everyone, new to this forum and C++ but im having fun anyway made this script could it be made any more efficent/better in terms of coding ?
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
#include <iostream>
#include <cstdlib>
#include <math.h>

using namespace std;
int num1;
int num2;
char operation;
int main(){
cout << "What would you like to do today ?" << endl <<
"+ - / * OR Q(quit)" << endl;
cin >> operation;
while(operation != 'Q'){
switch(operation) {
case '+' :
cout << "Enter first number you wish to add? " << endl;
cin >> num1;
cout << "+" << endl;
cin >> num2;
cout << "=" << num1 + num2;
break;
case '-':
cout << "Enter first number you wish to subtract? " << endl;
cin >> num1;
cout << "-" << endl;
cin >> num2;
cout << "=" << num1 - num2;
break;
case '/':
cout << "Enter first number you wish to Divide? " << endl;
cin >> num1;
cout << "/" << endl;
cin >> num2;
cout << "=" << num1 / num2;
break;
case '*':
cout << "Enter first number you wish to multiply? " << endl;
cin >> num1;
cout << "*" << endl;
cin >> num2;
cout << "=" << num1 * num2;
break;
default:
cout << "Didn't understand input!";
cin.ignore(100,'\n');
};
}
cout << "Goodbye!";
}
We call them programs; a script is something that is interpreted by an interpreter. A program is compiled into the actual machine instructions.

#include <math.h>
This being C++, we use <cmath> now.

using namespace std;
Namespace pollution is an ugly thing. Sticking this at the top of your code wrecks the whole point of namespaces.

1
2
3
int num1;
int num2;
char operation;

Putting these at the global scope level is a very bad habit to get into. They're all used within the scope of main, so put them in there.

main returns an int. Get into the habit of explicitly returning something.
Use indentation to make it easier to read your code.

I agree with everything Moschops says except for the last thing. There is no reason to have a return statement in main unless you return something else than 0.
Last edited on
Ok so ive made some changes and im not too sure why ive got undeclared variable errors on the cout, endl and cin at the begining of the code

Updated 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
45
46
47
48
#include <iostream>
#include <cstdlib>

int main(){
int num1;
int num2;
char operation;
cout << "What would you like to do today ?" << endl <<
"+ - / * OR Q(quit)" << endl;
cin >> operation;
    while(operation != 'Q'){
        switch(operation) {
        case '+' :
        cout << "Enter first number you wish to add? " << endl;
        cin >> num1;
        cout << "+" << endl;
        cin >> num2;
        cout << "=" << num1 + num2;
        break;
        case '-':
        cout << "Enter first number you wish to subtract? " << endl;
        cin >> num1;
        cout << "-" << endl;
        cin >> num2;
        cout << "=" << num1 - num2;
        break;
        case '/':
        cout << "Enter first number you wish to Divide? " << endl;
        cin >> num1;
        cout << "/" << endl;
        cin >> num2;
        cout << "=" << num1 / num2;
        break;
        case '*':
        cout << "Enter first number you wish to multiply? " << endl;
        cin >> num1;
        cout << "*" << endl;
        cin >> num2;
        cout << "=" << num1 * num2;
        break;
        default:
        cout << "Didn't understand input!";
        cin.ignore(100,'\n');
        };
    }
cout << "Goodbye!";
return EXIT_SUCCESS;
}
You get errors because you removed using namespace std; and didn't add std:: in front of all the standard functions, types and objects. std::cout, std::cin, ...

You indentation could be better.

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
#include <iostream>
#include <cstdlib>

int main(){
	int num1;
	int num2;
	char operation;
	std::cout << "What would you like to do today ?" << std::endl <<
	             "+ - / * OR Q(quit)" << std::endl;
	std::cin >> operation;
	while(operation != 'Q'){
		switch(operation) {
		case '+' :
			std::cout << "Enter first number you wish to add? " << std::endl;
			std::cin >> num1;
			std::cout << "+" << std::endl;
			std::cin >> num2;
			std::cout << "=" << num1 + num2;
			break;
		case '-':
			std::cout << "Enter first number you wish to subtract? " << std::endl;
			std::cin >> num1;
			std::cout << "-" << std::endl;
			std::cin >> num2;
			std::cout << "=" << num1 - num2;
			break;
		case '/':
			std::cout << "Enter first number you wish to Divide? " << std::endl;
			std::cin >> num1;
			std::cout << "/" << std::endl;
			std::cin >> num2;
			std::cout << "=" << num1 / num2;
			break;
		case '*':
			std::cout << "Enter first number you wish to multiply? " << std::endl;
			std::cin >> num1;
			std::cout << "*" << std::endl;
			std::cin >> num2;
			std::cout << "=" << num1 * num2;
			break;
		default:
			std::cout << "Didn't understand input!";
			std::cin.ignore(100,'\n');
		};
	}
	std::cout << "Goodbye!";
	return EXIT_SUCCESS;
}
Last edited on
Thank you everyone just another quick one when i break out of the case for + where does it take the program?
Im just getting a bit confused as It keeps taking back to the addition case!
Would I have wipe whatevers stored in the operation variable ?
break; takes you to the end of the switch (line 44).
Last edited on
Thankyou Peter87 ive added a few more things but the while loop ive added doesnt seem to be registering the yes or no inputed into the script at the moment ive got this
while(qu != 'N' || 'Y')
and from what ive learnt it should loop until a Y or N is inputed could you see why this wouldnt work ?
while(qu != 'N' || qu != 'Y')

My opinion is adding using namespace std; is fine, as long as your writing a simple program. You can also be more explicit by writing
1
2
using std::cout;
using std::cin;
Last edited on
Im still to read up on the namespace thing just going with adding std:: to everything at the moment any this is my whole script I want it to basically only carry on after the first calucation after the user has inputed if they want to or not
p.s i have only changed the addition case
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
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <cstdlib>

int main(){
	long num1, num2;
	char qu;
	char operation;
	std::cout << "What would you like to do today ?" << std::endl <<
	             "+ - / * OR Q(quit)" << std::endl;
	std::cin >> operation;
	while(operation != 'Q'){
		switch(operation) {
		case '+' :
			std::cout << "Enter first number you wish to add? " << std::endl;
			std::cin >> num1;
			std::cout << "+" << std::endl;
			std::cin >> num2;
			std::cout << "=" << num1 + num2 << std::endl << "More addition?(Y/N)" << std::endl;
            std::cin >> qu;
                while(qu != 'N' || qu != 'Y' ) {
                std::cout << "More addition?(Y/N)" << std::endl;
                std::cin.ignore(100,'\n');
                std::cin >> qu;
                }
                if(qu == 'Y'){
                    break;}else{
                    std::cout << "+ - / * OR Q(quit)" << std::endl;
                    std::cin >> operation;
                    break;
                    }
		case '-':
			std::cout << "Enter first number you wish to subtract? " << std::endl;
			std::cin >> num1;
			std::cout << "-" << std::endl;
			std::cin >> num2;
			std::cout << "=" << num1 - num2 << std::endl << "More subtraction?(Y/N)" << std::endl;
            std::cin >> qu;
                while(qu != 'N' || 'Y') {
                std::cout << "More subtraction?(Y/N)" << std::endl;
                std::cin.ignore(100,'\n');
                std::cin >> qu;
                }
                if(qu == 'Y'){
                    break;}else{
                    std::cout << "+ - / * OR Q(quit)" << std::endl;
                    std::cin >> operation;
                    break;
                    }
		case '/':
			std::cout << "Enter first number you wish to Divide? " << std::endl;
			std::cin >> num1;
			std::cout << "/" << std::endl;
			std::cin >> num2;
			std::cout << "=" << num1 / num2 << " remainder "<< num1%num2 << std::endl << "More division?(Y/N)" << std::endl;
            std::cin >> qu;
                while(qu != 'N' || 'Y') {
                std::cout << "More division?(Y/N)" << std::endl;
                std::cin.ignore(100,'\n');
                std::cin >> qu;
                }
                if(qu == 'Y'){
                    break;}else{
                    std::cout << "+ - / * OR Q(quit)" << std::endl;
                    std::cin >> operation;
                    break;
                    }
		case '*':
			std::cout << "Enter first number you wish to multiply? " << std::endl;
			std::cin >> num1;
			std::cout << "*" << std::endl;
			std::cin >> num2;
			std::cout << "=" << num1 * num2 << std::endl << "More multiplication?(Y/N)" << std::endl;
            std::cin >> qu;
                while(qu != 'N' || 'Y') {
                std::cout << "More multiplication?(Y/N)" << std::endl;
                std::cin.ignore(100,'\n');
                std::cin >> qu;
                }
                if(qu == 'Y'){
                    break;}else{
                    std::cout << "+ - / * OR Q(quit)" << std::endl;
                    std::cin >> operation;
                    break;
                    }
		default:
			std::cout << "Didn't understand input!";
			std::cin.ignore(100,'\n');
			std::cin >> operation;
		};
	}
	std::cout << "Goodbye!";
	return EXIT_SUCCESS;
}
Think of a namespace as a last name. If I say "Stan," it's ambiguous; there might be many "Stan's." But, if I say "Stan Smith," it's unambiguous.; you know exactly which "Stan" I'm talking about. (Of course, in C++ there can be only one Stan in the Smith family, and all Smith's are in the same family. Also, "Stan Smith" would be written Smith::Stan.)

So by saying using namespace Smith; Your saying, look for all identifiers (names) in both the global namespace (the family that has no last name) as well as the Smith namespace (Smith family.)
Saying using Smith::Stan says saying "Stan" from now on is enough to find Smith::Stan (Stan Smith.)

As for your problem, I suggest taking care to indent your program better, and maybe comment which control structures those breaks are for. That might help you follow your own logic better so you can debug the program. I see a few places were your writing redundant code.

C++ programs are not "scripts." A script is interpreted/parsed. C++ is compiled into machine code, i.e. a natively executable file/program.
Last edited on
Ok so ill comment on the code to show you where its not working and thank you for that explanation very helpfull im going to purchase a c++ book soon I found that, that helped with php anyway
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <cstdlib>

int main(){
	long num1, num2;
	char qu;
	char operation;
	std::cout << "What would you like to do today ?" << std::endl <<
	             "+ - / * OR Q(quit)" << std::endl;
	std::cin >> operation;
	while(operation != 'Q'){
		switch(operation) {
		case '+' :
			std::cout << "Enter first number you wish to add? " << std::endl;
			std::cin >> num1;
			std::cout << "+" << std::endl;
			std::cin >> num2;
			std::cout << "=" << num1 + num2 << std::endl << "More addition?(Y/N)" << std::endl;
            std::cin >> qu;
                while(qu != 'N' || qu != 'Y' ) { //ok so this is where is think it is all going wrong whats supposed to happen
                        std::cout << "More addition?(Y/N)" << std::endl; //The while loop is supposed to only let the user continue
                        std::cin.ignore(100,'\n');                       //if they input a Y or a N but its doesnt seem to work and 
                        std::cin >> qu;                                  //neither does the IF below, i have a feeling its not recognising
                    }                                                    //whats been inputed and mathing it to the 'N' and 'Y' 
                if(qu == 'Y'){
                        break;}else{
                        std::cout << "+ - / * OR Q(quit)" << std::endl;
                        std::cin >> operation;
                        break;
                    }
		case '-':
			std::cout << "Enter first number you wish to subtract? " << std::endl;
			std::cin >> num1;
			std::cout << "-" << std::endl;
			std::cin >> num2;
			std::cout << "=" << num1 - num2 << std::endl << "More subtraction?(Y/N)" << std::endl;
            std::cin >> qu;
                while(qu != 'N' || 'Y') {
                std::cout << "More subtraction?(Y/N)" << std::endl;
                std::cin.ignore(100,'\n');
                std::cin >> qu;
                }
                if(qu == 'Y'){
                    break;}else{
                    std::cout << "+ - / * OR Q(quit)" << std::endl;
                    std::cin >> operation;
                    break;
                    }
		case '/':
			std::cout << "Enter first number you wish to Divide? " << std::endl;
			std::cin >> num1;
			std::cout << "/" << std::endl;
			std::cin >> num2;
			std::cout << "=" << num1 / num2 << " remainder "<< num1%num2 << std::endl << "More division?(Y/N)" << std::endl;
            std::cin >> qu;
                while(qu != 'N' || 'Y') {
                std::cout << "More division?(Y/N)" << std::endl;
                std::cin.ignore(100,'\n');
                std::cin >> qu;
                }
                if(qu == 'Y'){
                    break;}else{
                    std::cout << "+ - / * OR Q(quit)" << std::endl;
                    std::cin >> operation;
                    break;
                    }
		case '*':
			std::cout << "Enter first number you wish to multiply? " << std::endl;
			std::cin >> num1;
			std::cout << "*" << std::endl;
			std::cin >> num2;
			std::cout << "=" << num1 * num2 << std::endl << "More multiplication?(Y/N)" << std::endl;
            std::cin >> qu;
                while(qu != 'N' || 'Y') {
                std::cout << "More multiplication?(Y/N)" << std::endl;
                std::cin.ignore(100,'\n');
                std::cin >> qu;
                }
                if(qu == 'Y'){
                    break;}else{
                    std::cout << "+ - / * OR Q(quit)" << std::endl;
                    std::cin >> operation;
                    break;
                    }
		default:
			std::cout << "Didn't understand input!";
			std::cin.ignore(100,'\n');
			std::cin >> operation;
		};
	}
	std::cout << "Goodbye!";
	return EXIT_SUCCESS;
}
while(qu != 'N' || qu != 'Y' )

When will this EVER not be true?

If qu is N, then the one on the right is true, so the whole OR statement is true.
If qu is Y, then the one on the left is true, so the whole OR statement is true.
If qu is something else, then the one on the left is true and the one on the right is true , so the whole OR statement is true.

This will always come out as true so this will loop forever.
What if you typed asfdafsadf in ??
qu is a char so it will only be one char at a time
ryt ok if i typed A in ?
Let's check, shall we.

qu is A.

A is not N, so qu != 'N' comes out as TRUE.
A is not Y, so qu != 'Y' comes out as TRUE.

while(qu != 'N' || qu != 'Y' ) is thus
while(TRUE || TRUE)
which will always come out as TRUE.

while(qu != 'N' || qu != 'Y' ) will loop forever.



I thought if you had qu != 'N' || qu != 'Y' and qu is N or Y then it would be false and the script would continue. IF anything else was entered it would loop till Y or N was inputed.

The != does return true if they dont match ?
I thought if you had qu != 'N' || qu != 'Y' and qu is N or Y then it would be false and the script would continue.

That's incorrect. If qu is N or Y, only one side of the || is false. The other side is TRUE, and when an || has TRUE on one side, the final answer is TRUE.

The != does return true if they dont match ?

That's correct.

qu != 'N' || qu != 'Y'

If qu is N, then the one on the right is true, so the whole OR statement is true.
If qu is Y, then the one on the left is true, so the whole OR statement is true.
If qu is something else, then the one on the left is true and the one on the right is true , so the whole OR statement is true.
Ah lmao I was tired when writing this and still am now lol thanks for your help but i cant see of a way to get round this is there some sort of match function that will take the variable and check if it is equall to Y or N?
Pages: 12