is this the best way to do this

Hi
Im new to programing and am not taking a course or any thing just using this site so I would be very great full if someone would tell me if this example is the best syntax for this situation


#include <iostream>
#include <string>
using namespace std;
int main()
{

int x = 1;
string y;

while (x == 1)
{
{ cout << "hello press start to play q to quit" << endl;
loop:
getline (cin, y);}

if (y == "s")
{x = x + 1;}

else if (y == "q" )
{ x = x+2;}

else
{cout << "Invalid input" << endl;
goto loop;}
}
while (x == 2)
{cout << "starting..." << endl;
return 0;}

while (x == 3)

{cout << "goodbye..." << endl;
return 0;}
}
This post should have been deleted.
Last edited on
I did some indentation fixing for you:
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
#include <iostream>
#include <string>
using namespace std;

int main() 
{
	int x = 1;
	string y;

	while (x == 1)
	{
		{
			cout << "hello press start to play q to quit" << endl; 
		loop:
			getline (cin, y);}

			if (y == "s")
			{
				x = x + 1;
			}
			else if (y == "q" )
			{
				x = x+2;
			}
			else
			{
				cout << "Invalid input" << endl;
				goto loop;
			}
		}
		while (x == 2)
		{
			cout << "starting..." << endl;
			return 0;
		}
		while (x == 3)
		{
			cout << "goodbye..." << endl;
			return 0;
		} 
	}


As you can see, it is MUCH easier to read, and you can clearly see that you are missing the closing brace for main. Also, I am curious why you used a nameless scope on line 12?
Im sorry when I was pasting it in I lost the last brace I know it works but just wanted to know if it was writen properly
closed account (z05DSL3A)
The use of goto is deprecated.
No it's not. It is just discouraged.
Nameless scopes are not commonly used. The only use I've seen them in is when a block of code only needs to be used once, the variable declarations are used once, and when it pulls data from variables outside of the scope. (a function is normally used for this, but there are exceptions)

Also, please use better titles :)
Last edited on
closed account (z05DSL3A)
ls909,

Don't be so hard on yourself. Don't be afraid to contribute and don't take offence if someone corrects you. It's all good.
@strongdrink: I use nameless scopes when I need unconditional code to execute with temporary stack variables. Since he is not making any temporary stack variables it seems odd :\ unless he just wanted to keep the "loop" label in its own scope?
Last edited on
ls909, him say:


You could use something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
while (true)
{
     cout << "Hello, press 's' to play, or 'q' to quit." << endl;
     cin >> y; // Why use getline()?
     cin.ignore(); // Clear the input buffer.
     if(y == "s")
     {
          x = x + 1;
     }
     if(y == "q")
     {
          x = x + 2;
     }
     if(y != "s" || "q")
     {
          continue;
     }
     else
     {
          break;
     }
}


But he became very shy :(
@LB yeah, pretty much what I was trying to say..
Topic archived. No new replies allowed.