trouble with loops, functions, and organization´ - c++

and I would like to preface this by saying this is NOT a current homework assignment; but it is an assignment from 3 years ago before I dropped out of school. I am self teaching and am revisiting an old assignment. I am NOT asking for the entire program, I'm simply looking for help building the skeleton for the initial start of the game.
MORE INFO:
Player 1 will enter word(of any length / i have been using "Testing") for Player 2 to guess. Player 2 will have 5 letter guesses, and 5 word guesses. If Player 2 enters "Testing" it should be able to ignore the case between upper/lower (WITHOUT using toupper / tolower)
IF: Player 2 enters more than 1 letter for a guess: "aa" make them guess again until they only guess 1 letter "a".
The problems I'm facing is: I don't know where to place everything, I feel I'm mixing up or messing up the functions, and everytime I try to organize it, it only gets worse. I've restarted it several times, I'm just having trouble getting it all laid out.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
string word, wordguess, lower, dashes;
string letterguess;
int i = 0;
bool GameOver = false, Validletterguess = true, Validwordguess = true;
cout << "Player 1, enter a word for Player 2 to guess: " << endl;
getline(cin, word);
cout << endl;
cout << "Player 2, you have 5 letter guesses, and 5 word guesses." << endl;
cout << "Guess your first letter: " << endl;
while (GameOver == false) // Start of Game. Setup for Round 1 letter guess and word guess.
{
while (letterguess.length() != 1) // 1 letter only. loop until they enter 1 letter
{
cout << endl << "Type a single letter and press <enter>: ";
cin >> letterguess; // enter letter guess
for (int i = 0; i < letterguess.length(); i++) //ignore case of letter guess
{
if (letterguess.at(i) >= 'A' && letterguess.at(i) <= 'Z')
{
lower += letterguess.at(i) + 32;
}
else
{
lower += letterguess.at(i);
}
}
if (letterguess.at(i) == word.at(i) && Validletterguess == true) //if Player2 guesses a correct letter, replace the dash with letter and display location: ex. If "T" then "You guessed the 1st and 4th letter"
{
cout << "You guessed the first letter right!" << endl; // figure out how to display dashes?
dashes.at(i) = letterguess.at(i);
cout << "Enter your first word guess: " << endl;
cin >> wordguess;
}
else
cout << "Wrong letter! Enter your first word guess: " << endl;
cin >> wordguess;
if (wordguess == word & Validwordguess = true)
{
cout << "You guessed the word correctly in 1 try! " << endl;
Gameover = true;
}
}
}
}

