#include <iostream>
usingnamespace std;
int main()
{
cout << "I am just going to run through some input and output statements with loops, go over some\n";
cout << "functions, look at arrays, vectors, c-strings, and last but not least,\n";
cout << "and definitley everybodys favorite, pointers!\n\n";
cout << "Alright, now time for some input!";
cout << "Who is your favorite basketball player?\n";
cout << "Type LeBron or Kobe, exactly as you see it appear, then press enter\n";
string input, LeBron, Kobe; //used to hold the name of favorite player
cin >> input; //gets the input from the user
if(input == LeBron)
{ //if and else loop compares input and
cout << "Good choice!"; //prints correct output depending on
}
//the logic assigned
else
{
cout << "Kobe is NOT a VERY good basketball player atm";
}
return 0;
}
#include <iostream>
#include <string>
int main()
{
std::cout << "I am just going to run through some input and output statements with loops, go over some\n";
std::cout << "functions, look at arrays, vectors, c-strings, and last but not least,\n";
std::cout << "and definitley everybodys favorite, pointers!\n\n";
std::cout << "Alright, now time for some input!";
std::cout << "Who is your favorite basketball player?\n";
std::cout << "Type LeBron or Kobe, exactly as you see it appear, then press enter\n";
std::string input;
std::string LeBron = "LeBron";
std::string Kobe = "Kobe"; //used to hold the name of favorite player
std::cin >> input; //gets the input from the user
if (input == LeBron)
{
//if and else loop compares input and
std::cout << "\nGood choice!\n"; //prints correct output depending on
}
//the logic assigned
else
{
std::cout << "\nKobe is NOT a VERY good basketball player atm\n";
}
return 0;
}
Or you could just compare your input against a string literal:
#include <iostream>
#include <string>
int main()
{
std::cout << "I am just going to run through some input and output statements with loops, go over some\n";
std::cout << "functions, look at arrays, vectors, c-strings, and last but not least,\n";
std::cout << "and definitley everybodys favorite, pointers!\n\n";
std::cout << "Alright, now time for some input!";
std::cout << "Who is your favorite basketball player?\n";
std::cout << "Type LeBron or Kobe, exactly as you see it appear, then press enter\n";
std::string input;
std::cin >> input; //gets the input from the user
if (input == "LeBron")
{
//if and else loop compares input and
std::cout << "\nGood choice!\n"; //prints correct output depending on
}
//the logic assigned
else
{
std::cout << "\nKobe is NOT a VERY good basketball player atm\n";
}
}
I am by no means great at c++, but I got your program to work by simply getting rid of the strings LeBron and Kobe, including string at the top, and putting LeBron and Kobe in quotes in the loop. I believe the issue was that you were declaring LeBron and Kobe as separate strings rather than something that could be set in the input string.
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "I am just going to run through some input and output statements with loops, go over some\n";
cout << "functions, look at arrays, vectors, c-strings, and last but not least,\n";
cout << "and definitley everybodys favorite, pointers!\n\n";
cout << "Alright, now time for some input!";
cout << "Who is your favorite basketball player?\n";
cout << "Type LeBron or Kobe, exactly as you see it appear, then press enter\n";
string input; //used to hold the name of favorite player
cin >> input; //gets the input from the user
if(input == "LeBron")
{ //if and else loop compares input and
cout << "Good choice!\n"; //prints correct output depending on
system("PAUSE"); /*System pause so you can read the output rather than it rapidly closing out */
}
//the logic assigned
else if (input == "Kobe")
{
cout << "Kobe is NOT a VERY good basketball player atm";
}
return 0;
}
Thanks so much! I see what i did! Also, I noticed you implemented the std thing into the code where there are cout and cin statements. Is this something i should just get into a habit doing now as i move into computer science 2?
Honestly I would say that implementing namespace std is a preference, if you're comfortable with typing std:: before stuff then you can do that. I just think it makes it simpler for reading through and typing but if you prefer it that way then that's fine too.
but I got your program to work by simply getting rid of the strings LeBron and Kobe, including string at the top, and putting LeBron and Kobe in quotes in the loop.
You did something similar to what I did with my second version, the one that used a string literal for input comparison.
If you have only TWO choices you do not need to include another comparison check on your else statement.
If you have to use system("pause"); then placing in in your if block is not a good idea. What if "the user" chooses "Kobe?" *PLOOF!* Console window disappears immediately after displaying the output.
Place the system("pause"); statement AFTER your if/else blocks and before your return 0; statement.
Using system("pause"); is a very, very, VERY BAD idea.
Thanks so much! I see what i did! Also, I noticed you implemented the std thing into the code where there are cout and cin statements. Is this something i should just get into a habit doing now as i move into computer science 2?
For a price of a few extra keystrokes, trying to debug a multiple file/multiple 3rd party library project becomes manageable within the lifetime of most sentient lifeforms.
If you have only TWO choices you do not need to include another comparison check on your else statement.
I know I honestly forgot that was in there, I had put it in because the first time I ran his program to try to see what was wrong it gave me the else output and I put that there before I realized what the actual problem was.