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

C++ Rock-paper-Scissors - Page 2

post #11 of 12
Quote:
Originally Posted by G33K;12734426 
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.

This... you might also try and re-bracketing your if else statments. Do you have a debugger on hand?
Edited by badatgames18 - 3/15/11 at 8:17am
Galbus
(21 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K 8M Skylake Quad-Core 4.0 G... ASUS ROG MAXIMUS VIII EXTREME LGA 1151 Intel Z1... 3x MSI GeForce GTX 1080 Founders Edition  2x [Trident Z] F4-3600C16D-16GTZKW 16-16-16-36... 
Hard DriveHard DriveOptical DriveOptical Drive
Plextor M8Pe AIC 1TB PCI-Express 3.0 x4 MLC Int... Plextor M8Pe AIC 1TB PCI-Express 3.0 x4 MLC Int... LG BH16NS40 Blu-ray Card Reader: Rosewill RDCR-11003 - 3.5" 74-in-1... 
CoolingOSMonitorKeyboard
Custom Ultra cool passive heatsink Microsoft Windows 10 Pro 64-bit ViewSonic XG2703-GS Black 27" 4ms (GTG) IPS G-S... Das Keyboard 4 Professional Mechanical Keyboard 
PowerCaseMouseMouse Pad
CORSAIR AXi Series AX1500i Digital 1500W 80 PLU... Corsair Obsidian 750D Black Aluminum / Steel AT... Logitech G900 Chaos Spectrum Logitech 
AudioOtherOtherOther
onboard Canon i-SENSYS LBP7680Cx (5089B014) Duplex 9600... Canon imageFORMULA DR-F120 (9017B002) 600 dpi U... Brother Personal Plain Paper Fax Machine, FAX-575 
  hide details  
Reply
Galbus
(21 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K 8M Skylake Quad-Core 4.0 G... ASUS ROG MAXIMUS VIII EXTREME LGA 1151 Intel Z1... 3x MSI GeForce GTX 1080 Founders Edition  2x [Trident Z] F4-3600C16D-16GTZKW 16-16-16-36... 
Hard DriveHard DriveOptical DriveOptical Drive
Plextor M8Pe AIC 1TB PCI-Express 3.0 x4 MLC Int... Plextor M8Pe AIC 1TB PCI-Express 3.0 x4 MLC Int... LG BH16NS40 Blu-ray Card Reader: Rosewill RDCR-11003 - 3.5" 74-in-1... 
CoolingOSMonitorKeyboard
Custom Ultra cool passive heatsink Microsoft Windows 10 Pro 64-bit ViewSonic XG2703-GS Black 27" 4ms (GTG) IPS G-S... Das Keyboard 4 Professional Mechanical Keyboard 
PowerCaseMouseMouse Pad
CORSAIR AXi Series AX1500i Digital 1500W 80 PLU... Corsair Obsidian 750D Black Aluminum / Steel AT... Logitech G900 Chaos Spectrum Logitech 
AudioOtherOtherOther
onboard Canon i-SENSYS LBP7680Cx (5089B014) Duplex 9600... Canon imageFORMULA DR-F120 (9017B002) 600 dpi U... Brother Personal Plain Paper Fax Machine, FAX-575 
  hide details  
Reply
post #12 of 12
Code:
// random.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int main ()
{
bool playing = true;
// I like to use while loops, do work too, they just test at the end so they have to go through at least one
// time, each have their benifits.
while (playing) {
char ch1, ch2, ans = NULL;

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

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


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;
}


// while (ans == 'y' || ans == 'Y'); // Does nothing at this point

// Just use Ctrl + F5 to stop at the end instead of system pause
return 0;
}
=]

To quickly format in VS, hold control press and hold a then press and hold k and then press and hold f

so CTRL + A + K +F

It will never exit the loop because I haven't taken the last command into account. you can just set playing to false to break out.

Edit -- Oh you got it already.
Edited by Twinnuke - 3/15/11 at 8:37am
Unto The Void
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 6800k Gigabyte G1 X99-Phoenix SLI Gigabyte GTX 1080 G1 32GB RipJaw 4 DDR4-3000 
Hard DriveHard DriveHard DriveHard Drive
Samsung 850 500GB Crucial M4 2 x Spinpoint F1 Samsung 850 1TB 
CoolingOSMonitorMonitor
Noctua NH-D15S Windows 10 LG Ultrawide 29" Asus 24" 
KeyboardPowerCaseMouse
Logitech 810 EVGA P2 650 Watt In Win 303 Logitech G502 
  hide details  
Reply
Unto The Void
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 6800k Gigabyte G1 X99-Phoenix SLI Gigabyte GTX 1080 G1 32GB RipJaw 4 DDR4-3000 
Hard DriveHard DriveHard DriveHard Drive
Samsung 850 500GB Crucial M4 2 x Spinpoint F1 Samsung 850 1TB 
CoolingOSMonitorMonitor
Noctua NH-D15S Windows 10 LG Ultrawide 29" Asus 24" 
KeyboardPowerCaseMouse
Logitech 810 EVGA P2 650 Watt In Win 303 Logitech G502 
  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