There are several things in C++ that can assist you. It's good to see that you're already using std::string and std::getline to deal with the user input. The problem seems to be that you've gotten tangled up in organizing the game logic so that it flows, and setting up structures that can help you.
I did actually go ahead and write a game just for kicks. The hope is that I can provide some of that and describe it so you can digest in chunks, and you can see how one can build a program up a bit at a time.
So let's start by making a stub for the function that will actually run the game. You can call this from main. It simplifies the actual running of the game by separating it from the other setup and shutdown stuff. It also means you can run several games in a row later, without having to modify the game loop.
enum GameResult {
None,
Win,
Loss,
};
GameResult PlayHangman(const std::string& target, int wrongLetterLimit, int wrongWordLimit)
{
return None;
}
And to illustrate that point, here is the full main that I ended up writing to invoke this game. Even though it's a one-off, you can see that it can be useful. In this case, I chose to read the game settings from the command line:
void ExitSyntaxMessage(int code = -1)
{
std::cerr << "Syntax: hangman <word> [guesses [wordGuesses]]\n";
exit(code);
}
int main(int argc, char** argv)
{
// Get game settings
std::string target;
int letterGuesses = 5;
int wordGuesses = 5;
try {
if (argc < 2) throw std::runtime_error("Not enough arguments");
target = argv[1];
if (argc > 2) letterGuesses = std::stoi(argv[2]);
if (argc > 3) wordGuesses = std::stoi(argv[3]);
}
catch(...)
{
ExitSyntaxMessage();
}
// Play game
GameResult result = PlayHangman(target, letterGuesses, wordGuesses);
// Deliver result and exit
switch(result)
{
case Win:
std::cout << "Congratulations!\n";
return 0;
case Loss:
std::cout << "Better luck next time!\n";
return 1;
default:
std::cout << "Game stopped.\n";
return -2;
}
}
So, now there's a simple framework for your game to run in. There's not much code, but it's something you can immediately start testing before moving on to fleshing out the game itself.
At this point, I should mention some headers that this program will be needing. Some will have been required already. Others are required for stuff we're about to do.
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include <set>
On to the game... A helper function to turn a string into lowercase is always handy. We'll definitely make use of that. Note that this uses a lambda function. If you don't have a modern C++ compiler (with C++11 support) you can just use an ordinary function pointer instead.
std::string Lowercase(const std::string& s)
{
std::string lc(s);
std::transform(lc.begin(), lc.end(), lc.begin(),
[](char c)->char{ return std::tolower(c); });
return lc;
}
Now it's time to expand on the PlayHangman stub. It's still gonna be a stub, but we can set up a few things that we'll be needing and get those tested before proceeding.
GameResult PlayHangman(const std::string& target, int wrongLetterLimit, int wrongWordLimit)
{
GameResult result = None;
// Create lowercase target and add its characters to set of remaining letters
std::string lcTarget = Lowercase(target);
std::set<char> lettersRemaining(lcTarget.begin(), lcTarget.end());
std::set<std::string> guesses;
// Set up game parameters
int letterGuessesRemaining = wrongLetterLimit;
int wordGuessesRemaining = wrongWordLimit;
// Sanity-test to ensure game is winnable
if (wordGuessesRemaining == 0 && letterGuessesRemaining < lettersRemaining.size())
{
std::cout << "Game is not winnable...\n";
return None;
}
// Game loop until stream error or game finishes
bool done = false;
while (!done)
{
done = true; // The loop is a stub for now
}
//// ^^^ for now, just use this bit to test the game setup stuff.
//// Make sure that your lowercase bits are working and the set of
//// remaining letters works. You can add some output code to debug
//// their values and run tests from the command line to verify.
return result;
}
That is going to be the primary structure of a single game. So let's talk about it. Notice again how I'm still not going into detail. At this point, I've already thought about how I should logically be running the game.
Now, I should say that in reality, most people don't write code in a linear way like this from the outside in. It's more of an organic process, but I do take care to separate stuff out into logical bits, and reshuffle/organize stuff as I go. I also try not to do too much at once.
You'll see by the way I've presented this, that I'm encouraging you to develop a solid platform in which to write your game logic. By the time you're writing that logic, you should be able to trust that everything else already works because you tested it.
Some things happening up there are:
The target string is copied into a lowercase version of itself. This will be used to test the word-guesses. There are other ways to test strings ignoring case, but this is just a simple way.
Because we've built that string, we can also use it to construct a std::set containing exactly one of each unique character in that string. That's a one-liner, constructing the set from the string's iterators. Very neat and tidy!
We also have a set of strings called guesses -- this will track all the guesses (correct/incorrect inclusive) so that you don't get penalized for accidentally repeating something you already guessed.
There's a sanity check, which is a duplicate of what will eventually be the end-game test inside the loop. To be honest, that was one of the last things I added, but I've put it here because it's part of the pre-game setup, and apart from the stubbed loop, this is the entire "game" sequence.
Checkpoint : Game skeleton complete
At this point, you might have seen enough to go off and complete the game. There are some important concepts introduced up there. In particular, the idea of storing the remaining letters as a std::set might be just the kind of trick that makes everything click into place.
Reading from here on will complete the program. It's up to you whether you want to do that, or stop reading and have a crack at it yourself first.
Let's start fleshing out some of the game loop. First, you probably wanna deal with showing the current game state and requesting input. That happens in two steps. The first part builds a string by hiding characters that are not yet guessed and then outputs it. The second part is an input-validating loop that discards empty lines, ignores duplicate guesses and handles end-of-stream.
Note that the input is converted to lowercase. This just simplifies things. Especially when checking for duplicate guesses.
while (!done)
{
// Create prompt from original string with a dash for each hidden character
std::string prompt(target);
for(char& c : prompt)
{
if (lettersRemaining.count(std::tolower(c)) != 0) c = '-';
}
std::cout << prompt << "\n";
// Get input
std::string input;
for (bool validInput = false; !validInput && !done; )
{
std::cout << "> " << std::flush;
if (!std::getline(std::cin, input))
{
done = true;
}
else if (!input.empty())
{
input = Lowercase(input);
validInput = guesses.insert(input).second;
if (!validInput)
{
std::cout << "You already guessed that!\n";
}
}
}
if (done)
continue;
// TODO: Process guess, update game state, and check end-game conditions
}
Once again, we have expanded on the implementation and now have something to test. So make sure it all compiles and works the way you want it to. Obviously the game will run forever right now, but that's fine -- you can just terminate the process.
When you're happy, move on to the actual logic. This is where we start putting together everything that has already been set up.
Thanks to our input loop, we now know that the input is now a new guess comprising either 1 letter or a word. So I start by branching for either the letter guess or the word guess. You should start to see a pattern here, right? Once again, I write an empty section of code to do something, and then start actually filling it in...
// Check the guessed letter or word
bool correctGuess = false;
if (input.size() == 1)
{
if (letterGuessesRemaining == 0)
{
std::cout << "No more letter guesses remain.\n";
}
else
{
// Test the guessed letter
}
}
else
{
if (wordGuessesRemaining == 0)
{
std::cout << "No more word guesses remain.\n";
}
else
{
// Test the guessed word
}
}
So, the letter test... Recall we already built the lettersRemaining set and tested it. And those are the only ones obscured by dashes in the prompt. So it then becomes trivial to determine whether they guessed one. If it's in the set, they guessed correctly and you remove it from the set. Otherwise, they burn up one of their guesses.
Because the input is already lowercase, we can use the letter verbatim to search within the values stored in the set (which are also lowercase).
// Test the guessed letter
char letter = input[0];
if (lettersRemaining.count(letter) != 0)
{
correctGuess = true;
lettersRemaining.erase(letter);
}
else
{
std::cout << "Nope!\n";
--letterGuessesRemaining;
}
The word test is even easier. Recall that we stored a lowercase version of the target word already, and the input was also converted to lowercase. So we just compare. You see how all this lowercase business has actually made life less complicated?
// Test the guessed word
if (input == lcTarget)
{
correctGuess = true;
lettersRemaining.clear(); //<-- we can use this to test for a win
}
else
{
std::cout << "Nope!\n";
--wordGuessesRemaining;
}
We are quite literally almost done! The only thing left to do is check whether the game should stop due to being won or lost. That's the last part of the game loop.
Because the code handling a correct word guess is also polite and clears the lettersRemaining set, we can use that as a test for a winning condition regardless of whether a letter or word was guessed.
You'll also see that bit of logic again for the game losing condition. Recall that from before the main loop where we checked if it was even possible to win.
// If guessed incorrectly, show remaining attempts
if (!correctGuess)
{
std::cout << "\nAttempts remaining: "
<< letterGuessesRemaining << " letters, "
<< wordGuessesRemaining << " words.\n";
}
// Check if game is complete
if (lettersRemaining.empty())
{
std::cout << target << "\n";
result = Win;
done = true;
}
else if (wordGuessesRemaining == 0 && letterGuessesRemaining < lettersRemaining.size())
{
std::cout << target << "\n";
result = Loss;
done = true;
}
I hope this has been helpful, that you've been able to follow along, and that you understand the breakdown and explanation. This is generally how I approach programming. I like to build up pieces of code that I can rely on, instead of getting lost in some details and overlooking more fundamental things.
There may be some techniques, language features or parts of the standard library used here that you have not encountered before. That's fine -- you can use that to learn, experiment and research online. Keep https://cppreference.com bookmarked in your browser.
If nothing else, I hope that this gives you some insight in breaking down tasks into small bits that you care about now, and other stuff that you can worry about later. Building up a program iteratively this way enables you to test code regularly and increases your chances of finding silly mistakes that could hamstring you later. It is so common to see beginners just write a whole program in one hit, run it, then freak out because it doesn't "work".

