11 Replies - 503 Views - Last Post: 19 February 2011 - 01:50 PM Rate Topic: -----

#1 Liinx  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 18-February 11

C++ isn't running the code how I have it.

Posted 18 February 2011 - 09:40 AM

Ok, so I posted earlier about rand() and switches, well I kind of figured out what I'm doing. But I'm running into a problem. I can't post my code because its part of a school assignment and I want to learn to do this not just have someone fix it.

My problem is that for the text based RPS game that I'm making won't display if they won or not. I have the random code.

#include <ctime>

int compSelection

compSelection = rand() % (3 + 1);



I also tried
compSelection = 1 + rand() % (3 + 1);



I believe either of those would for for the way I'm doing it. But I'm not sure if thats the problem, but then after that I had was looking through my code and realized that I'm not sure which way would work better or which wouldn't work at all. For determining who wins. I had both of these kinda codes.

Case(compSelection)
Case '1':
If
Else If
Else If
Else
break;
Case '2':
If
Else If
...
and so on.


Then I tried
If(userSelection == "R")
Case(compSelection)



doing it that way I would have needed 3 or 4 case statements, so I thought that the first way would have worked better.

Anyways, both ways I did it, I came up with the same problem.
I'd have the user enter their name and then once they did that the Letter choice(I did a capital R so my code wouldn't have to capitalize it until I got started on it and working properly.

Once they did that, the program would run the default of the case, so I took that out and it wouldn't display anything. just the "Press any button to continue" line at the end of the command problem.

I don't know I've re-read my code like 100 times and I can't find anything that would cause that. I'm building it in Visual Studio 2003. I build or rebuild then run without debug.(only way the properly works at my school)

So can anyone help me out a little bit and help me get this fixed. I don't understand what it could be. I don't have any build errors and it builds perfectly fine.

P.S., sorry I couldn't provide the code so you can see exactly what my problem is. I know my teacher browsers this from time to time and I don't want a fix someone else does, I want one that I do so I can figure out what I did wrong and learn from it.

Thanks

--Linx

Is This A Good Question/Topic? 0
  • +

Replies To: C++ isn't running the code how I have it.

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,224
  • Joined: 23-August 08

Re: C++ isn't running the code how I have it.

Posted 18 February 2011 - 09:47 AM

I can guarantee you one thing: C++ is running the code EXACTLY as you have it. What's happening is your code is not written to do what you WANT it to do.

And without seeing code, it's really hard to diagnose your problem. Maybe, if you're so worried about your teacher finding you got help here, you should ASK THE TEACHER for help?
Was This Post Helpful? 1
  • +
  • -

#3 Liinx  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 18-February 11

Re: C++ isn't running the code how I have it.

Posted 18 February 2011 - 09:50 AM

She's not a C++ person and has learned it by sitting down and looking through it, I asked her and she sat down and messed with my code for about 20 minutes, she couldn't get it to work properly either. But thanks for your help anyways.

I'm not worried about her finding out I got help here. I don't want her to get on here and see that I've posted my code and someone has fixed it for me. I don't learn that way, I don't see what was wrong with it either. Both of which I want to do.

This post has been edited by Liinx: 18 February 2011 - 09:53 AM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,753
  • Joined: 25-December 09

Re: C++ isn't running the code how I have it.

Posted 18 February 2011 - 10:27 AM

#include <ctime>

int compSelection

compSelection = rand() % (3 + 1);

Case(compSelection) // This should be switch
Case '1':           // This should be case
   // do something
break;
Case '2':           // This should be case
   // do something else
and so on.




In the above code snippet you have compSelection declared as an int, but in the switch statement you are comparing to a character case '1':. You should be comparing to an int case 1:. Also you have Case(compSelection) it should be switch(compSelection). Also remember that C/C++ is case sensitive Case is not equal to case or CASE.


If you don't want to post your actual code that would be ok. But you should be able to change the code enough, by changing names removing sections etc. to illustrate your problem. Then post that complete program along with any questions and error/warning messages you receive when you compile that snippet.



Jim

This post has been edited by jimblumberg: 18 February 2011 - 10:28 AM

Was This Post Helpful? 0
  • +
  • -

#5 DivideByZero  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 238
  • View blog
  • Posts: 551
  • Joined: 02-December 10

Re: C++ isn't running the code how I have it.

Posted 18 February 2011 - 10:45 AM

Line 3 needs a semicolon after the declaration.
Was This Post Helpful? 0
  • +
  • -

#6 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: C++ isn't running the code how I have it.

Posted 19 February 2011 - 05:57 AM

