Help ! Error correction.

This is a simple rock, paper, scissors game written by me. But whenever I select 1,2,3 the program terminates abrubtly and when I press 4 the results are shown as 0 0 because when I start playing it terminates the program or leads to an infinite loop. I can't rectigy the issue.
So, I hope I will get the help from here as soon as possible

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// This program plays you in a game of Rock/Paper/Scissors
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<process.h>
int scomp,suser;
void result()
{
    cout<<"______________________________________\n";
    cout<<"\n             Result";
    cout<<"Your Score : "<<suser;
    cout<<"Computers Score : "<<scomp;
    if(suser>scomp)
    {
	cout<<"Congratulations ! You Win.";
    }
    else if(suser<scomp)
	{
		cout<<"Computer Wins.";
	}
	else if(suser==scomp)
	     {
		cout<<"It's a Tie";
	     }
}
void select(int choice)
{
    switch(choice)
    {
	case 1:                                      //rock
	{
		cout<<"You picked Rock.\n";
		cout<<"Now here was my choice.\n";
		break;
	}
	case 2:                                      //paper
	{
		cout<< "You picked Paper.\n";
		cout<< "Now here was my choice.\n";
		break;
	}
	case 3:					     //scissor
	{
		cout<<"You picked Scissors.\n";
		cout<<"Now here was my choice.\n";
		break;
	}
	case 4:
		result();
	default: cout<<"Error ! Wrong Choice.";
    }
}
void cselect()
{
    unsigned seed;
    int comp;
    time_t t;
    seed =(unsigned)time(&t);
    srand(seed); //For the random generator.
    comp = 1 + rand()%3; //Computers pick.
    switch(comp)
    {
	case 1:
	{
		cout<<"Rock\n";
		break;
	}
	case 2:
	{
		cout<<"Paper\n";
		break;
	}
	case 3:
	{
		cout<<"Scissor\n";
	}
    }
    clrscr();
}
void main()
{
    clrscr();
    int choice,comp,suser=0,scomp=0;
    int i;
    cout<<"Rock/Paper/scissors is a simple game. The rules are: \n Rock beats scissors. \n Scissors beats Paper. \n Paper beats rock.\n";
a:
    cout<<"Game Choices.\n\n";
    cout<<"1. Rock\n";
    cout<<"2. Paper\n";
    cout<<"3. Scissors\n";
    cout<<"4. Quit, exits the game.\n";
    cout<<"\nPlease enter your choice : ";
    cin>> choice;
    select(choice);
    if(choice!=1 && choice!=2 && choice!=3)
    {
	   goto a;
    }
    cselect();
    if((choice==1 && comp==2 )||(choice==3 && comp==1)||(choice==2 && comp==3))
    {
	scomp++;
    }
    if((choice==2 && comp==1 )||(choice==1 && comp==3)||(choice==3 && comp==2))
    {
	suser++;
    }
}
Hello Sunny2612,

Welcome to the forum.

If this compiles for you then you have a very old compiler that needs to be updated. I would suggest something that covers the C++11 standards.

As is you program does not compile for me.

"iostream.h" is not a proper header file in C++ and probably not in C either.

"conio.h" is also not a C++ header file and not available to all compilers. Nor is it standard C++ use.

"stdlib.h" and "time.h" would be better as "cstdlib" and "ctime".

I am not sure what you would need "process.h" for or what would be the best C++ replacement.

The function "clrscr()" may work for you, but this does not work for everyone.

Once I clean up the problems then I can test the program.

Hope that helps,

Andy
Hello Sunny2612,

While working to get the program to compile one of the errors I received was for the use of an uninitialized variable, "comp". It is good practice to initialize all variables when they are defined. Most variables like "int"s, "double"s, "char" should be initialized when defined. The use of a set of empty {}s after the variable name is all that is need. "std::string", "std::vector" and a couple of others are the exceptions.

The use of "goto" is also bad programming. This can be replaced with a do/while loop like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	do
	{
		CLS;

		std::cout << "Rock/Paper/scissors is a simple game. The rules are: \n Rock beats scissors. \n Scissors beats Paper. \n Paper beats rock.\n";

		std::cout << "Game Choices.\n\n";
		std::cout << "1. Rock\n";
		std::cout << "2. Paper\n";
		std::cout << "3. Scissors\n";
		std::cout << "4. Quit, exits the game.\n";
		std::cout << "\nPlease enter your choice : ";
		std::cin >> choice;
		select(choice);

		if (choice != 1 && choice != 2 && choice != 3 && choice != 4)
		{
			std::cout << "\n Invalid choice. Try again.";
			std::this_thread::sleep_for(std::chrono::seconds(3));  // Requires header files "chrono" and "thread"
		}

	} while (choice != 1 && choice != 2 && choice != 3 && choice != 4);


The "CLS;" is my replacement for your "clrscr()".

Line 19. I used to pause long enough to read the message. The whole number in ()s is the amount of seconds to pause.

The function "cselect()" looks OK, but you get the computers pick and then loose it when the function ends. This function needs to return an int and return comp just before the closing brace of the function.

Back in main the if statements that start t line 101 in your code do not cover all possibilities. I am thinking this should be if/else if statements and you mab be able to do everything with one set of if/else if statements.

I am not worried about it now, but eventually most of what is in main would need to be inside a while loop with the option to continue at the end of the while loop. This way the game would continue until you type "N" to end.

Hope that helps,

Andy
Actually I'm in 11th std and have just started learning C++ this year. In our college we have turbo C++ compiler and Yes, that's an old one.
Registered users can post here. Sign in or register to post.