i have my whole program written but when it prompts the user if they want to play again(y/n) there is a 1 after it and i cammot figure out why. any help would greatly appreciated
here is my program:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
using namespace std;

int main ()
{
	int numofgames;
	int compchoice;
	int choice;
	bool done;
	int noOfGuesses=0;
	int ncount=0;
	int sum=0;
	int noofgamesplayed=0;
	int avgNoOfGuesses=0;
	int tie=0;
	int win=0;
	int loss=0;

	char opt;

	//chosing how many games you would like to play welcome you to the game
	cout << "Please choose an ODD number of games you would like to play>";
	cin >> numofgames;
	cout << "Welcome to Rock paper scissors"; 
	cout << endl;
	cout << "You are playing against the computer";
	cout << endl;
	do 
	{
		done = false;
		noOfGuesses = 0;
		// player chooses rock, paper, scissors
		while ((noOfGuesses < numofgames) && (!done))
		{
			++noofgamesplayed;
			compchoice = (rand() + (int)time(0)) % 3;
			cout << "Please type 0 for rock, 1 for paper and 2 for scissors";
			cin >> choice;
			cout << endl;
			noOfGuesses++;
			if (choice == 0) 
			{
				if (compchoice == 0)//Rock
				cout << "It's a tie!" << tie++ << endl;
				else if (compchoice == 1)
					cout << "Paper covers rock! Sorry, you lose!" << loss++ << endl;

				else if (compchoice == 2)
					cout << "Rock pounds scissors! You win!" << win++ <<endl;
			}
			if (choice == 1)//Paper
			{
				if (compchoice == 0)
					cout << "Paper covers rock! You win!" << win++ << endl;
				else if (compchoice == 1)
					cout << "It's a tie!" << tie++ << endl;
				else if (compchoice == 2)
					cout << "Scissors cuts paper! Sorry, you lose!" << loss++ << endl;

			}
			if (choice == 2)//scissors
			{
				if (compchoice == 0)
					cout << "Paper covers rock! Sorry you lose" << loss++ << endl;

			else if (compchoice == 1)
					cout << "Scissors cuts paper! You win!" << win++ << endl;

				else if (compchoice == 2)
					cout << "It's a tie!" << tie++ << endl;
				sum+= noOfGuesses ;
			}
			if (choice > 2)
				cout << "Your guess is INVALID pick again!" << endl;
		}

		// you will be able to play the game again or stop
		cout<<"Would you like to try the game again <Y/N>? ";
		cout<< compchoice<<endl;
		cin>>opt;
		// shows the number of game the player had played, total number of guesses and the average of guessing

	} while( opt != 'n');
	cout<<"\n Number of Games played " <<noofgamesplayed;
	cout<<"\n Total number of guesses:" << numofgames;
	cout<<"\n Guessing Average:" <<numofgames/noofgamesplayed;
	cout<<"\n Number of Games Won:" << win;
	cout<<"\n Number of Games Lost:" << loss;
	cout<<"\n Number of Games Tied:" << tie;
	_getch();
	return 0;

}

Dani AI

Generated

The stray "1" after the prompt was exactly the symptom pointed out: a stray cout was printing the computer choice. Removing that line fixes the immediate visual glitch. A few other quick fixes and clarifications will make the program behave more predictably and avoid subtle bugs later.

  • Seed the RNG once at program start and generate choices with rand() % 3, or use the modern <random> facilities. Seeding inside the loop or mixing time() into the expression is unnecessary. Example (C-style):
srand(static_cast<unsigned int>(time(nullptr)));
int compchoice = rand() % 3;

See the rand/srand notes on cppreference: rand / srand.

  • For safer, higher-quality randomness use <random> and std::uniform_int_distribution:
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dist(0, 2);
int compchoice = dist(gen);

Reference: C++ <random> header.

  • Avoid streaming counters directly into messages with tie++ (or win++) because cout << tie++ prints the old numeric value. Increment counters on their own, then print only the message:
++tie;
cout << "It's a tie!" << endl;
  • Normalize the play-again input and handle case-insensitivity:
opt = static_cast<char>(std::tolower(static_cast<unsigned char>(opt)));
} while (opt != 'n');

(See tolower usage: tolower.)

  • Final stats: keep a proper totalGuesses accumulator and compute average with floating-point to avoid integer division:
double avg = totalGuesses / static_cast<double>(noofgamesplayed);

Also: remove platform-specific _getch() unless needed (or replace with std::cin.get() for portability), and decide clearly whether counters reset between replayed matches or accumulate across sessions.

These changes address the visible "1" and improve correctness, randomness, and user input handling for future maintenance.

Recommended Answers

All 2 Replies

>>and i cammot figure out why.
Because you told it to print it on line 82. Delete that line and it will be ok.

thank you very much. you are a great help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.