View PostLiinx, on 18 February 2011 - 05:50 PM, said:

I'm not worried about her finding out I got help here. I don't want her to get on here and see that I've posted my code and someone has fixed it for me. I don't learn that way, I don't see what was wrong with it either. Both of which I want to do.

That's fine - this is a learning community, so you'll be relieved to hear that noone is going to be handing out free complete homework solutions.

A better alternative is to write a small, complete compilable example program which displays the same problem you're having in your real code; This is better for us too, than having to wade through possibly 100s of lines of your real code to find the one part which isn't behaving as you intend. Sometimes by doing that you might even solve the problem yourself before it even reaches the forum.
Was This Post Helpful? 0
  • +
  • -

#7 Liinx  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 18-February 11

Re: C++ isn't running the code how I have it.

Posted 19 February 2011 - 01:17 PM

Sorry, that is all correct on the program, i just did a quick type up of everything. I don't get any errors or anything, it just will not display the results of the RPS game.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,753
  • Joined: 25-December 09

Re: C++ isn't running the code how I have it.

Posted 19 February 2011 - 01:20 PM

So if you won't post any code showing the problem, how do you expect us to be able to help?

Jim
Was This Post Helpful? 0
  • +
  • -

#9 Liinx  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 18-February 11

Re: C++ isn't running the code how I have it.

Posted 19 February 2011 - 01:35 PM

//RPS - Built and Created by Ryan
//2/19/2011
//Attempt #3

#include <iostream>
#include <string>
#include <ctime>
#include <algorithm>

using namespace std;

int main()
{
	//variables
	int compSelection;
	string userSelection;
	string name;

	//input data
	cout << " -- The King's RPS Game -- " << endl;
	cout << "Please enter your name: " << endl;
	getline(cin, name);
	cout << "Ok " << name << ", lets get started then" << endl;
	cout << "Please select the letter that corresponds with the choice that you want." << endl;
	cout << "[R] - Rock" << endl;
	cout << "[P] - Paper" << endl;
	cout << "[S] - Scissors" << endl;
	cin >> userSelection;

	//random code and capitalize
	compSelection = rand() % 3 + 1;

	switch(compSelection)
{
	{
	case '1': 
		//Rock	
		if(userSelection == "R")
			cout << "You have tied";
		else if(userSelection == "P")
			cout << "You have beat the computer";
		else if(userSelection == "S")
			cout << "The Computer has beaten you";
		else
			cout << "You have entered an invalid letter";
		break;
	}
	case '2': 
		{
		//Rock	
		if(userSelection == "R")
			cout << "You have tied";
		else if(userSelection == "P")
			cout << "You have beat the computer";
		else if(userSelection == "S")
			cout << "The Computer has beaten you";
		else
			cout << "You have entered an invalid letter";
		break;
		}
	case '3': 
		{
		//Rock	
		if(userSelection == "R")
			cout << "You have tied";
		else if(userSelection == "P")
			cout << "You have beat the computer";
		else if(userSelection == "S")
			cout << "The Computer has beaten you";
		else
			cout << "You have entered an invalid letter";
		break;
		}
	}
		return 0;
}

	


Thats the code, I've recoded it about 3 times looking for the problem, each time it won't output who wins.

I know that all the cases display the samething, I'll fix once I get it running properly. and not sure if I need the brackets in between all the case or not.

This post has been edited by Liinx: 19 February 2011 - 01:38 PM

Was This Post Helpful? 0
  • +
  • -

#10 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: C++ isn't running the code how I have it.

Posted 19 February 2011 - 01:44 PM

Why have made userSelection a string. It can take only a character as input. Why no make it char.

And remove the '' from case '1' and so on...

It should be just case 1 : as compSelection is an int.

This post has been edited by chinchang: 19 February 2011 - 01:49 PM

Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,753
  • Joined: 25-December 09

Re: C++ isn't running the code how I have it.

Posted 19 February 2011 - 01:47 PM

	int compSelection;
	switch(compSelection)
{
	{
	case '1':


In the above snippet you have defined compSelection as an integer. So in your switch you must compare to an integer. This case '1': is comparing to the character literal '1' this character has a numerical value of 49. So what you are actually saying here is case 49:. What you want to do is remove the '' and just use case 1: for this switch statement.


Jim
Was This Post Helpful? 0
  • +
  • -

#12 Liinx  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 18-February 11

Re: C++ isn't running the code how I have it.

Posted 19 February 2011 - 01:50 PM

Thank you very much it worked. Sorry I wasn't posting my code at first.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1