Trying to Understand

0 Top Honk · June 18, 2014
So I just started watching Buckys videos yesterday since I decided to pick up C++, just because. Pretty simple so far, granted I'm only on his 9th video, not particularly hard to commit to memory or anything. So I'm at the part where we are making functions other than int main() using void and so I got inventive because I don't like to become dependent on tutorials so I...venture, if you will -- trying to experiment. So I am trying to basically make a simple Y/N program. Up until now, we've been working with integers for calculators. So I did a bit of poking around in a powerpoint my cousin made since she is actually good at C++ and saw there was a value for characters called Char. So I figured...since I'm not working with numbers, int wouldn't be correct, so I'll use char instead since I'll be using characters. 

Long story short, I made it, but when I press y, nothing happens. However, when I press n, both articles of text appear which were supposed to be situational, depending upon y or n being entered. My guess, I did the if's wrong. Just wanna make sure.


#include <iostream>

using namespace std;

void boop();

int main()
{
boop();
return 0;
}

void boop()
{
char y;
char n;

cout << "Don't boop mah nose!
" << "Boop? Y/N
";
cin >> y;
cin >> n;

if (y == y)
{
cout << "
Ah! My nose! No!!!
";
}

if (y == n)
{
cout << "
Thank you for not booping my nose.
";
}
}



Post a Reply

Replies

Oldest  Newest  Rating
-2 Alexander Taylor · June 19, 2014
Lets go through this code step by step


#include <iostream>

using namespace std;

void boop();

int main()
{
boop();
return 0;
}


this is all correct,
you include the input output library,
set up your namespace,
declare your function,
define your main function


void boop()
{
char y;
char n;


you define your function,
but then you create 2 char variables, you are only getting a single input so you only need 1


    cout << "Don't boop mah nose!" << "Boop? Y/N";
cin >> y;
cin >> n;


you then output to the user that they should give input, all is fine there
then you try to get input twice, again, you are only trying to get a single input, you dont need to get both.

a char variable, does not have the value of the variable name, that is to say, when you do cin >> y; it doesnt get if the user pressed y, it takes whatever the user input and stores it in that variable


if (y == y)
{
cout << "Ah! My nose! No!!!";
}

if (y == n)
{
cout << "Thank you for not booping my nose.";
}


you then compare the variables to themselfs, y will always = y, if you want to check if the value of a variable equals a character you would do " y== 'y' "
this way you check if the value of y is equal to the character y

also you use 2 if statements, this will cause you to do a check twice in every case, even if the first case is correct, which would mean the second case is false. you should use the else if statement

here is how i would have written the program you where trying to create


#include <iostream>

using namespace std;

void boop();

int main()
{
boop();
return 0;
}

void boop()
{
char input;

cout << "Don't boop mah nose!" << "Boop? Y/N";
cin >> input;

if (input == 'y')
{
cout << "Ah! My nose! No!!!";
}
else if (input == ',')
{
cout << "Thank you for not booping my nose.";
}
}


Hope this helps
0 Johnny Lindsey · June 18, 2014
Well, String is actually a data type, used as a string of characters. I can't remember the last time I used the char data type. And input is just the name of the variable. Glad to help out!
0 Top Honk · June 18, 2014
Yep, you were right on the money, Johnny

Though I can't say I'm surprised that I mucked it up. Haven't learned string inputs yet.
0 Top Honk · June 18, 2014
For some reason it copy and pasted weird, but the way the formatting was in the OP wasn't how it looked in the actual .cpp, lol. That was weird.
+2 Johnny Lindsey · June 18, 2014
Not much of a C++ expert (Java is my strong suit), but I've been on that street before. I think I may have a solution:
#include <iostream>
 
using namespace std;

void boop();

int main()
{
boop();
return 0;
}

void boop()
{
string input;

cout << "Don't boop mah nose!" << "Boop? Y/N";
cin >> input;


if (input == "Y")
{
cout << "Ah! My nose! No!!!";
}

if (input == "N")
{
cout << "Thank you for not booping my nose.";
}
}


There are probably better forms of this, but, like I said, I'm not that familiar with C++.
+1 Mathias Frits Rørvik · June 18, 2014
The formatting is all messed up. 
  • 1

C++

106,974 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator