I need help with CPP

Posts 1–7 of 7 · Page 1 of 1
I need help with CPP
I am really cancer with cpp so I need some help. I made this shitty calculator but I don't know how to make sub integers when I to work out each group individually.

#include <iostream>
using namespace std;

int main(int argc, char** argv) {
int e;
cout << "Which shit you want to use? \n[add], [sub], [div], [mul]\n";

int add
{
//Addition
int a;
cout << "Choose 1st stupid gay number \n";
cin >> a;
int b;
cout << "Choose a 2nd gay number \n";
cin >> b;
int c = a + b;
cout << "The answer is...";
cout << c;
}

int sub
{
//Subtraction
int a;
cout << "Choose 1st stupid gay number \n";
cin >> a;
int b;
cout << "Choose a 2nd gay number \n";
cin >> b;
int c = a - b;
cout << "The answer is...";
cout << c;
}

int div
{
//Division
int a;
cout << "Choose 1st stupid gay number \n";
cin >> a;
int b;
cout << "Choose a 2nd gay number \n";
cin >> b;
int c = a / b;
cout << "The answer is...";
cout << c;
}

int mul
{
//Multiplication
int a;
cout << "Choose 1st stupid gay number \n";
cin >> a;
int b;
cout << "Choose a 2nd gay number \n";
cin >> b;
int c = a * b;
cout << "The answer is...";
cout << c;
}
return 0;
}
I don't even know how you can get this to run but you're declaring each of your operation functions inside main. Move them to outside of main and turn your main function into a menu

Code:
#include <iostream>
#include <string>

using namespace std;

int add()
{
}

int sub()
{
}

int div()
{
}

int mul()
{
}

int main(int argc, char** argv) {
	int e;
	string option;

	std::cout << "Which shit you want to use? \n[add], [sub], [div], [mul]\n";

	// Switch on the first letter of the users input and make sure the character is lowercase
	// i.e., if the user input Mul then 'M' will turn into 'm' and use the case 'm'
	switch (tolower(option[0]))
	{
		case 'a':
			add();
			break;
		case 's':
			sub();
			break;
		case 'd':
			div();
			break;
		case 'm':
			mul();
	}

	return 0;
}
Quote Originally Posted by InunoTaishou View Post
I don't even know how you can get this to run but you're declaring each of your operation functions inside main. Move them to outside of main and turn your main function into a menu

Code:
#include <iostream>
#include <string>

using namespace std;

int add()
{
}

int sub()
{
}

int div()
{
}

int mul()
{
}

int main(int argc, char** argv) {
	int e;
	string option;

	std::cout << "Which shit you want to use? \n[add], [sub], [div], [mul]\n";

	// Switch on the first letter of the users input and make sure the character is lowercase
	// i.e., if the user input Mul then 'M' will turn into 'm' and use the case 'm'
	switch (tolower(option[0]))
	{
		case 'a':
			add();
			break;
		case 's':
			sub();
			break;
		case 'd':
			div();
			break;
		case 'm':
			mul();
	}

	return 0;
}
It still doesn't work the way you said it. I just takes me straight to the end where it tells me to press any button to continue then it closes
Quote Originally Posted by CuntyLawrence View Post
It still doesn't work the way you said it. I just takes me straight to the end where it tells me to press any button to continue then it closes
Then it's doing exactly what it should be doing because I didn't put any code in the add, sub, div, or mul functions, you'll have to do those yourself.

To prevent the black window from closing put system("pause"); or cin.get(); before return 0; in main()

Code:
cin.get();

return 0;
Also, using the system() function is usually bad practice for anything you want to have someone else use but if you're just using it to test code then it's safe.
Quote Originally Posted by InunoTaishou View Post
Also, using the system() function is usually bad practice for anything you want to have someone else use but if you're just using it to test code then it's safe.
Let's use the correct terminology:
but if you're using it to test code
but if you're using it for debug-purposes
That sounds better. Also you shouldn't use system() just because it isn't portable.
p.s: Note that you shouldn't use using namespace std;! It's the worst thing you can do! @InunoTaishou @CuntyLawrence
Quote Originally Posted by InunoTaishou View Post
To prevent the black window from closing put system("pause"); or cin.get(); before return 0; in main()

Code:
cin.get();

return 0;
I prefer getchar();
I haven't done C++ in a while but another way to wait for a keystroke to continue is...

#include conio.h

/r code here
//...
getch();
Posts 1–7 of 7 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us