Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ Rock-paper-Scissors
New Posts  All Forums:Forum Nav:

C++ Rock-paper-Scissors

post #1 of 12
Thread Starter 
Welp I'm building a basic 2 user input rock paper scissor, and I seem to have a problem getting it to take both inputs and running the if loops.

i have tried it with and without the switch's in it.
Code:
#include <iostream>
using namespace std;

int main ()
{
int ch1, ch2, ans;
do
{

cout << "Player 1 type R for rock, S for scissors, or P for paper, and press enter." << endl;
cin >> ch1;

switch (ch1)
{
case 'R':
case 'r':
break;
case 'P':
case 'p':
break;
case 'S':
case 's':
break;
}


cout << "Player 2 type R for rock, S for scissors, or P for paper, and press enter." << endl;
cin >> ch2;

switch (ch2)
{
case 'R':
case 'r':
break;
case 'P':
case 'p':
break;
case 'S':
case 's':
break;
}

if ((ch1 == 'R') || (ch1== 'r'))
{
if ((ch2== 'R') || (ch2== 'r'))
cout << "Tie no one wins!" << endl;
else if ((ch2 == 'P') || (ch2== 'p'))
cout << "Player 2 wins, paper covers rock!" << endl;
else if ((ch2== 'S') || (ch2== 's'))
cout << "Player 1 wins, rock breaks scissors!" << endl;
}
else if ((ch1 == 'P') || (ch1== 'p'))
{
if ((ch2== 'P') || (ch2== 'p'))
cout << "Tie no one wins!" << endl;
else if ((ch2 == 'R') || (ch2== 'r'))
cout << "Player 1 wins, paper covers rock!" << endl;
else if ((ch2== 'S') || (ch2== 's'))
cout << "Player 2 wins, scissors cut paper!" << endl;
}
else if ((ch1 == 'S') || (ch1== 's'))
{
if ((ch2== 'S') || (ch2== 's'))
cout << "Tie no one wins!" << endl;
else if ((ch2 == 'P') || (ch2== 'p'))
cout << "Player 1 wins, scissors cut paper!" << endl;
else if ((ch2== 'R') || (ch2== 'R'))
cout << "Player 2 wins, rock breaks scissors!" << endl;
}
cout << "If you want to run the program again press Y" << endl;
cin >> ans;
system ("pause");
} while (ans == 'y' || ans == 'Y');

return 0;
}
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
post #2 of 12
Good luck.I'm currently doing this in my Java class.I'd try to help but I just started my assignment,and of course I have no C++ experience either.biggrin.gif
    
CPUMotherboardGraphicsRAM
Amd Phenom II X4 GigaByte 7770 Kingston HyperX Blu 4GB 
Hard DriveOptical DriveOSMonitor
Raid 0 2 500Gb Seagate 7200 Lite-On DVD Drive W7 Asus VE245 24'' 1920x1080 
PowerCase
650w Cooler Master 310 Elite Mid-Tower Case 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Amd Phenom II X4 GigaByte 7770 Kingston HyperX Blu 4GB 
Hard DriveOptical DriveOSMonitor
Raid 0 2 500Gb Seagate 7200 Lite-On DVD Drive W7 Asus VE245 24'' 1920x1080 
PowerCase
650w Cooler Master 310 Elite Mid-Tower Case 
  hide details  
Reply
post #3 of 12
Why do u need the switch??
If u gonna use if statement then u dont need the switch.
post #4 of 12
Thread Starter 
Quote:
Originally Posted by TLCH723;12734328 
Why do u need the switch??
If u gonna use if statement then u dont need the switch.

Ok I only put that in to see if it would get it to run the loops but it doesn't.

It receives the first input then prints the next cout statement then ends.
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
post #5 of 12
Maybe take out the do-while and make it a recursive call that restarts if "y" and exits if anything else? I didn't use do-whiles a lot, I preferred recursion lol.

Just for safety, I'd add an error checker in your switches, so that anything else will go back to the cout/cin and not mess it up down the line.

I also noticed you declared characters as ints. I had problems with some programs trying to do that, but I was using an old, open source, outdated compiler :x I'm a bit rusty on this, I wanted to brush up on my knowledge so I'm not sure if that would help, but try declaring them as chars.

Anyway, besides that it looks pretty clean, and it should work. I'd try myself and compile it, but I don't have a compiler on my computer yet.
Proteus
(17 items)
 
 
Ordinary Toaster
(10 items)
 
