-2
#include <iostream>
#include <vector>
#include <ctime>
#include <algorithm>
#include <string>
using namespace std;



int main()
{
    string bob;
    string alice;
    string name;
    for (int i = 0; i < 1; i++) {
        cout << "Please enter the name bob or alice ";
        cin >> name;
    }
    if (name == alice && name == bob) {
        cout << " Greetings " << name;
    }
    else {
        cout << "This name is invalid try again." << endl;
    }

    return 0;
}

Hello, I understand that this may be an unimportant or irrelevant topic, but I have a review test coming up in my computer science class. I'm trying to get a quick refresher in all that I've learned in last semester and I'm curious to why in this line of code when you type in bob or alice it should come out saying Greetings alice, or Greeting bob which ever you have inputted. Though right now it's completely skipping it and going to invalid try again. It's almost as if the information is being completely bypassed and then the code coming to a conclusion that nothing has been inputted.

Again I'm extremely sorry for the stupid question I'm just looking for a refresher.

  • `alice` and `bob` are empty strings in your code. You want either `std::string bob = "bob";` or `name == "bob"`. – Drew Dormann Jan 19 '22 at 21:48
  • 1
    Also, `&&` means "AND". You likely don't want to check if `name == alice` AND `name == bob`. How can it be alice AND bob, unless they are the same? – Drew Dormann Jan 19 '22 at 21:50
  • 1
    The for loop is correct but unnecessary. I mean when you want to execute code 1 time its odd to want to use a for() or any other type of loop. – drescherjm Jan 19 '22 at 21:53

1 Answers1

2

Initialize your strings and change the && which is a Logical AND to a Logical OR ||

Also it would be better to void using namespace std;, more here . Also there is no point in looping where you looped,because name will hold only the last input. Below the code I added maybe a case you wanted to catch,which loops some inputs and Greets Bob or Alice if found.

int main()
{
    std::string bob = "Bob";
    std::string alice = "Alice";
    std::string name;

    std::cout << "Please enter the name bob or alice ";
    std::cin >> name;
    if (name == alice || name == bob) {
        std::cout << " Greetings " << name;
    }
    else {
        std::cout << "This name is invalid try again." << endl;
    }

    return 0;
}

Perhaps what you wanted to do with your loop?

int main()
{
    const std::string bob = "Bob"; //This will not change and DOESN'T need to change during runtime,so we make it const (same as alice below)
    const std::string alice = "Alice";
    std::string name; //This WILL change in fact in runtime,because we take input here right? So we don;t make it const

    for(int i = 0 ; i < 1 ; ++i){
      std::cout << "Please enter the name bob or alice " << std::endl;
      std::cin >> name;
      if (name == alice || name == bob) {
          std::cout << " Greetings " << name << std::endl;
      }
      else {
          std::cout << "This name is invalid try again." << std::endl;
      }
    }
    return 0;
}

Reading suggestion as stated in comments : const

laegirl
  • 144
  • 13
  • tactical note: consider making `alice` and `bob` `const std::string`. This way the compiler can catch it if they are accidentally used in such a way that they will be modified. – user4581301 Jan 19 '22 at 21:57
  • I thought of that,I didnt want to overwhelm him with tons of information though. I will add it in my edits since its being brought up – laegirl Jan 19 '22 at 21:59
  • I have a quick question for a better understanding on my part why do you add the parentheses around bob and alice? is it because a string value, i mean what's the difference between. Again I do apologize for the stupid question. String bob = bob string alice = alice – John Richardson Jan 19 '22 at 22:00
  • @JohnRichardson If statements have the syntax if(condition) { //do stuff}. Here your condition is Alice OR Bob which is name == "Alice" || name == "Bob" .Wrap it around with parenthesis and you got what I typed – laegirl Jan 19 '22 at 22:01
  • Thank you for clarifying with me I do appreciate it. – John Richardson Jan 19 '22 at 22:03
  • Leave it out of the answer. It's just noise in the answer's context, a side note that a future asker might find helpful. – user4581301 Jan 19 '22 at 22:03
  • @JohnRichardson If you have any question lets us know while we're at it,else you can accept an answer :) – laegirl Jan 19 '22 at 22:08