techPowerUp! Forums

Go Back   techPowerUp! Forums > Software > Programming & Webmastering

Reply
 
Thread Tools
Old Sep 25, 2011, 06:35 PM   #26
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts

System Specs

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
Kreij is online now  
Reply With Quote
The Following User Says Thank You to Kreij For This Useful Post:
Old Sep 25, 2011, 06:48 PM   #27
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts

System Specs

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
Kreij is online now  
Reply With Quote
Old Sep 25, 2011, 06:51 PM   #28
Aleksander
2000 Posts
 
Aleksander's Avatar
 
Join Date: Dec 2009
Posts: 3,028 (2.39/day)
Thanks: 648
Thanked 280 Times in 228 Posts

System Specs

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
Aleksander is offline  
Reply With Quote
Old Sep 25, 2011, 06:52 PM   #29
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts

System Specs

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
Kreij is online now  
Reply With Quote
Old Sep 25, 2011, 06:56 PM   #30
Aleksander
2000 Posts
 
Aleksander's Avatar
 
Join Date: Dec 2009
Posts: 3,028 (2.39/day)
Thanks: 648
Thanked 280 Times in 228 Posts

System Specs

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..
______
Help me!

Last edited by Aleksander; Sep 25, 2011 at 07:04 PM.
Aleksander is offline  
Reply With Quote
Old Sep 25, 2011, 07:00 PM   #31
ctrain
200 Posts
 
Join Date: Jan 2010
Posts: 384 (0.31/day)
Thanks: 1
Thanked 72 Times in 59 Posts

Quote:
Originally Posted by Aleksander Dishnica View Post
What should i put in this row when i have numbers from 1 to 10 for example?
Code:
cin.ignore(numeric_limits<streamsize>max(), '\n');
You don't change anything, it was right in the first place.

Quote:
Originally Posted by Aleksander Dishnica View Post
The problem is:
If the user types in a wrong integer like 3980218408....
that throws an error and the game goes back
If the user types in a string or char, that makes the game
restart infinitely
See the throw i have programmed in the first post

Also, your prototype is wrong as i don't mean to exit the game
The player could be anywhere, but if he doesn't press an integer
he looses the game and has to restart!
I already answered it why it's restarting, clear the error and stream or parse everything as a string from the start.

Quote:
Originally Posted by temp02 View Post
You really shouldn't call the game function again when some error occurs, you would be increasing the call stack for no valid/good reason, use Kreij's approach and loop the game function call on main.
Also add "cin.clear();" to the catch, like so:
Code:
catch(int x)
{
  cout << "\n You typed a wrong number! ERROR: " << x << endl;
  cin.clear(); // flush the input error buffer
}
cin.clear() doesn't clear the input stream, it will just try and clear the error state and the offending character is probably still there.
ctrain is offline  
Reply With Quote
Old Sep 25, 2011, 07:08 PM   #32
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts

System Specs

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
Kreij is online now  
Reply With Quote
Old Sep 25, 2011, 07:27 PM   #33
Aleksander
2000 Posts
 
Aleksander's Avatar
 
Join Date: Dec 2009
Posts: 3,028 (2.39/day)
Thanks: 648
Thanked 280 Times in 228 Posts

System Specs

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
Aleksander is offline  
Reply With Quote
Old Sep 25, 2011, 07:36 PM   #34
razaron
500 Posts
 
razaron's Avatar
 
Join Date: Apr 2008
Location: london
Posts: 608 (0.33/day)
Thanks: 170
Thanked 185 Times in 140 Posts

System Specs

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;
	}
}
EDIT: why are my tabs so big and everyone else's so small?
EDIT2: I forgot to initialize "inputAsInteger". Make it initially equal to 0 and then it should work.
__________________
My Anime List
BattleTag (EU): razaron#1882
Steam: Razaron
razaron is offline  
Reply With Quote
Old Sep 25, 2011, 07:47 PM   #35
Aleksander
2000 Posts
 
Aleksander's Avatar
 
Join Date: Dec 2009
Posts: 3,028 (2.39/day)
Thanks: 648
Thanked 280 Times in 228 Posts

System Specs

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
Aleksander is offline  
Reply With Quote
Old Sep 25, 2011, 07:56 PM   #36
razaron
500 Posts
 
razaron's Avatar
 
Join Date: Apr 2008
Location: london
Posts: 608 (0.33/day)
Thanks: 170
Thanked 185 Times in 140 Posts

System Specs

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.
__________________
My Anime List
BattleTag (EU): razaron#1882
Steam: Razaron

Last edited by razaron; Sep 25, 2011 at 08:06 PM.
razaron is offline  
Reply With Quote
Old Sep 25, 2011, 08:56 PM   #37
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts

System Specs

Quote:
Originally Posted by razaron View Post
EDIT: why are my tabs so big and everyone else's so small?
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
Kreij is online now  
Reply With Quote
Old Sep 25, 2011, 09:39 PM   #38
ctrain
200 Posts
 
Join Date: Jan 2010
Posts: 384 (0.31/day)
Thanks: 1
Thanked 72 Times in 59 Posts

Quote:
Originally Posted by Aleksander Dishnica View Post
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
I showed how to handle it...
ctrain is offline  
Reply With Quote
Old Sep 25, 2011, 09:52 PM   #39
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,113 (5.28/day)
Thanks: 591
Thanked 5,492 Times in 2,936 Posts

System Specs

@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
Kreij is online now  
Reply With Quote
Old Sep 26, 2011, 12:26 AM   #40
Aleksander
2000 Posts
 
Aleksander's Avatar
 
Join Date: Dec 2009
Posts: 3,028 (2.39/day)
Thanks: 648
Thanked 280 Times in 228 Posts

System Specs

Quote:
Originally Posted by ctrain View Post
I showed how to handle it...
Lol didn't get much attention :P
Aleksander is offline  
Reply With Quote
Old Sep 26, 2011, 05:07 PM   #41
Aleksander
2000 Posts
 
Aleksander's Avatar
 
Join Date: Dec 2009
Posts: 3,028 (2.39/day)
Thanks: 648
Thanked 280 Times in 228 Posts

System Specs

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;
}
Still the problem wont be fixed
Aleksander is offline  
Reply With Quote
Old Sep 26, 2011, 05:46 PM   #42
ctrain
200 Posts
 
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.
ctrain is offline  
Reply With Quote
Old Sep 26, 2011, 06:08 PM   #43
Aleksander
2000 Posts
 
Aleksander's Avatar
 
Join Date: Dec 2009
Posts: 3,028 (2.39/day)
Thanks: 648
Thanked 280 Times in 228 Posts

System Specs

I don't have to convert the int to string.
If i convert it, the user had already input the number
Aleksander is offline  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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


All times are GMT. The time now is 07:17 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
no new posts