my first program

This is my first true program who i made 80% myself 20% from tutorials , i'm newbie in C++, so what is your opinion about it? ,i made it in code blocks.
I just want your opinion on it , it's count for me to become a better programmer , i don't understand anymore after i writed so for my is like magic i just write this.

#include <iostream>

using namespace std;

int main()
{
int a = 0;
int b = 0;
char err;
char sign;
cout << "SIMPLE CALCULATOR VERSION V2 (by andreiz112dn)" << endl;
cout << "PLEASE ENTER THE SIGN FOR CALC" << endl;
cout << "SIGNS : (* , / , + , -)" << endl;
cout << "sign >";
cin >> sign;
cout << "NUMBER I > ";
cin >> a;
cout << "NUMBER II > ";
cin >> b;

switch(sign)
{
case '*' : cout << "THE RESULT IS " << a * b << endl;
break;
case '/' : cout << "THE RESULT IS " << a / b << endl;
break;
case '+' : cout << "THE RESULT IS " << a + b << endl;
break;
case '-' : cout << "THE RESULT IS " << a - b << endl;
break;
default:
cout << "ERROR!" << endl;
}
cout << "TRY AGAIN? , WRITE [y] OR [n]" << endl;
cout << ">";
cin >> err;
switch(err)
{
case 'y' :

do
{
cout << "PLEASE ENTER THE SIGN FOR CALC" << endl;
cout << "SIGNS : (* , / , + , -)" << endl;
cout << "sign >";
cin >> sign;
cout << "NUMBER I > ";
cin >> a;
cout << "NUMBER II > ";
cin >> b;
switch(sign)
{
case '*' : cout << "THE RESULT IS " << a * b << endl;
break;
case '/' : cout << "THE RESULT IS " << a / b << endl;
break;
case '+' : cout << "THE RESULT IS " << a + b << endl;
break;
case '-' : cout << "THE RESULT IS " << a - b << endl;
break;
default:
cout << "ERROR!" << endl;
}
cout << "TRY AGAIN? , WRITE [y] or [n]" << endl;
cout << ">";
cin >> err;
} while (1 < 2);

}
return 0;
}
Last edited on
for your first program, looks great. Most people do something much smaller for their first couple, so you are off to a great start.

Hello Andreiz112dn,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

To start with line 3:

Try to avoid using using namespace std; in your programs it may seem easy now, but WILL get you in trouble some day.

It is better to learn to qualify what is in the standard name space with "std::" and then to learn what is in the standard name space now while it is easy.

What you are most likely to use for now is "std::cout", "std::cin" and "std::endl". About a week or so of typing this and you will not even notice that you are doing it.

A few blank lines here an there to break thing up makes the code easier to read. As an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	int a = 0;
	int b = 0;
	char err;
	char sign;
	std::cout << "SIMPLE CALCULATOR VERSION V2 (by andreiz112dn)" << std::endl;
	std::cout << "PLEASE ENTER THE SIGN FOR CALC" << std::endl;
	std::cout << "SIGNS : (* , / , + , -)" << std::endl;
	std::cout << "sign >";
	std::cin >> sign;
	std::cout << "NUMBER I > ";
	std::cin >> a;
	std::cout << "NUMBER II > ";
	std::cin >> b;

	switch (sign)

A blank line after line 6 says the "std::cout" is a new section so to speak.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
	int a = 0;
	int b = 0;
	char err;
	char sign;

	std::cout << "SIMPLE CALCULATOR VERSION V2 (by andreiz112dn)" << std::endl;
	std::cout << "PLEASE ENTER THE SIGN FOR CALC" << std::endl;
	std::cout << "SIGNS : (* , / , + , -)" << std::endl;
	std::cout << "sign >";
	std::cin >> sign;
	std::cout << "NUMBER I > ";
	std::cin >> a;
	std::cout << "NUMBER II > ";
	std::cin >> b;

	switch (sign)


You have initialized the "int"s, but not the "char"s.

It is ALWAYS a good practice and programming to initialize your variables. If your compiler is using the C++11 standards or after the easiest way to initialize variables is with empty {}s, e.g., int num{};. This will initialize the variable to 0 (zero) or 0.0 for a double. A "char" will be initialized to "\0". "std::string"s are empty to start with an do not need initialized. Should you need to you can put a number between the {}s.You can also Initialize an array, e.g., int aNumbers[10]{};. This will initialize all elements of the array to 0 (zero). A use of int aNumbers[10]{ 1 }; will initial the first element to "1" and the rest of the array to "0". Following the "1" with ", 2 ..." will initialize the array with the numbers that you have used up to the entire array.

Looking over your code it looks OK for now until I get a chance to compile and run it. Then I will most likely have more to say.

One thing I did notice is the condition of the do/while loop. "(1 < 2)" will always be true, so what you have is an endless loop here. Until I dig into the program more I am not sure what to change it to right now.

Hope that helps,

Andy

Edit:
Last edited on
Hello Andreiz112dn,

After I made the changes I mentioned and started running the program I noticed that there is no way to end the program.

This shows the changes I mad to the beginning of the program. Notice the comments.

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
#include <iostream>
#include <cctype>  // <---Added this for the toupper and tolower functions. There are others that are useful.

//using namespace std;  / <--- Best not to use.

int main()
{
	int a = 0;
	int b = 0;
	char err{};  // <--- Initialized variable.
	char sign{};  // <--- Initialized variable.

	std::cout << "SIMPLE CALCULATOR VERSION V2 (by andreiz112dn)" << std::endl;
	std::cout << "PLEASE ENTER THE SIGN FOR CALC" << std::endl;
	std::cout << "SIGNS : (* , / , + , -, (E)nd): ";  // <--- Changed. You may not like the way it is displayed. That is OK.
	std::cout << "sign > ";  // <--- Added space after >. And in other places in the program.
	std::cin >> sign;
	sign = std::toupper(sign);  // <--- Added.

	if (sign == 'E') return 0;  // <--- Added.

	std::cout << "NUMBER I > ";
	std::cin >> a;
	std::cout << "NUMBER II > ";
	std::cin >> b;

	switch (sign)  // <--- You could put this in a function and call the function when needed. 


The code for the first switch, "switch(sign)" could be put in a function, which is what I would do, and then call the function when needed.

Still not sure what to do about the do/while loop, but you might consider something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
	char choice{};
	bool cont{ trrue };

	do
	{
		// Your code here.

		switch (choice)
		{
			case 'E':
				cont = false;
				break;
			default:
				break;
		}
	} while (cont);
}


This will need some changes when you ask ""TRY AGAIN? , WRITE [Y] or [N]". And when it comes to the [Y] or [N] part be consistant with the letters. Eithar use all capital or all lower case lettere in your program. Otherwise you will confuse the user. Hense the use of "std::toupper" or "std::tolower".

Over all the program is perty good, but there are some ways to condense it and some parts that need to be added like a way to end the program.

Hope that helps,

Andy
Hello Andreiz112dn,

I must apologize, my bad I referred to being consistent I was looking at "TRY AGAIN? , WRITE [y] OR [n]". Some how or somewhere I thought I say one set of "y/n"s in lowercase letters and the other in uppercase letters. Later I came to realize I was wrong.

Andy
Topic archived. No new replies allowed.