CPUMotherboardGraphicsRAM
i5 2500k P67 Extreme 4 Gen 3 GTX 780 16GB Kingston HyperX Beast 
Hard DriveOptical DriveCoolingCooling
Crucial BX200 Cheap something that writes DVD-RW Hyper 212 Evo Cooler Master SickleFlow 120 
OSMonitorKeyboardPower
Windows 10 Pro 2x Asus VW246H Corsair Strafe MX Brown Corsair HX650 
CaseMouseMouse PadAudio
Thermaltake Commander MS-I Epic Edition SteelSeries Sensei RAW SteelSeries QCK Altec Lansing BXR1221 
Audio
Sentey Symph GS-4531 
CPUMotherboardGraphicsRAM
Intel Core 2 Duo T9550 ASUS Nvidia GTX 260m 4 GB 
Hard DriveOSMonitorMonitor
Scorpio Blue 750GB Windows 7 1366x768 Epson projector 
Mouse
Logitech M305 
CPUMotherboardRAMHard Drive
I5 650 Intel DH57JG 8gb generic 750gb Scorpio Blue 
Hard DriveCoolingCoolingOS
3tb Caviar Green Cooler Master SickleFlow 120 Stock Intel Cooler Debian 8 
PowerCase
Cheap generic 450w Some cheap junk I had lying around 
  hide details  
Reply
Proteus
(17 items)
 
 
Ordinary Toaster
(10 items)
 
CPUMotherboardGraphicsRAM
i5 2500k P67 Extreme 4 Gen 3 GTX 780 16GB Kingston HyperX Beast 
Hard DriveOptical DriveCoolingCooling
Crucial BX200 Cheap something that writes DVD-RW Hyper 212 Evo Cooler Master SickleFlow 120 
OSMonitorKeyboardPower
Windows 10 Pro 2x Asus VW246H Corsair Strafe MX Brown Corsair HX650 
CaseMouseMouse PadAudio
Thermaltake Commander MS-I Epic Edition SteelSeries Sensei RAW SteelSeries QCK Altec Lansing BXR1221 
Audio
Sentey Symph GS-4531 
CPUMotherboardGraphicsRAM
Intel Core 2 Duo T9550 ASUS Nvidia GTX 260m 4 GB 
Hard DriveOSMonitorMonitor
Scorpio Blue 750GB Windows 7 1366x768 Epson projector 
Mouse
Logitech M305 
CPUMotherboardRAMHard Drive
I5 650 Intel DH57JG 8gb generic 750gb Scorpio Blue 
Hard DriveCoolingCoolingOS
3tb Caviar Green Cooler Master SickleFlow 120 Stock Intel Cooler Debian 8 
PowerCase
Cheap generic 450w Some cheap junk I had lying around 
  hide details  
Reply
post #6 of 12
try putting the variable as char instead of int.
post #7 of 12
Tbh, I haven't done this for 2 years x_x but I still remember a lot, I vaguely remember you could in some cases put ints into chars or something. I need to brush up on my C++ lachen.gif. That was the last thing I looked at and it seemed wrong.
Edited by G33K - 3/14/11 at 7:27pm
Proteus
(17 items)
 
 
Ordinary Toaster
(10 items)
 
CPUMotherboardGraphicsRAM
i5 2500k P67 Extreme 4 Gen 3 GTX 780 16GB Kingston HyperX Beast 
Hard DriveOptical DriveCoolingCooling
Crucial BX200 Cheap something that writes DVD-RW Hyper 212 Evo Cooler Master SickleFlow 120 
OSMonitorKeyboardPower
Windows 10 Pro 2x Asus VW246H Corsair Strafe MX Brown Corsair HX650 
CaseMouseMouse PadAudio
Thermaltake Commander MS-I Epic Edition SteelSeries Sensei RAW SteelSeries QCK Altec Lansing BXR1221 
Audio
Sentey Symph GS-4531 
CPUMotherboardGraphicsRAM
Intel Core 2 Duo T9550 ASUS Nvidia GTX 260m 4 GB 
Hard DriveOSMonitorMonitor
Scorpio Blue 750GB Windows 7 1366x768 Epson projector 
Mouse
Logitech M305 
CPUMotherboardRAMHard Drive
I5 650 Intel DH57JG 8gb generic 750gb Scorpio Blue 
Hard DriveCoolingCoolingOS
3tb Caviar Green Cooler Master SickleFlow 120 Stock Intel Cooler Debian 8 
PowerCase
Cheap generic 450w Some cheap junk I had lying around 
  hide details  
Reply
Proteus
(17 items)
 
 
Ordinary Toaster
(10 items)
 
