• School project, bug fixing.
    24 replies, posted
Hello, I would need some help on a bug I have on my school project. The game is roulette and it's ofc written in c++. [url]http://pastebin.com/znn4vAwB[/url] The problem is that when I write any other sign than a numeric it goes bat crazy and it writes "Invalid Number!" and then it continues on from main() and starts to execute other functions when it just should loop over and ask the user for a new value. Is it possible to make the user enter a numeric in a easy way? Thank you. Edit: Ignore that it spits out "Invalid Number" even if it where correct. I just want it to loop over if the user enters anything else then a numeric value. Ty
[cpp]if (bet_number > 36 && bet_number < 0)[/cpp] Shouldn't this be if (bet_number > 36 || bet_number < 0)?
[QUOTE=BlkDucky;33597207][cpp]if (bet_number > 36 && bet_number < 0)[/cpp] Shouldn't this be if (bet_number > 36 || bet_number < 0)?[/QUOTE] Thank you, that really helped in a way. Now the only problem is that it loops "What number would you like...." over and over again.
The way you have it written now, everything is invalid. If it's not in the range 0-36, it's invalid. If it is, it's still invalid. You literally don't have a valid path through that loop.
[QUOTE=ROBO_DONUT;33597374]The way you have it written now, everything is invalid. If it's not in the range 0-36, it's invalid. If it is, it's still invalid. You literally don't have a valid path through that loop.[/QUOTE] Updated the code [url]http://pastebin.com/znn4vAwB[/url]
Are they teaching you the 'break' keyword? Because I'd do something like [cpp] for (;;) { std::cout << "\nWhat number do you want to bet on?(0-36)" << std::endl; std::cin>>bet_number; if (bet_number >= 0 && bet_number <= 36) break; std::cout << "Invalid Number!" << std::endl; } [/cpp] [editline]6th December 2011[/editline] [QUOTE=landizz;33597406]Updated the code [url]http://pastebin.com/znn4vAwB[/url][/QUOTE] You still don't have a valid way to escape the loop. Suppose the user enters an invalid number first. Your code sets bet_number_invalid to true. In order for the loop to terminate, bet_number_invalid needs to be false. Where does your code set bet_number_invalid to false inside the loop? Nowhere, so you can't escape the loop.
you need to set bet_number_invalid to true in the else for it to work
[QUOTE=ROBO_DONUT;33597430]Are they teaching you the 'break' keyword? Because I'd do something like [cpp] for (;;) { std::cout << "\nWhat number do you want to bet on?(0-36)" << std::endl; std::cin>>bet_number; if (bet_number >= 0 && bet_number <= 36) break; std::cout << "Invalid Number!" << std::endl; } [/cpp] [editline]6th December 2011[/editline] You still don't have a valid way to escape the loop. Suppose the user enters an invalid number first. Your code sets bet_number_invalid to true. In order for the loop to terminate, bet_number_invalid needs to be false. Where does your code set bet_number_invalid to false inside the loop? Nowhere, so you can't escape the loop.[/QUOTE] No, my code sets the bet_number_invalid to false first and if the user enters an invalid sign bet_number_invalid becomes true. Which continues the loop. And yes I am familiar with "break" and I have now added it to the code. [QUOTE=Stary2001;33597466]you need to set bet_number_invalid to true in the else for it to work[/QUOTE] Tried that too and it looped the same question over and over again, without the chance to input a numeric.
[QUOTE=landizz;33597502]No, my code sets the bet_number_invalid to false first and if the user enters an invalid sign bet_number_invalid becomes true.[/QUOTE] And then it's stuck true for the rest of eternity... [QUOTE=landizz;33597502]And yes I am familiar with "break" and I have now added it to the code.[/QUOTE] Don't just add things in arbitrarily. Your code is going to get worse and worse. Choose one method to solve the problem and stick with it the whole way through. If you want to use a do-while loop with an exit condition variable, then set the exit condition to false to exit. If you want to use an 'infinite' loop with an unconditional jump, that works too. Just don't use a do-while loop with an exit condition value and then arbitrarily start throwing in unconditional jumps. Nobody is going to be able to make sense of it.
[QUOTE=ROBO_DONUT;33597694]And then it's stuck true for the rest of eternity... Don't just add things in arbitrarily. Your code is going to get worse and worse. Choose one method to solve the problem and stick with it the whole way through. If you want to use a do-while loop with an exit condition variable, then set the exit condition to false to exit. If you want to use an 'infinite' loop with an unconditional jump, that works too. Just don't use a do-while loop with an exit condition value and then arbitrarily start throwing in unconditional jumps. Nobody is going to be able to make sense of it.[/QUOTE] Well so far have none methods work. The code now looks like this: [url]http://pastebin.com/zq2CWnbS[/url] And when I enter "d" it loops "What number would you like..." over and over again.
[QUOTE=landizz;33597778]And when I enter "d" it loops "What number would you like..." over and over again.[/QUOTE] That would be Windows' standard library functions for you. They're all broken.
[QUOTE=ROBO_DONUT;33597804]That would be Windows' standard library functions for you. They're all broken.[/QUOTE] What do you mean? The code works now except for when I am entering anything else than a numeric value. Is it really possible to demand this for Programming A course? Aiming for the best grade.(A++ in USA?)
[QUOTE=landizz;33597867]What do you mean?[/QUOTE] This is a common problem that you really can't do anything about, short of switching to scanf() or something. Scanf(), though, is a C thing, and many C++ zealots will go into a fit upon seeing it. Your code would work fine on any other platform. Actually, you might want to try your code in Code::Blocks. Maybe GCC will get it right.
[QUOTE=ROBO_DONUT;33597895]This is a common problem that you really can't do anything about, short of switching to scanf() or something. Scanf(), though, is a C thing, and many C++ zealots will go into a fit upon seeing it. Your code would work fine on any other platform. Actually, you might want to try your code in Code::Blocks. Maybe GCC will get it right.[/QUOTE] So Visual c++ isn't a good program to code in? I thought it was like the standard of all c++ programming.
[QUOTE=ROBO_DONUT;33597804]That would be Windows' standard library functions for you. They're all broken.[/QUOTE] I'm not quite sure what you mean. std::cin works just fine, it just has a small quirk. If your input is invalid, the stream gets an error flag set and doesn't remove the invalid input from the stream. So you'll have to do something like this: [cpp]int in; do { std::cout << "Enter a number:" << std::endl; if (!(std::cin >> in)) { // Invalid input, clear the stream. std::cin.clear(); // this removes the error flag std::cin.ignore(10000, "\n"); // ignore up to 10k characters or up to a newline from the stream // this effectively resets the stream, so you can use std::cin again } } while(in != --1);[/cpp]
[QUOTE=raBBish;33597999]I'm not quite sure what you mean. std::cin works just fine, it just has a small quirk. If your input is invalid, the stream gets an error flag set and doesn't remove the invalid input from the stream. So you'll have to do something like this: [cpp]int in; do { std::cout << "Enter a number:" << std::endl; if (!(std::cin >> in)) { // Invalid input, clear the stream. std::cin.clear(); // this removes the error flag std::cin.ignore(10000, "\n"); // ignore up to 10k characters or up to a newline from the stream // this effectively resets the stream, so you can use std::cin again } } while(in != --1);[/cpp][/QUOTE] Nice! I took the "IF" part and changed "\n" to '\n' and it worked! Just hope the teacher doesn't care that I took it of the internet. He can't possibly think I would be able to do that by myself:P
[QUOTE=landizz;33598135]Nice! I took the "IF" part and changed "\n" to '\n' and it worked! Just hope the teacher doesn't care that I took it of the internet. He can't possibly think I would be able to do that by myself:P[/QUOTE] I took it from [url=http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.3]here[/url] (found by google "std::cin invalid input") and simplified it a bit. I doubt you'll need to include the numeric limits part.
[QUOTE=landizz;33598135]Nice! I took the "IF" part and changed "\n" to '\n' and it worked! Just hope the teacher doesn't care that I took it of the internet. He can't possibly think I would be able to do that by myself:P[/QUOTE] You SHOULD know the difference between "\n" and '\n' (the difference is "" means a sting and '' is a char). I would hope your teacher discussed primitive types and strings before getting into something like this...
[QUOTE=Jetsurf;33599822]You SHOULD know the difference between "\n" and '\n' (the difference is "" means a sting and '' is a char). I would hope your teacher discussed primitive types and strings before getting into something like this...[/QUOTE] Yes I know the difference. The teacher is great. I knew a little c++ when I started this course so up to now I have been floating:) He gave me a challenge thou that I can't really get to work. Sadly I can't get any help from anyone.. The teacher has been learning/knowing c++ for maybe 20 years or so and he also teaches math A-E. So he's good at what he does:)
[QUOTE=landizz;33599905]Yes I know the difference. The teacher is great. I knew a little c++ when I started this course so up to now I have been floating:) He gave me a challenge thou that I can't really get to work. Sadly I can't get any help from anyone.. The teacher has been learning/knowing c++ for maybe 20 years or so and he also teaches math A-E. So he's good at what he does:)[/QUOTE] Misunderstood what you didn't understand there, my bad.
Landizz, I will always help you <3
[QUOTE=likesoursugar;33612304]Landizz, I will always help you <3[/QUOTE] And I love you for that:)
:3 Let's hold hands and pray. Dear satan...
[QUOTE=a203xi;33700022]:3 Let's hold hands and pray. Dear satan...[/QUOTE] Hey men, don't bring that kind of stuff up. This is a very Christian subforum and we don't appreciate Satan in here.
[url]http://lists.freebsd.org/pipermail/freebsd-chat/2011-November/006642.html[/url] and [url]http://monster-island.org/tinashumor/humor/daemon.html[/url] (this link can also be found in the some reply to that mail)
Sorry, you need to Log In to post a reply to this thread.