![]() |
|
|
#26 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts
|
Try something like this.
Code:
int number;
int main()
{
game();
}
void game ()
{
bool valid_input = false;
while (!valid_input)
{
valid_input = GetInput();
}
... /// continue with game code
}
bool GetInput()
{
cin >> number;
if (cin.Fail())
{
// show error because it's a string
return false;
}
else
{
if (number is < 0 || number > 9)
{
// show error becuase it's out of range
return false;
}
}
return true;
}
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
| The Following User Says Thank You to Kreij For This Useful Post: |
|
|
#27 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts
|
My C++ is a little rusty so please forgive any syntax problems.
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
|
|
#28 |
![]() |
Still i get errors
![]() Don't get the bool programming, but i got your idea and it seems better than try EDIT:LOL, so you made your errors by yourself ahahahaha anyway thanks ![]() I will try ti fix the errors |
|
|
|
|
|
#29 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts
|
What errors and what other problems.
If you keep posting, I'll keep trying to help. ![]() bool (or Boolean) is just a type of variable (true or false). I'm probably mixing C# and C++ code together. lol
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
|
|
#30 |
![]() |
Errors:
a function-definition is not allowed here before '{' token| expected '}' at end of input| no return statement in function returning non-void| ||=== Build finished: 2 errors, 1 warnings ===| I think you are mixing something.... maybe cheese with rice ![]() There are only 2 most awful errors ever.... parenthesis One like this } And one like this { Code:
<o><o> ..L.. ______ Last edited by Aleksander; Sep 25, 2011 at 07:04 PM. |
|
|
|
|
|
#31 | |||
![]() Join Date: Jan 2010
Posts: 384 (0.31/day)
Thanks: 1
Thanked 72 Times in 59 Posts
|
Quote:
Quote:
Quote:
|
|||
|
|
|
|
|
#32 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts
|
LOL ... I told you my C++ was a little rusty. Hard to write code without seeing the all the classes and the way you have everything set up.
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
|
|
#33 |
![]() |
Still, i think there should be something to make it.
Like they do it in games. There must be something we have not figured out yet. Or maybe programers don't really want us to know.... I will let it like that for now, as i don't know what to do What if the user enters 12k321j3.... ahahahaha |
|
|
|
|
|
#34 |
![]() Join Date: Apr 2008
Location: london
Posts: 608 (0.33/day)
Thanks: 170
Thanked 185 Times in 140 Posts
|
I don't know c++ so here's my Java solution. It should be easy to convert to c++.
Code:
import java.util.*;
class test{
public static void main(String args[]){
game();
}
public static void game(){
Scanner input = new Scanner(System.in);
String userInput = input.nextLine(); //gets input from the user as a string.
int inputAsInteger;
if(checkInput(userInput)==true)
inputAsInteger = Integer.parseInt(userInput); //if "checker" from the checkInput() method is true for "userInput" parse it as an integer.
else
game(); //if not then start from the beginning.
//carry on with the game() methods code using the variable "inputAsInteger" as the users input.
}
public static boolean checkInput(String x){
boolean checker = false;
try{
int i = Integer.parseInt(x);
checker = true; //if the string does parse, checker will equal true.
}
catch(Exception e){
checker = false; //if the string does NOT parse, checker will equal false.
}
return checker;
}
}
EDIT2: I forgot to initialize "inputAsInteger". Make it initially equal to 0 and then it should work. |
|
|
|
|
|
#35 |
![]() |
After all.... you are not a beginner in java anymore
![]() Btw you tabs are equal as all I hate java, since the last version. Anyway, why the hell you return checker? What i got from your statement is: Sorry man, i cannot speak english, this is why i am speaking american ![]() If you don't know c++ how comes you programmed it in java? Also, your program, if converted, the problem will persist for sure |
|
|
|
|
|
#36 |
![]() Join Date: Apr 2008
Location: london
Posts: 608 (0.33/day)
Thanks: 170
Thanked 185 Times in 140 Posts
|
You have to return checker so your method knows which boolean to return (even though there's only one).
Also the program works. It keeps initializing game() until a correct input is given. I checked it by adding "System.out.println(inputAsInteger);" at the end of the game() method. As for why in Java. That's because I managed to help you and understand your c++ code without knowing c++. Java and c++ are amazingly similar. EDIT: I'm still a noob. Last edited by razaron; Sep 25, 2011 at 08:06 PM. |
|
|
|
|
|
#37 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts
|
I use spaces for indents, not tabs in the code tags.
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
|
|
#38 | |
![]() Join Date: Jan 2010
Posts: 384 (0.31/day)
Thanks: 1
Thanked 72 Times in 59 Posts
|
Quote:
|
|
|
|
|
|
|
#39 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts
|
@Raz : Your Java code does a recursive call to game(). That is never something you want to do when error checking. Each time the game() method is called the calling method gets put on the stack as the recursive called method has to return to the calling method, so when the last game() call finaly completes it has to pop back through any game() method calls that called it.
Hope tht's not too confusing.
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
|
|
#40 |
![]() |
|
|
|
|
|
|
#41 |
![]() |
Tried making a char or string value to the number instead of int, but still nothing new.
It works worse ![]() I tried another kind of programming of the exceptions like this: Code:
int number = 0;
try
{
cin >> number;
if(number < 1 || number >7)
{
throw 200;
}
}
catch(int e)
{
cout << "\n You typed a wrong number! ERROR: " << e << endl;
}
catch(ios_base::failure&)
{
cout << "\n You didn't type in a number!!" << endl;
}
catch(...)
{
cout << "\n I don't know what happened" << endl;
}
|
|
|
|
|
|
#42 |
![]() Join Date: Jan 2010
Posts: 384 (0.31/day)
Thanks: 1
Thanked 72 Times in 59 Posts
|
save exceptions for... exceptional behavior, not something you can easily predict.
you can use stringstream for converting numerical types to strings and vice versa. |
|
|
|
|
|
#43 |
![]() |
I don't have to convert the int to string.
If i convert it, the user had already input the number |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [WTB][US] Apple iPod Classic 5th Gen | Shadowdust | Buy/Sell/Trade/Giveaway Forum | 0 | May 11, 2011 02:30 PM |
| Asus won't take my 5th RMA. | Corduroy_Jr | Motherboards & Memory | 14 | Dec 6, 2010 06:26 PM |
| Happy 5th Anniversary WCG! | t77snapshot | World Community Grid (WCG) | 7 | Nov 18, 2009 01:47 AM |
| 5th age in AoE3? | spectre440 | Games | 4 | Apr 19, 2006 06:47 AM |