Related

Console has input prompt then closes without output on a very simple program

I have a very simple program that won't give any console output.
I've tried getting input at the end using cin.get() and holding with system("pause"). I've also tried getting input at the start of the program then outputting at the end.
#include <iostream>
using namespace std;
int main(){
int bulb, bulbOpen=0, multiple;
for ( bulb=1; bulb<101 ; bulb=bulb+1 ){
for ( multiple=1; 100; multiple++){
if (bulb/multiple==0){
bulb = bulb * (-1);
}
}
if ( bulb<<0 ){
bulbOpen = bulbOpen + 1;
}
}
cout << "The remaining open light bulbs are " << bulbOpen << "." << endl;
return 0;
}
I'm a beginner programmer so any help, recommendations and explanations are very welcome.
EDIT:
Thanks to Rapha for the fixes and the advice, here's the updated code:
#include <iostream>
int main(){
int bulb, bulbCopy, bulbOpen=0, multiple;
for ( bulb=1; bulb<101 ; bulb++ ){
bulbCopy = bulb;
for ( multiple=1; multiple<101; multiple++){
if (bulbCopy%multiple==0){
bulbCopy = bulbCopy * (-1);
}
}
if ( bulbCopy<0 ){
bulbOpen = bulbOpen + 1;
}
}
std::cout << "The remaining open light bulbs are " << bulbOpen << "." << std::endl;
std::cin.get();
return 0;
}
The exercise went like this: You've got 100 light bulbs. You take every number from 1-100 and for every lightbulb with the position a multiple of said number, you switch it's current state. So basically if you've got bulb 2, you first switch it ON because it's a multiple of 1, then you switch it OFF because it's a multiple of 2.
And you've got to check how many remaining lightbulbs are still open by the end.
The answer is 10.
The Main-Problem why you get no output is, that the code is causing an infinity-loop (The loop cant escape and will run forever) and you never reach the std::cout part of the code
Ok there's a lot going on and the first thing is (You probably will hear this a lot on this platform) don't use using namespace std; instead use the std::-prefix for c++-Standard Things. I think its ok to use if you start out, but its a really bad Practice.
Then another thing is, cin.get() already 'pauses' or interrupting the program until you entered an input so system("pause") really isn't needed here.
To get input simply do it like that:
int input;
std::cin >> input;
std::cout << "My output was: " << input;
Then another thing is, i dont really know what you try to do with the nested for-loops but in the second for-loop you have a conditions that doesnt really make sense
for(multiple=1; 100; multiple++)
^^^
What you probably want is something like
for(multiple=1; multiple<100; multiple++)
And then saying bulb/multiple==0 doesn't really make sense either, because its only true if bulb is 0, maybe you mean bulb%multiple==0 (modulo).
And there's probably a typo in one condition where you wrote bulb<<0 where you probably want to write bulb<0
But no matter what you do, it still runs into a infinite loop, because the conditions are weird. And in normal cases you really shouldn't change the iteration-variable of your loop inside your loop (only if you know thats exactly what you want) but in most cases that just breaks your program, especially if youre starting to learn the language.
Maybe if you say exactly what you want, we can help you more.