CPUMotherboardGraphicsRAM
i5 2500k P67 Extreme 4 Gen 3 GTX 780 16GB Kingston HyperX Beast 
Hard DriveOptical DriveCoolingCooling
Crucial BX200 Cheap something that writes DVD-RW Hyper 212 Evo Cooler Master SickleFlow 120 
OSMonitorKeyboardPower
Windows 10 Pro 2x Asus VW246H Corsair Strafe MX Brown Corsair HX650 
CaseMouseMouse PadAudio
Thermaltake Commander MS-I Epic Edition SteelSeries Sensei RAW SteelSeries QCK Altec Lansing BXR1221 
Audio
Sentey Symph GS-4531 
CPUMotherboardGraphicsRAM
Intel Core 2 Duo T9550 ASUS Nvidia GTX 260m 4 GB 
Hard DriveOSMonitorMonitor
Scorpio Blue 750GB Windows 7 1366x768 Epson projector 
Mouse
Logitech M305 
CPUMotherboardRAMHard Drive
I5 650 Intel DH57JG 8gb generic 750gb Scorpio Blue 
Hard DriveCoolingCoolingOS
3tb Caviar Green Cooler Master SickleFlow 120 Stock Intel Cooler Debian 8 
PowerCase
Cheap generic 450w Some cheap junk I had lying around 
  hide details  
Reply
post #8 of 12
Thread Starter 
exactly, I just realized this when I built my old cp files again thanks guys!!!

I feel so dumb over looking that, but I was looking at another persons program but his accepts the inputs as 1 for rock, 2 for paper and etc. and he had int so I totally forgot to switch them over to char.
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
post #9 of 12
Good luck thumb.gif programming is fun, I need to get back into it x_x lachen.gif
Proteus
(17 items)
 
 
Ordinary Toaster
(10 items)
 
CPUMotherboardGraphicsRAM
i5 2500k P67 Extreme 4 Gen 3 GTX 780 16GB Kingston HyperX Beast 
Hard DriveOptical DriveCoolingCooling
Crucial BX200 Cheap something that writes DVD-RW Hyper 212 Evo Cooler Master SickleFlow 120 
OSMonitorKeyboardPower
Windows 10 Pro 2x Asus VW246H Corsair Strafe MX Brown Corsair HX650 
CaseMouseMouse PadAudio
Thermaltake Commander MS-I Epic Edition SteelSeries Sensei RAW SteelSeries QCK Altec Lansing BXR1221 
Audio
Sentey Symph GS-4531 
CPUMotherboardGraphicsRAM
Intel Core 2 Duo T9550 ASUS Nvidia GTX 260m 4 GB 
Hard DriveOSMonitorMonitor
Scorpio Blue 750GB Windows 7 1366x768 Epson projector 
Mouse
Logitech M305 
CPUMotherboardRAMHard Drive
I5 650 Intel DH57JG 8gb generic 750gb Scorpio Blue 
Hard DriveCoolingCoolingOS
3tb Caviar Green Cooler Master SickleFlow 120 Stock Intel Cooler Debian 8 
PowerCase
Cheap generic 450w Some cheap junk I had lying around 
  hide details  
Reply
Proteus
(17 items)
 
 
Ordinary Toaster
(10 items)
 
CPUMotherboardGraphicsRAM
i5 2500k P67 Extreme 4 Gen 3 GTX 780 16GB Kingston HyperX Beast 
Hard DriveOptical DriveCoolingCooling
Crucial BX200 Cheap something that writes DVD-RW Hyper 212 Evo Cooler Master SickleFlow 120 
OSMonitorKeyboardPower
Windows 10 Pro 2x Asus VW246H Corsair Strafe MX Brown Corsair HX650 
CaseMouseMouse PadAudio
Thermaltake Commander MS-I Epic Edition SteelSeries Sensei RAW SteelSeries QCK Altec Lansing BXR1221 
Audio
Sentey Symph GS-4531 
CPUMotherboardGraphicsRAM
Intel Core 2 Duo T9550 ASUS Nvidia GTX 260m 4 GB 
Hard DriveOSMonitorMonitor
Scorpio Blue 750GB Windows 7 1366x768 Epson projector 
Mouse
Logitech M305 
CPUMotherboardRAMHard Drive
I5 650 Intel DH57JG 8gb generic 750gb Scorpio Blue 
Hard DriveCoolingCoolingOS
3tb Caviar Green Cooler Master SickleFlow 120 Stock Intel Cooler Debian 8 
PowerCase
Cheap generic 450w Some cheap junk I had lying around 
  hide details  
Reply
post #10 of 12
A few things:

Try adding parentheses around your ans == 'y' and ans == 'Y' conditions in your while loop.

Try changing your while condition to
Code:
while ((ans == 89) || (ans == 121))
and see if that works. 89 and 121 the decimal values associated with ascii characters 'Y' and 'y'. This may matter sense you are using ints instead of chars.

It might not be the cause of your program failing to loop, but it would be better to store characters in a char data type so I would change that regardless of if it is the cause of the error.

Also, I don't see the need for your switch statements if your going to use if statements.

After trying the above if it still doesn't work try printing the value of ans at the very of the of the do statement... just for debugging purposes.
Edited by Xazen - 3/15/11 at 7:45am
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ Rock-paper-Scissors