-2

I have been working on this little program in Visual Basic 2013 in an attempt to create a sort of tiered structure for user-input commands. Basically, I want the first of a two word input to direct the program to an area of code with a set of responses for the second word. In this program, the first word can either be "human" or "animal." These words direct the program to functions that select the kind of animal or human.

#include "stdafx.h"
#include <iostream>
#include <sstream>

void ifAnimal(std::string b) //This is the set of responses for a first word of "Animal"
{

  if (b == "pig")
   {
    std::cout << "It is a pig." << std::endl;
   }

  if (b == "cow")
   {
    std::cout << "It is a cow." << std::endl;
   }
}

void ifHuman(std::string b) //This is the set of responses for a first word of "Human"
{
  if (b == "boy")
   {
    std::cout << "You are a boy." << std::endl;
   }

  if (b == "girl")
   {
    std::cout << "You are a girl." << std::endl;
   }

}

int main()
  { 



while (1)
{
    std::string t;
    std::string word;
    std::cin >> t;
    std::istringstream iss(t); // Set up the stream for processing
    int order = 0; 


    //use while loop to move through individual words
    while (iss >> word)
    {

        if (word == "animal") 
        {
            order = 1;
            continue; //something wrong with these continues
        }

        if (word == "human")
        {
            order = 2;
            continue;
        }

        if (order == 1) 
        {
            std::cout << "The if statement works" << std::endl;
            ifAnimal(word);
        }

        if (order == 2) 
        {
            std::cout << "This one too" << std::endl;
            ifHuman(word);
        }
     }

   }
  return 0;

}

The problem is that whenever the program reaches the continue statements, the if statements calling my functions are not triggered. No text is displayed at all. If the continue statements are removed, the if statements trigger, but then the corresponding function has the wrong word. Am I unaware of something that those continues are doing? Is there a better way to accomplish what I want to do?

  • 3
    Are you sure you know what `continue` means? – LogicStuff Mar 25 '15 at 21:44
  • `continue` immediately skips to the innermost `while` or `for` loop brace. So yes, if the first `if` hits, none else will be executed. – Jongware Mar 25 '15 at 21:45
  • 1
    The logic is really flawed here. When `word == "animal"` you then check if `word == "pig"`. **It can't be both**. – Drew Dormann Mar 25 '15 at 21:48
  • In my opinion a better solution would be to use if - else for your if blocks. One if-else for checking the word and one if - else for checking the order. then you probably do not need continue at all. But depends on your case. – bbakiu Mar 25 '15 at 21:49

2 Answers2

1

what continue means is skip the rest of the loop and go back to the top. whatever is after the continue statement won't be executed if continue gets hit.

It looks like you expect your word to be two words at the same time, so once you execute ifAnimal() none of the cases in ifAnimal will be met. word will never be "pig" or "cow" when you call that method because you only ever call that method when word is equal to "animal", and you don't change it after that.

  • I put `t` into a getline instead of a cin, and got the whole line of code into `t`. The while loop functioned correctly then, extracting the stream word by word. I did not even have to remove the `continue`. – Robin the Sprite Mar 27 '15 at 19:14
0

Continue means "Go immediately to the top of the loop, and start over again". You do not want that at all.

  //use while loop to move through individual words
    while (iss >> word)
    {
        if (word == "animal") 
        {
            order = 1;
        }
        else if (word == "human")
        {
            order = 2;
        }

        if (order == 1) 
        {
            std::cout << "The if statement works" << std::endl;
            ifAnimal(word);
        }

        if (order == 2) 
        {
            std::cout << "This one too" << std::endl;
            ifHuman(word);
        }
     }
abelenky
  • 63,815
  • 23
  • 109
  • 159
  • 3
    I wouldn't say "Go immediately to the top of the loop, and start over again", because in the `for` loop, control variables wouldn't change - rather "go to the end". – LogicStuff Mar 25 '15 at 21:50
  • 1
    Strictly speaking, `continue` causes passes control to the end of the loop. If there is a `continue` statement in a `do {} while();` loop, the `while` statement will be executed after the `continue;` statement. – R Sahu Mar 25 '15 at 21:51