Creating a presentation in c++ ( I'm stuck ) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm working on a school project (I'm in seventh grade) and it's about why I want to be a computer programmer. I have to make a presentation of sorts about what a computer programmer is and what they do. I thought it would be a good idea to code my own presentation in a way. I've coded some of it already but i'm stuck. This is what I have so far,
#include <iostream>
#include <string>
using namespace std;
int main()
{
string question;
cout << "Type [1] to begin...";
cin >> question;
if(question == "1")
{
cout << "A computer programmer figures out the process of
designing, writing, testing, debugging, and maintaining the source c
ode for computer programs";
return 0;
}
}
Now what i want to be able to do is add a "goto" type of statement where it can go to something like "int second()" and cout something new like "what are programming languages?" and then a description of what they are after the user inputs something like "yes". Any help would be appreciated. I'm really new to c++. Thanks :)
I think this question is more suited for codereview, but since the code does not compile as is, we may as well help you with your broken code (and then you take it to codereview)
First, let's format your code. This is a useful skill to learn because it helps other coders help you write better code:
#include <iostream>
#include <string>
using namespace std;
int main() {
string question;
cout << "Type [1] to begin...";
cin >> question;
if(question == "1") {
cout << "A computer programmer figures out the process of
designing, writing, testing, debugging, and maintaining the source c
ode for computer programs";
return 0;
}
}
Easiest way to format is to copy it into an IDE, use the IDE to format, then copy it back here, select the code and press the button.
Now to solve this problem.
Your question seems centred around controlling the flow of the program - being able to transition from one stage to the next in a way that puts the user in control and only delegates control back to your program once the user has made a decision.
The problem
Ask the user to enter a 1
Display the following text
A computer programmer figures out the process of designing, writing,
testing, debugging, and maintaining the source code for computer
programs
Ask the user if they wanted to continue
If so, display the following:
what are programming languages?
4b. If not, end the program.
Ask the user if they wanted to continue
etc, etc
As you can see, there is indeed a pattern and this pattern comes down to the following:
Ask what the user wants to do
Do it
Repeat until you run out of slides or the user doesn't want to continue
And just like that, we have abstracted away the complexity and are only focused on following the pattern.
Pay attention to the repeat part because that is what allows this pattern to work for more than one slide of your presentation. There are many ways to represent the repeat part, and for that you should find some good tutorials to teach you some of them. I won't bother describing all of them (just search youtube, you will find tons), but for this particular problem, the best way to represent your pattern is with a do-while loop.
Here is what it will look like:
do {
// Ask the user a question
// Get the user's input
// validate the user's input
// if they want to see the slide show it
// other wise, leave this loop
while (I have not run out of slides);
This is psuedo-code, but here is how it transforms your code:
#include <iostream>// cin, cout
#include <string> // string
#include <vector> // vector
#include <cstddef> // size_t
using namespace std;
int main() {
vector<string> slides = {
"A computer programmer figures out the process of"
"designing, writing, testing, debugging, and maintaining the source c"
"ode for computer programs",
"what are programming languages?",
// Add more here
};
size_t current_slide_index = 0;
string user_response;
do {
cout << "Type [1] to continue: ";
cin >> user_response;
cin.ignore(100, '\n'); // This is used to skip to the next line
if (user_response == "1") {
cout << slides.at(current_slide_index) << std::endl;
} else {
break;
}
} while (++current_slide_index < slides.size());
cout << "Happy learning\n";
return 0;
}
A few notes
I used a vector to hold the slides. This is the most recommended collection type in C++. There are many others, but for the most part, a vector will serve you well.
cin >> does not normally go to the next line after reading something, so I had to manually shift it to the next line. That's the reason for cin.ignore(100, '\n');
As I said in the beginning, this question is more suited for codereview, so take what I've shown you here, make your changes as you learn more about it, and later on have it reviewed once again by the folks at https://codereview.stackexchange.com/.
I think you can try a pattern like this:
#include <iostream>
#include <string>
using namespace std;
void q1()
{
cout << "A computer programmer figures out the process of "
"designing, writing, testing, debugging, and maintaining the source "
"code for computer programs.\n";
}
void q2()
{
cout << "what are programming languages? ...\n";
}
// void q3() ... ... ...
int main()
{
string question = "1";
cout << "Type [1] to begin... ([99] for quiting): ";
cin >> question;
/* while loop: http://en.cppreference.com/w/cpp/language/while */
while (question != "99") {
/* if statement: http://en.cppreference.com/w/cpp/language/if */
if (question == "1") {
q1(); // this is a "function call", you are invoking q1()
}
else if (question == "2") {
q2();
}
// else if(... q3() ... q4() ... and so on.
/* read a new response for checking in the while condition */
cout << "Next? ";
cin >> question;
}
return 0;
}
Using the the goto can be accomplished as below. You can also use the SWITCH..CASE to accomplish the same.
int main()
{
string question;
label:
cout << "Type [1,2,....] to begin...";
cin >> question;
if(question == "1")
{
cout << "A computer programmer figures out the process of designing, writing, testing, debugging, and maintaining the source code for computer programs" << endl;
goto label;
}
if(question == "2")
{
cout << " A programming language is a type of written language that tells computers what to do in order to work" << endl;
}
First of all it's fantastic that you want to be a programmer and I wish you luck on your assignment. You can certainly ask about learning c++ here, but this site is focused on question and answer, not on teaching. You have a pretty specific question here, but it can be solved in a broad variety of ways, which I'm sure you'll learn about soon.
What I would recommend for a presentation is to ignore the input so you don't have as many branches in your code. Then you can simply add the next part directly after the first.
string question;
cout << "Type [1] to begin...";
cin >> question;
cout << "A computer programmer figures out the process of
designing, writing, testing, debugging, and maintaining the source c
ode for computer programs";
cout << "Type [1] to continue...";
cin >> question;
cout << "Part 2";
return 0;
On the language choice:
C++ is definitely the wrong language to start. Because it's the most complicated programming language in existence(it's not an opinion, it's Science). You can't fully understand C++ without understanding other subset languages it contains. For example, C.
Don't listen to people that say you can code without complete understanding. You'll waste your time and won't become real engineer.
C is most stable and continuously respected programming language in Human History. Most modern CS celebrities like Mark Zucker., etc, started with C.
C can look as impressive as C++, if that's what you're interested in.
On the problem:
What you're doing is console dialog with finite determined input. In CS it's called "finite automata" or "state machine". State machine can be represented as a bunch of circles(states) and arrows between them: "Do this if the next input is that". Pick a starting circle and the end circle. i.e. your program terminates when it gets there. Such diagram are really that simple.
Step-by-step solution(fill in blanks):
0)Define IO.
Your input: 1 integer:
int input;
Your output: const strings of characters.
1) Draw state machine diagram, with states and arrows between them. Denote each arrow as Integer, output_string.
For example, '1, "A computer programmer figures.."' - is an arrow from the starting state (0) to some other state (1).
2) Create integer for states:
int state = 0;
3) Translate your state machine into diagram as following:(You don't need goto's. Loop can play as a goto)
while(scanf("%d", &input)){
switch(state){
case 0:
switch(input){
case 1:
printf("A programmer blabla\n");
state = 2;
break;
case 2:
...
{
break;
case 1:
...
case 10: // - last state
switch(input){
...
default:
printf("goodbye");
return 0; // terminate the program;
}
}
}
You need to know about while loops, Switch statements, printf() and scanf(). Wikipedia is ok.
After you did this put the code inside main function and make necessary includes and you're good to go. You need to complete your homework yourself.

Validating a string to be all digits

Hi I'm having trouble validating this string to be all decimals, even if I type in 9999 it still tell me my if statement comes out false. I think it's a typo but I don't know where.
cout<<"Enter a very large number"<<endl;
cin>>In1; //inputs a string
for(int i=0; 0<In1.length();i++){ //the loop that goes thru each index
if (!(isdigit(In1[i]))){ //validates each index
//tells the user to try again
cout<<"You did not enter a valid input, please try again"<<endl;
In1="";
cin>>In1;
i=0;//starts the loop over if reached
}
}
I keep receiving the "You did not enter a valid input, please try again" regardless of whether I type it right or wrong.
for(int i=0; 0<In1.length();i++){
See what you did? Change to
for(int i=0; i<In1.length();i++)
In your loop condition you need to compare i with In1.length().
You might want to change
0<In1.length()
to
i<In1.length()
Using
#include<algorithm>
if ( std::find_not_if( in1.begin(), in1.end(), isdigit ) != in1.end() ){ ...
might have prevented this unfortunate incident, and is also quite clear on the intent. The dual _not/!= muddles it slightly but still.
There are quite a few convenience algorithms, replacing common uses for simple for- statements. Most of them are on the form
do_this( where_to_start, where_to_end, do_this_operation )
There is usually nothing special or dramatic with these function, they apply the operation to each element in the start-end sequence.
You have find, count, copy, and generate to mention a few. Their purpose is to clarify the intent of your for-statement. You can find a complete list at http://en.cppreference.com/w/cpp/algorithm
You will almost certainly find that, over time, you become more adept at seperating different parts of code into the functionality that they each provide. Making debugging and later modification considerably easier.
It also makes, as Captain Giraffe points out, the intent of the code considerably more clear - something that can only make reading the code easier & quicker.
I've not used std::find_not_if, opting instead to use the method that you've chosen (based on the assumption that the important thing is knowing how to get the right answer, as opposed to simply supplying the right answer - well, that and me not knowing of find_not_if's existence :grin:) You'll see that I've chucked it into it's own function, which I call from main. The function also only performs a single task - that of checking the validity of the string. Any attempt to prompt the user for this text, re-prompt in the case of error and finally, take action on the correct input is the sole responsibility of the code that calls isValidNumericalString - there's no reason you couldn't throw those functions into their own functions, as opposed to having a single, large body of main.
#include <iostream>
using namespace std;
// returns true if all characters in string are numerical (0-9)
bool isValidNumericalString(string inputString)
{
int i, n = inputString.length();
for (i=0; i<n; i++)
if ( !isdigit(inputString[i]) )
return false;
return true;
}
int main()
{
string In1;
cout << "Enter a very large number (digits 0-9 only. 10e1 is unacceptable): ";
cin >> In1;
while (!isValidNumericalString(In1))
{
cout << "You did not enter a valid input, please try again :p" << endl;
cout << "Enter a very large number (digits 0-9 only. 10e1 is unacceptable): ";
cin >> In1;
}
cout << "Congratulations - '" << In1 << "' is a valid string representation of a number" << endl;
return 0;
}

Strange behaviour when reading in int from STDIN

Suppose we have a menu which presents the user with some options:
Welcome:
1) Do something
2) Do something else
3) Do something cool
4) Quit
The user can press 1 - 4 and then the enter key. The program performs this operation and then presents the menu back to the user. An invalid option should just display the menu again.
I have the following main() method:
int main()
{
while (true)
switch (menu())
{
case 1:
doSomething();
break;
case 2:
doSomethingElse();
break;
case 3:
doSomethingCool();
break;
case 4:
return 0;
default:
continue;
}
}
and the follwing menu():
int menu()
{
cout << "Welcome:" << endl
<< "1: Do something" << endl
<< "2: Do something else" << endl
<< "3: Do something cool" << endl
<< "4: Quit" << endl;
int result = 0;
scanf("%d", &result);
return result;
}
Entering numerical types works great. Entering 1 - 4 causes the program to perform the desired action, and afterwards the menu is displayed again. Entering a number outside this range such as -1 or 12 will display the menu again as expected.
However, entering something like 'q' will simply cause the menu to display over and over again infinitely, without even stopping to get the user input.
I don't understand how this could possibly be happening. Clearly, menu() is being called as the menu is displayed over and over again, however scanf() is part of menu(), so I don't understand how the program gets into this error state where the user is not prompted for their input.
I originally had cin >> result which did exactly the same thing.
Edit: There appears to be a related question, however the original source code has disappeared from pastebin and one of the answers links to an article which apparently once explained why this is happening, but is now a dead link. Maybe someone can reply with why this is happening rather than linking? :)
Edit: Using this example, here is how I solved the problem:
int getNumericalInput()
{
string input = "";
int result;
while (true)
{
getline(cin, input);
stringstream sStr(input);
if (sStr >> result)
return result;
cout << "Invalid Input. Try again: ";
}
}
and I simply replaced
int result = 0;
scanf("%d", &result);
with
int result = getNumericalInput();
When you try to convert the non-numeric input to a number, it fails and (the important part) leaves that data in the input buffer, so the next time you try to read an int, it's still there waiting, and fails again -- and again, and again, forever.
There are two basic ways to avoid this. The one I prefer is to read a string of data, then convert from that to a number and take the appropriate action. Typically you'll use std::getline to read all the data up to the new-line, and then attempt to convert it. Since it will read whatever data is waiting, you'll never get junk "stuck" in the input.
The alternative is (especially if a conversion fails) to use std::ignore to read data from the input up to (typically) the next new-line.
1) Say this to yourself 1000 times, or until you fall asleep:
I will never ever ever use I/O functions without checking the return value.
2) Repeat the above 50 times.
3) Re-read your code: Are you checking the result of scanf? What happens when scanf cannot convert the input into the desired format? How would you go about learning such information if you didn't know it? (Four letters come to mind.)
I would also question why you'd use scanf rather than the more appropriate iostreams operation, but that would suffer from exactly the same problem.
You need to verify if the read succeeded. Hint: it did not. Always test after reading that you successfully read the input:
if (std::cin >> result) { ... }
if (scanf("%d", result) == 1) { ... }
In C++ the failed state is sticky and stays around until it gets clear()ed. As long as the stream is in failed state it won't do anything useful. In either case, you want to ignore() the bad character or fgetc() it. Note, that failure may be due to having reached the end of the stream in which case eof() is set or EOF is returned for iostream or stdio, respectively.

