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

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.

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.

trouble with loops, functions, and organization´

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".

C++ Xcode won't run my for loop (very short) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want to continue reading Stroustrup PPUC++, but I'm stuck! I wrote a BleepOutTheBadWords program a few days ago and it worked. I tried to write the program with my younger brother a few days ago and I failed. This is essentially a program I wrote to figure out what is going wrong. It is not printing out each word in the "sentence" vector.
#include <iostream>
#include "std_lib_facilities.h" // Stroustrup header file
using namespace std;
int main()
{
cout << "Write a sentence." << endl;
vector<string> sentence;
// Put user input into "sentence" vector
for (string word; cin >> word; )
sentence.push_back(word);
// Print each word in "sentence" vector
for (int i = 0; i < sentence.size(); ++i)
cout << sentence[i] << endl;
// Keep window open and return 0 to show program succeeded
keep_window_open();
return 0;
}
The answer is going to be obvious. Please just state the obvious if you are so kind. I looked through the suggested readings on two different pages before posting this.
XCode won't run my for loop
Your loop is running. The thing that you missed is how to terminate the loop.
for (string word; cin >> word; )
This loop will terminate when cin >> word evaluates to false. Normally, i.e. without an error condition, it will evaluate to false when your input is finished. The exact process to signal an end of stream, or EOF, is platform dependent. If you are running this program on OSX then the most common way to signal EOF is to hit Ctrl + D button, unless you changed the default configuration of your keyboard. Once you signal EOF this input loop will terminate and you will be able to see the output.
I'm pretty much sure that Stroustrup discussed this on his book (though I can not refer to an exact page number). However "Chapter 10: Input and Output Streams" of his books covers these things in detail.

Program won't quit if user enters -1 while using return 0;

Here's one of the return 0; sections I have in my main() function:
cout << "Would you like to remove a Pokemon? (Y/N): ";
cin >> removePokemonChoice;
cin.ignore(5, '\n');
if (toupper(removePokemonChoice) == 'Y')
{
deletePokemon();
cout << "New list:" << endl;
displayPokemon();
}
if (removePokemonChoice == -1)
return 0;
currently, it won't exit out of the program if the user enters -1. I can't use an else statement because I want the program to loop (there's a similar question after this bit that I want to come after) until -1 is entered.
Your program asks "(Y/N)" so I suppose removePokemonChoice is a char type... it is strange to expect -1 value from char data. By the way "-1" need 2 characters, so it must be string :-)
Think of it this way:
You're writing a program that asks you whether or not you want to remove a Pokemon. Normally, like in the actual game itself when Prof. Oak prompts you with a confirmation of do you want Charmander (and the answer is yes, you always go with the fire type, no exceptions). The choices the game will always give you is "Yes" or "No".
Would you ask a yes/no question and expect a numerical answer back? While it is true the compiler converts and does numerical stuff in the background, what you really should be thinking about is, is it intuitive to the user?

Debugging Going Too Far Into My Code

I am currently using Visual Studio 2013. I've never used the debugger for my C++ code before, but I used to use it all the time for programming my MSP430. Anyways, I'm trying to get back into programming and trying to use the debugger to step through my code and follow the logic of my if/else statements. When I try to use the debugger to do this, once it begins going into all of the prewritten C++ code for terms such as if, #include, ect. I am trying to get my debugger to ignore all of the standard C++ behind the scenes details, and just step through my code. I messed with Microsoft "My Code Only" feature, but can't seem to get it to do what I desire. Worst case, I guess I have to set a breakpoint after after line I want to go through, but was curious if there was an easier method. Thanks!
UPDATE:
Here is the example code I am using to test your suggestions.
#include <iostream>
#include <string>
using namespace std;
int main(){
cout << "line one\n";
cout << "line one\n";
cout << "line one\n";
cout << "line one\n";
cout << "line one\n";
cout << "line one\n";
cout << "line one\n";
cout << "line one\n\n";
string input;
cin >> input;
cout << "line one";
cout << "line one";
cout << "line one";
return 0;
}
It's nothing pretty, but I'm trying to just step from the first cout statement, see it displayed in my console window, click a button, have it display the next cout statement, repeat, well repeatedly.
I'm sure I'm just not implementing the suggestions correctly. When I try the step out method, it ends up running all of my cout statements. Is this just because I'm trying to do a cout operation, instead of a logic tree, such as if/whiles/ ect?
Here's the illogical mess I'm actually trying to use this method to trace my path through the logic. It's pretty bad code, but before I scrap it and rewrite it, I was trying to figure out how to step through it and trace the mess.
#include <iostream>
#include <string>
using namespace std;
int main(){
string action;
bool running = 1;
int turn = 1;
while (running){
//display map
if (turn == 1){
//ask for user input
cout << "Choose a planet (ex: A1 or D4) or END turn: ";
cin >> action;
if (action == "END"){
if (turn == 1){
turn = 2;
}
else {
turn = 1;
}
}
}
else{
cout << "It is not your turn";
turn = 2;
}
//change players turn
}
return 0;
}
I'm trying to step through my nested conditionals, because when I run the code it just prints "It is not your turn" for ever. I'm pretty sure I know the real reason it does this, but debugging this snippets not the purpose of the question. :)
When stepping through the code with MSVC debugger you usually have two options (to be used after setting a breakpoint or having your debugger started and your program paused somehow):
Step into (shortcut F11)
Step out (shortcut F10)
the first enters in detail into function calls, operator overrides, object construction and doesn't leave an instruction until everything has been "stepped through", while the latter is what you want: a "high level" overview that just skips everything in which an instruction can be decomposed and "jumps" to the next instruction.
The "Just my code" used to be a managed-feature only but it seems they've enabled it for native C++ as well. This can be useful to avoid pestering the stack trace with lots of unnecessary entries, but the step-in and step-out mechanism still holds.
Edit: the above works IFF your app is compiled in debug mode. Release mode is an optimized version for.. well.. releasing programs to the end-users. That means it won't have debugging information and thus setting breakpoints won't work as expected.
For the code above:
Make sure you're compiling in debug mode (you can change this by going into the project properties)
Put a breakpoint on the first statement of your main() function
Launch the debugger
When the execution stops on the line you put the breakpoint into, press F10 to step to the next instruction
Notice that the execution will be passed to the program when you step over a console input operation to let you digit something on the terminal screen. It will resume in the debugger as soon as you enter the newline character.
In addition to just "stepping out" immediately after stepping into an operator or new or an accessor that is not of interest, there is also a context menu item "Step into ..." that will bring up a dialog asking which function you actually want to step into. When this works (my experience is that it is often a bit flakey), it will allow you to completely bypass other getters, operators, etc., that are used before the function call of interest is called.
Sometimes, however, the simplest thing is to set a breakpoint in the function that you want to step into and then Continue. I find this the best approach if I'm needing to do it repeatedly.