Home > front end >  Loop returning Message depending on how many characters you type
Loop returning Message depending on how many characters you type

Time:12-26

How come the loop is returning the message Enter # when done MORE instead of once depending on how many words you enter? EG type a single letter it loops the message Enter # when done but if you type what? it returns it Enter # when done x4 ....the same amount of letter in the word.I am new to c coming from c so im confused.Dont worry about other stuff in the code I need help with this. Thank you :)

#include <iostream>
#include <stdio.h>

int main() {
  char sup;
  while (sup != '#') {
    std::cout << "Hi\n";
    std::cout << "Enter # when done";
    std::cin >> sup;
    if(sup == '#') {
      std::cout << "Ok you want to go.";
    }
  }
  std::cin.get();
}

CodePudding user response:

Example with std::string

// preferably do not include stdio.h in C   programs
#include <string> 
#include <iostream>

int main()
{
    std::string input;  // use standard library string class

    while (input != "#")
    {
        std::cout << "Hi\n";
        std::cout << "Enter # when done : ";
        
        std::cin >> input;
        if (input == "#")
        {
            std::cout << "Ok you want to go.\n";
        }
    }

    return 0; // <== important to otherwise your program is ill-formed
}

CodePudding user response:

As you set in while loop that (sup != '#'), it will take input character until you type #.

  • Related