Win32 - Processing the backspace command?

TL;DR I need to know which characters are deleted with Win32.
Basically, I need to know what characters are deleted, each time they are. Considering that there are three methods to delete, (Backspace, delete, and Right click>Delete) I'm not sure if I'll just be re-using the same code or what. There's also the option of multiple characters being selected, as well as the option for undo/redo. There's probably something else I'm missing.
As I said above, I need to know which characters are deleted, and how to use undo/redo and how to tell if when using those if characters were added or deleted. (If that's something easily Google, tell me, I just thought of them as I've been writting this post)
What exactly are you talking about? The win32 default control windows? You can subclass them to do that...
http://msdn.microsoft.com/en-us/library/bb773183%28v=vs.85%29.aspx
Well, if you come up with something specific code-wise and don't quite know what to do, just ask here and see if someone can't help you.
With regard to undo/redo, I believe it all depends on the size of the focus area, and I'm not sure how MS does it for, say, Word, or Excel, etc.
But for short stuff, what I posted above should work in a universal sense, but apparently not in your case.
Nevertheless, if anyone wanted to shorten the iteration in the above, they could replace the loop above by starting right at the end of shorter string, like this --
for(int i = iLen2, x=0; i < iLen1; i++, x++)
szAnswer[x]=str1[i];
This simply confines the interation to the missing characters.
Here is a small Win32 Console application that will demonstrate a very simple way to determine which characters have been deleted. You can easily adapt this to a Win32 App, add iterations to it, and so on
#include <iostream>
#include <string>
using namespace std;
#ifndef MAX_PATH
#define MAX_PATH 256
#endif
int main()
{
int iLen1, iLen2, iResult;
char str1[]="The old red box.";
char str2[]="The old red";
char szAnswer[MAX_PATH]="";
// strcmp() will be greather than 0
// if str1 is longer than str2
if(strcmp(str1, str2) > 0)
{
iLen1=strlen(str1);
iLen2=strlen(str2);
iResult=iLen1-iLen2;
cout << "The number of missing characters is " << iResult << endl << endl;
// now lets see which characters are missing
// we iterate through the entire length
// of str1, which is the full text, but
// we only start puting characters into our
// result when we get to the end of str2
for(int i=0, x=0; i < iLen1; i++)
{
if(i >= iLen2)
{
szAnswer[x++]=str1[i];
}
}
cout << endl << "The missing characters are --" << endl << endl << szAnswer << endl << endl;
}
return 0;
}