a couple simple things

heres my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 2 and 2 is 5

#include<iostream>
using namespace std;

int main()
{loop:
	int a,b,c;
	cout <<"Enter a number: ";
	cin >> a;
	cout <<"Enter a number: ";
	cin >> b;
	if (a==2 && b==2){
			c = 5;
			cout << a << " + " << b << " = " << c;
			cout << endl;}
	else {
	c = a + b;
	cout << a << " + " << b << " = " << c;
	cout << endl;}
	goto loop;
	return 0;
}


i want to know two things, how to do the same thing without using goto, the tutorial said that using goto is bad unless you absolutely have to, so there must be a way to keep the program running the same way it does now without using goto.

second, i am using visual studios 2005 and want to know how to finalize this to make it a .exe to send to my friend. i tried going to build and building it, but i dont know what that does. i went into the projects file and saw the exe of it, but when i sent it, my friend said it wouldnt work.
1st: You can use an infinite loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
    while(true){
        int a,b,c;
        cout <<"Enter a number: ";
        cin >> a;
        cout <<"Enter a number: ";
        cin >> b;
        if (a==2 && b==2){
                c = 5;
                cout << a << " + " << b << " = " << c;
                cout << endl;}
        else {
        c = a + b;
        cout << a << " + " << b << " = " << c;
        cout << endl;}
    }
	return 0;
}
NOTICE: this program will never end, you should put somewhere an option to stop your program

2nd: Try reading this: http://www.cplusplus.com/forum/beginner/10306/
Last edited on
Also, you have put:

1
2
3
if (a==2 && b==2) {
c = 5;
cout << a << " + " << b << " = " << c;


but 2 + 2 = 4 and not 5
yea thats a joke, about the novel 1984
Ah, I mistook that for something else, where there were four lights in a room.

"How many lights are there in the room?"

"Four."

*torture*

"How many now?"

"Four."

*torture*

This would go on and on and on and on until the torturee admitted there were five lights; then the torturer could do anything (mentally) to him; the victim was in essence broken.

Anyway:

The .exe file might require the object file to be in the same directory. I'm not sure; ask a Visual Studio expert.
Topic archived. No new replies allowed.