Best programming language?

Alright for this part of the code:

Code:
if (a<b) {
cout<<"You entered 'a' which equals: "<< a <<endl;
}
else {
cout<<"You entered 'b' which equals: "<< b <<endl;
}

You may think variable B is incorrect, but actaully it isnt. B is never outputted to the console because B is greater than A by the time it gets to the if statement. Basically the if statement is saying if variable A is less than variable B print variable A's value; else print B's value.

Getting back to your assumption last night, Visual Studio allows me to program in VB, C++, C#, or J#. The added on function in my posted code is probably specific to VS, autogenerated when I compiled the code I imagine.

Now back to your code. What you need to do is create a string variable and assign it the string returned from w/e the user entered and than use if statements to output the correct value depending on the variable the user entered, rather than comparing the values of the variable.

@ CG I'm shocked... thought you were going to say C# lol
 
Last edited:
Alright for this part of the code:

Code:
if ([B]a<b[/B]) {
cout<<"You entered 'a' which equals: "<< a <<endl;
}
else {
cout<<"You entered 'b' which equals: "<< b <<endl;
}
You may think variable B is incorrect, but actaully it isnt. B is never outputted to the console because B is greater than A by the time it gets to the if statement. Basically the if statement is saying if variable A is less than variable B print variable A's value; else print B's value.
But that's not what my code says, Justin...:wink: If you look back at my last post, and examine the code in the "if" statement closely, you'll notice I have it saying if the a variable is *greater* than b, execute the code under that statement (namely print a's value...though that part of the code isn't all that important, since the "if" statement is false anyway, and so doesn't get read from). But of course, that is a false statement since a is actually *less* than b. So since the "if" statement is false, it goes to the first "else if" statement, and executes that instead. And then next, it reads from the second "else if" statement, which is the code that's supposed to give the correct output for the b variable when entered. And that is why I can't understand why its not giving the correct output when I type in the b variable...:wtf:
Getting back to your assumption last night, Visual Studio allows me to program in VB, C++, C#, or J#. The added on function in my posted code is probably specific to VS, autogenerated when I compiled the code I imagine.
Ahh...ok. So you actually *were* using the C++ language last night when you tested it then. :wink: I originally thought (yes, I guess "assumption" is the right word for it...:tongueout:) that you were trying it in VB, which is why I made that comment several posts ago.
Now back to your code. What you need to do is create a string variable and assign it the string returned from w/e the user entered and than use if statements to output the correct value depending on the variable the user entered, rather than comparing the values of the variable.
Well, that is what I thought I did already (i.e. declared the a and b variables as integers, defining the specific integers they are (namely 24 for a, and 29 for b) and then telling the program to give off their true integer values when the variables themselves are given in the user's input), but its apparently not working for some reason, and I really don't understand why...:S

Jake

EDIT: BTW, I tried using the "Else" statement in place of the first "Else if", but I kept getting error messages with that for some reason, and so I decided to stick with "Else if", which at least allows the program to open and give output when the user types in either of the two variables (though the output for b is of course incorrect...). I'm not sure if that's allowed or not, having an "Else if" statement right after an "If" statement...
Now that I think about it, it might be part of my problem.
 
Last edited:
This is a static case because you're using hard-coded values for your variables. If A is 24 and B is 29, than I believe B is greater than A or A is less than B (as you're stating... it can be done either way), don't you agree? Since B is always 29 and A is always 24 when it runs through the if statement "a < b", it is always true, so A is always the value outputted to the console and the else instructions never execute.

Last time I checked a math statement with "a < b" meant A is less than B. A is less than B by the time the you get to the if statement, so it returns true and else instructions are never executed.
 
This is a static case because you're using hard-coded values for your variables. If A is 24 and B is 29, than I believe B is greater than A or A is less than B (as you're stating... it can be done either way), don't you agree? Since B is always 29 and A is always 24 when it runs through the if statement "a < b", it is always true, so A is always the value outputted to the console and the else instructions never execute.

Last time I checked a math statement with "a < b" meant A is less than B. A is less than B by the time the you get to the if statement, so it returns true and else instructions are never executed.

But the whole point I'm making is in my code, I said "a > b" which last time *I* checked means "greater than"...:??
 
Ok, after review you were right on your "last" addendum. If you looked at citation in my post you would know I was talking about the first code sample. Now, it should be giving you 29 (B) for the output than, running the else instructions. So what problems are you having with it than?
 
Ok, after review you were right on your "last" addendum. If you looked at citation in my post you would know I was talking about the first code sample. Now, it should be giving you 29 (B) for the output than, running the else instructions. So what problems are you having with it than?
My whole thing is the output message is

You entered 'a' which equals: 24
regardless of which letter is typed in, i.e. a or b. So the output for when I type in a is correct, but it says the exact same thing it did for a when typing in b instead. :wink: I'm thinking the program is not reading from the second "else if" statement, which is where I tell it to display b's integer value when typing in that variable...

I will play around with it a bit more, and see if I find the answer to my problem.

Cheers,

Jake
 
Last edited:
As mentioned in [post=34701]post 21[/post] for getting the right letter, you'll need to determine what is typed in. Simply stating the cin statement won't help you. You need to actually assign what it returns into a string variable and than use an if else statement to check the string (rather than comparing the values of the variables themselves) and output the right message.
 
As mentioned in [post=34701]post 21[/post] for getting the right letter, you'll need to determine what is typed in.
I did that with the code
Code:
cin>> a;
cin>> b;
Simply stating the cin statement won't help you. You need to actually assign what it returns into a string variable and than use an if else statement to check the string (rather than comparing the values of the variables themselves) and output the right message.

"if else" or "else if"...? :wink: BTW, how would I go about "actually assigning what it returns into a string variable"? I thought I already did that with the code
Code:
if (a > b) {
    cout<<"You entered 'a' which equals: "<< a <<endl;
    }
    else if (a=24) {
        [B]cout<<"You entered 'a' which equals: "<<a <<endl;[/B]
        }
    else if (b=29) {
    [B]cout<<"You entered 'b' which equals: "<< b <<endl;[/B]
    }

? :huh: And "by comparing the values of the variables themselves" do you mean where I state
Code:
if (a > b)
?
I thought the whole point of the "if" statement is to is to change the flow of a program, by issuing statements that are either true or false. If true, then the program executes the code within the braces for that function, but if false, the program moves on to the "else" statement and runs the code for that function instead... :wink: So in theory, at least, what's actually stated in the "if" statement's condition doesn't matter, because it is just whether it is true or false that counts.

Please correct me though if I am wrong about that. :brows:

Jake

Addendum:

Ok, so I tried this code instead:
Code:
#include <iostream>

using namespace std;

int main()
{
    int a;
    int b;

    a = 4 * 6; // (Note use of comments and of semicolon) a is 24
    b = a + 5; // b equals the original value of a with five added to it

    cout<<"Please enter letter a or b: ";
    cin>> a;
    cin>> b;
    cin.get();
    if (a > b) {
    cout<<"You entered 'a' which equals: "<< a <<endl;
    }
    [B]else if (a = 4 * 6) {[/B]
        cout<<"You entered 'a' which equals: "<<a <<endl;
        }
    [B]else if (b = a + 5) {[/B]
    cout<<"You entered 'b' which equals: "<< b <<endl;
    }


}

so that in the "else if" statements, I give the exact same math problem I gave originally to determine the values of the a and b variables, thinking that maybe it has to match that, but no go. :x Still has the same problem of displaying a's output when entering b in the console.
 
Last edited:
Here's an index of C++ keywords I found btw. Probably good to know what they are and what they do to give you a better understanding of the language.

Yes, if statements are for flow control and validation/prevention of runtime errors.. To assign in the value to a string variable use:

string variable = value you want to give it;

I'd give you the specific code, but being a C++ noob myself I'm not sure how you'd do a string variable. I believe it would be something like this:

static strVariableName = "";

Than assign it the value you want, in this case, using the cin function to get the string the user types in and assigning it to the variable so it can be used with the if statement later:

strVariableName << cin();

Now your if statement would look something like the following:

Code:
if (strVariableName = "a") {
cout<<"You entered 'a' which equals: "<< a <<endl;
}
else if (strVariableName = "b") {
cout<<"You entered 'b' which equals: "<< b <<endl;
}
else {
cout<<"This program only accepts a or b as input"<<endl;
}
 
Here's an index of C++ keywords I found btw. Probably good to know what they are and what they do to give you a better understanding of the language.

Yes, if statements are for flow control and validation/prevention of runtime errors.. To assign in the value to a string variable use:

string variable = value you want to give it;

I'd give you the specific code, but being a C++ noob myself I'm not sure how you'd do a string variable. I believe it would be something like this:

static strVariableName = "";
Well, I tried that and it didn't work...threw me a error message for that line. :frowning: I used static a = "a";
I also tried other ones as well, such as static>>a>> = "a"; and static<<a<< = "a"; and other similar lines that came to mind, but none worked. Not sure if the "static" function even works inside an int main() function...
Found this tutorial which explains the use of the static function (though I haven't got a chance to actually read it all yet), but it sounds like it may be just for other things that I not even learned about yet. :wink:
Than assign it the value you want, in this case, using the cin function to get the string the user types in and assigning it to the variable so it can be used with the if statement later:

strVariableName << cin();
Tried using that function, but got an error saying "no match for call to 'std::istream) ()' " on that line, so that didn't work as well.
Now your if statement would look something like the following:

Code:
[B]if (strVariableName = "a") {[/B]
cout<<"You entered 'a' which equals: "<< a <<endl;
}
[B]else if (strVariableName = "b") {[/B]
cout<<"You entered 'b' which equals: "<< b <<endl;
}
else {
cout<<"This program only accepts a or b as input"<<endl;
}
The errors for the "if" and "else if" lines when I tried that suggestion said "invalid conversion from 'constant char*' to 'int' " for both. :wink:

Any other ideas? Thanks for the link to those keywords, btw. I'll be sure to check them out.

Jake
 
Last edited:
Well CG's here. Being a C# aholic I'm sure he knows the right way to do things...

I'll see you both later. Trying to avoid staying up too late so I can get things done tomorrow.
 
Thanks for the help :smile:

Addendum:

Ok...I think I figured out *mostly* what it needs to be. :wink:

With the following code for my program I only get errors on two lines, and those are on the "if" and "else if" lines...apparently because the (a="a") and the (b="b") are not valid conditions for the "if" and "else if" functions. But I haven't yet found the *legit* way to tell the program the variable being entered is the same variable, in integer form, that needs to be displayed (not even with "if" and "else if" statements).

Code:
#include <iostream>

using namespace std;

int main()
{
    static int a = 4 * 6; // (Note use of comments and of semicolon) a is 24
    static int b = a + 5; // b equals the original value of a with five added to it
    cout<<"Please enter letter 'a' or 'b': ";
    cin>>a;
    cin>>b;
    cin.get();
    if [B](a="a")[/B] {
    cout<<"You entered 'a' which equals: "<< a <<endl;
}
    else if [B](b="b")[/B] {
    cout<<"You entered 'b' which equals: "<< b <<endl;
}
    else {
cout<<"This program only accepts a or b as input"<<endl;
}


}
Addendum:

Tried something a bit different now...

Code:
#include <iostream>

using namespace std;

int main()
{
    static int a = 4 * 6; // (Note use of comments and of semicolon) a is 24
    static int b = a + 5; // b equals the original value of a with five added to it
    cout<<"Please enter letter 'a' or 'b': ";
    cin>>a;
    cin>>b;
    cin.get();
    if (cin>>a)
    cout<<"You entered 'a' which equals: "<< a <<endl;
    else (cin>>b);
    cout<<"You entered 'b' which equals: "<< b <<endl;
    
}
The program compiled correctly with that, but now I'm having the *exact opposite* problem. :brows: Its now saying "You entered 'b' which equals: 29" regardless of which letter I actually type in (whether a or b). :| I expected it to work perfectly too...heck, it *should* have worked perfectly!
There is no reason now why I should be having these issues, since the code is completely correct as far as I can tell.
 
Last edited:
I wasn't getting any errors either, but its ovbiously not correct. Its what us programmers like to call "logical errors" rather than syntax ones because the code as far as the compiler is concerned is correct.

Look up the help topics for your compiler/ide (integrated development enviornment - program you're building the program in) for how to set breakpoints if it has this functionality. Breakpoints tell it to stop the program and give you a chance to step through the rest of it line by line to see what values the variables may have or what the code is actually doing.

If cin.get() is the method (rather than just cin() like I had stated yesterday) used to get what the user types in, use that when specifying a string variable so you can compare it. cin() on its own would be an object rather than a property I guess which would explain why it wasn't working:

static inputstring = cin.get();
if (inputstring="a") {
cout<<"You entered 'a' which equals: "<< a <<endl;
} else if inputstring="b") {
cout<<"You entered 'b' which equals: "<< b <<endl;
} else {
cout<<"This program only accepts 'a' or 'b' as input"<<endl;
}
 
I wasn't getting any errors either, but its ovbiously not correct. Its what us programmers like to call "logical errors" rather than syntax ones because the code as far as the compiler is concerned is correct.

Look up the help topics for your compiler/ide (integrated development enviornment - program you're building the program in) for how to set breakpoints if it has this functionality. Breakpoints tell it to stop the program and give you a chance to step through the rest of it line by line to see what values the variables may have or what the code is actually doing.
Yeah, I learned about breakpoints yesterday. :smile: I accidentally hit F5, which put a breakpoint in the line the cursor was in, and then I had to figure out what I did to do that. :??
If cin.get() is the method (rather than just cin() like I had stated yesterday) used to get what the user types in, use that when specifying a string variable so you can compare it. cin() on its own would be an object rather than a property I guess which would explain why it wasn't working:

static inputstring = cin.get();
if (inputstring="a") {
cout<<"You entered 'a' which equals: "<< a <<endl;
} else if inputstring="b") {
cout<<"You entered 'b' which equals: "<< b <<endl;
} else {
cout<<"This program only accepts 'a' or 'b' as input"<<endl;
}
Well, I understand what you're saying (I think...), but I tried the following code, and what its doing now is the console opens with no text displayed, and then if I then hit a key, the program displays the message and goes on from there. :wtf: Why that is, I don't know (maybe the *order* of the statements in the code is wrong?)...

#include <iostream>

using namespace std;

int main()
{
static int a = 4 * 6; // (Note use of comments and of semicolon) a is 24
static int b = a + 5; // b equals the original value of a with five added to it
static int inputstring = cin.get(); // inputstring equals the input by the user, which is in this case, 'a' or 'b'
cout<<"Please enter letter 'a' or 'b': ";
cin>>a;
cin>>b;
cin.get();
if (inputstring = a) {
cout<<"You entered 'a' which equals: "<< a <<endl;
}
else if (inputstring = b) {
cout<<"You entered 'b' which equals: "<< b <<endl;
}
else {
cout<<"This program only accepts 'a' or 'b' as input"<<endl;
}

}
Btw, I tried using what you suggested in the "if" and "else if" statements (i.e. if (inputstring = "a") { ), but kept getting errors saying "invalid conversion from 'const char*' to 'int' " in those lines, so I then just went with if (inputstring = a) { which works fine. Apparently quotes don't work in that form of statement. Also, the original problem of the same output being displayed no matter which variable is given in the input still remains...

Jake
 
Last edited:
You're beginning to see the importance of variables with different types of content and how problems may occur if you try to convert one type to another that cannot be done. Don't put the int keyword for the inputstring variable at declaration because it is holding a string (actually the compiler says char since you... the user, typed in only a or b as input, which could also be considered a variable of type char/character). You also don't need cin >> a; or cin >> b;. Those lines do nothing in regards to reading the input and saving it for analysis with your if statement.

In addition, you don't get the prompt to enter information until after enter because the code goes in sequential order and you've got the prompt output under an instruction to get input from the user.

So what is should look like than:

#include <iostream>

using namespace std;

int main()
{
static int a = 4 * 6; // (Note use of comments and of semicolon) a is 24
static int b = a + 5; // b equals the original value of a with five added to it
cout<<"Please enter letter 'a' or 'b': ";
static inputstring = cin.get(); // imputstring equals the imput by the user, which is in this case, 'a' or 'b'
if (inputstring="a") {
cout<<"You entered 'a' which equals: "<< a <<endl;
} else if inputstring="b") {
cout<<"You entered 'b' which equals: "<< b <<endl;
} else {
cout<<"This program only accepts 'a' or 'b' as input"<<endl;
}
}
 
Never mind...I figured it out. :smile: That other problem I just mentioned (i.e. the one with the program not displaying any output at first) turned out to to be indeed because of the order of the statements. :wink: It turns out I needed to put the line

static int inputstring = cin.get(); // inputstring equals the ibput by the user, which is in this case, 'a' or 'b'
right after cin.get() in the code, and right before if (inputstring = a) {...
Once I did that, and put it there instead of before the other two "static int" statements, like I originally had it, the program displayed the output "Please enter letter 'a' or 'b':" right when the console was opened, and without having to press a key first.

But I'm still having the original problem though. :frowning: The program is now giving a's output no matter which variable is given in the input.

EDIT: Just noticed your post...

Addendum:

You're beginning to see the importance of variables with different types of content and how problems may occur if you try to convert one type to another that cannot be done. Don't put the int keyword for the inputstring variable at declaration because it is holding a string (actually the compiler says char since you... the user, typed in only a or b as input, which could also be considered a variable of type char/character).
Well, that thought came to my mind already, and I tried using the "char" type of variable, instead of "int" but since it didn't work, I went back to using "int" like I was before. :smile:
You also don't need cin >> a; or cin >> b;. Those lines do nothing in regards to reading the input and saving it for analysis with your if statement.
Ok...so I deleted that part of the code (including the "cin.get()" statement which follows those two lines), so what I have now is

#include <iostream>

using namespace std;

int main()
{
static int a = 4 * 6; // (Note use of comments and of semicolon) a is 24
static int b = a + 5; // b equals the original value of a with five added to it
cout<<"Please enter letter 'a' or 'b': ";
static inputstring = cin.get(); // inputstring equals the ibput by the user, which is in this case, 'a' or 'b'
if (inputstring = a) {
cout<<"You entered 'a' which equals: "<< a <<endl;
}
else if (inputstring = b) {
cout<<"You entered 'b' which equals: "<< b <<endl;
}
else {
cout<<"This program only accepts 'a' or 'b' as input"<<endl;
}

}
but the program's not accepting no variable type entered in the "static" statement (I had that problem before too, which is why I used "int" in the static statement, though you advise not to). It gives me the error "ISO C++ forbids declaration of 'inputstring' with no type" in the line that says static inputstring = cin.get(); // inputstring equals the ibput by the user, which is in this case, 'a' or 'b' so obviously it doesn't accept not having any variable type given in the "static" statement...:wink:
In addition, you don't get the prompt to enter information until after enter because the code goes in sequential order and you've got the prompt output under an instruction to get input from the user.

So what is should look like than:
Right. I fixed that part of the code now, and so that problem is fixed, though the original problem still remains...
I'll now go over your code in detail to see if there's something I'm missing (which obviously there is, since the program's not working...:brows:smile:.

Addendum:

Ok, so I tried using the code I just mentioned in my last post, only changing the static inputstring = cin.get(); line to static char inputstring = cin.get(); and that works, no errors. But the original problem still remains, as always...:frowning:

So this is what my code is now:

#include <iostream>

using namespace std;

int main()
{
static int a = 4 * 6; // (Note use of comments and of semicolon) a is 24
static int b = a + 5; // b equals the original value of a with five added to it
cout<<"Please enter letter 'a' or 'b': ";
static char inputstring = cin.get(); // inputstring equals the ibput by the user, which is in this case, 'a' or 'b'
if (inputstring = a) {
cout<<"You entered 'a' which equals: "<< a <<endl;
}
else if (inputstring = b) {
cout<<"You entered 'b' which equals: "<< b <<endl;
}
else {
cout<<"This program only accepts 'a' or 'b' as input"<<endl;
}

}


Addendum:

I also forgot to mention I can enter *any* letter (not just a or b), and the program will give out a's output...:wtf:
 
Last edited:
You're forgetting the ""s in your if statements for a and b. Those are hard-coded values rather than the names of variables or functions, so they must be encapsulated in ""s.

if (inputstring = "a") { ...
else if (inputstring = "b") {...
 
You're forgetting the ""s in your if statements for a and b. Those are hard-coded values rather than the names of variables or functions, so they must be encapsulated in ""s.

if (inputstring = "a") { ...
else if (inputstring = "b") {...

Right...and as I stated in one of my above posts, that doesn't work (it wont even compile). It throws me an invalid conversion from 'const char*' to 'char' error message on those lines.

BTW, to keep everything neat here (and to prevent people from having to scroll up and down to read various posts to figure out what the current code is), this is what my code says now:

#include <iostream>

using namespace std;

int main()
{
static int a = 4 * 6; // (Note use of comments and of semicolon) a is 24
static int b = a + 5; // b equals the original value of a with five added to it
cout<<"Please enter letter 'a' or 'b': ";
static char inputstring = cin.get(); // inputstring equals the ibput by the user, which is in this case, 'a' or 'b'
if (inputstring = "a") {
cout<<"You entered 'a' which equals: "<< a <<endl;
}
else if (inputstring = "b") {
cout<<"You entered 'b' which equals: "<< b <<endl;
}
else {
cout<<"This program only accepts 'a' or 'b' as input"<<endl;
}

}
That's the code I just tried, after your suggestion, but got those error messages I just mentioned on the lines I marked in bold.
 
Last edited:
Try putting the const keyword after static in the inputstring variable declaration with ""s still in your if statements:

static const char inputstring = cin.get();

Doing so should make inputstring a constant char, which should match up with the if statement and at least if anything prevent that error.
 
Try putting the const keyword after static in the inputstring variable declaration with ""s still in your if statements:

static const char inputstring = cin.get();

Doing so should make inputstring a constant char, which should match up with the if statement and at least if anything prevent that error.

It didn't work. :frowning: I still got that conversion error...

Well, take a look at my current code. I'll break it all down, while I'm posting, so I can get a better understanding of what is actually going on.

#include <iostream>

using namespace std;

int main()
{
static int a = 4 * 6; // (Note use of comments and of semicolon) a is 24
static int b = a + 5; // b equals original value of a with five added to it
cout<<"Please enter letter 'a' or 'b': ";
static char inputstring = cin.get(); // inputstring equals the ibput by the user, which is in this case, 'a' or 'b'
if (inputstring = a) {
cout<<"You entered 'a' which equals: "<< a <<endl;
}
else if (inputstring = b) {
cout<<"You entered 'b' which equals: "<< b <<endl;
}
else {
cout<<"This program only accepts 'a' or 'b' as input"<<endl;
}

}
Ok, so the first line of the int main() function (not counting the "{" or commented out sections of course) is static int a = 4 * 6;
This line of course declares "a" as a permanently stored variable, of the "int" (integer) type, and defines the value of the "a" variable as 4 * 6 which of course equals 24.

The next line is static int b = a + 5;
This of course performs the same type of operation done on the "a" letter, i.e. declaring b as an integer, and defining its real value (which equals 29).

The next line cout<<"Please enter letter 'a' or 'b': "; which follows it uses the "cout" keyword to display the output indicated by the "<<" and " "" " operators, and ends the line with a semicolon. The output as one can see by simply looking at the middle of it is "Please enter letter 'a' or 'b:" without the quotes of course when it is actually being shown in the output of the compiled program.

The next line static char inputstring = cin.get(); declares 'inputstring' as a variable of "char" type which can store a single character (though to be honest, I'm not sure what they mean by "character"...whether that means the input can only be a single character when using that type of variable, or if it simple means there can only be one value given in the input), and defines the variable 'inputstring' as the function cin.get() which reads in input by the user, and uses it to perform whatever functions follow.

The next line in the code if (inputstring = a) uses the "if" keyword to make a simple test. If the condition given in the "if" statement is found to be true, then the code after it is executed. But, if on the other hand, the the condition is found to be false, it goes to the "else" statement, or in this case the "else if" statement as that is before the "else" statement. Ok, so what (inputstring = a) does is basically make the statement that the variable 'inputstring' (which was already defined as being equals to the cin.get() function, which reads in input by the user) equals the variable 'a' which in turn was pre-defined as having the value of 24.
So, what its saying is cin.get () = 24. Now, what determines whether that is true or not (in *theory* at least) is what follows the "if" statement.

So the next line in the program is cout<<"You entered 'a' which equals: "<< a <<endl;
Now, what that line does is tells the program to give off the output of "You entered 'a' which equals: " when the variable equaling 24 (namely a) is given in the user's input. So, what should happen is when the "if" statement is found to be true (i.e. when 'a' is entered), it returns a's ouput, already mentioned. But, if on the other hand the statement is found to be false (such as in the case that the user enters a variable that amounts to 29, a, instead of 24, b) the program skips the code under the "if" statement and goes to "else if" instead.

So the following line is of course else if (inputstring = b)
Now, "else if", from what I understand, is an alternative to an "else" statement, which in turn is given in case the "if" statement is found to be *false*, the program can continue and not halt. Also, from what I've read, its possible to have multiple "else if" statements, but not multiple "else" statements. And, it also seems that the better way to go when using "else if" statement and "else" statements together, is to have the "else" statement following the "else if" statement(s). (If I have that wrong, then please correct me...:wink:)
Ok, so what line does is it gives another statement, basically stating that the cin.get() function equals the 'b' variable, or the value of 29. Now, of course in order for that statement to be true, it needs to be matched with the value of 'b' in the input given by the user...other wise the program moves on to the "else" statement. And if found to be true, it of course executes the code contained within the braces of that statement:

Code:
cout<<"You entered 'b' which equals: "<< b <<endl;
Now what should happen is when the value of 'b' is given in the user's input, the program returns the output "You entered 'b' which equals: 29".
and ignores both the "else if" and the "else" statements, as the "if" statement was found to be true.

Of course, if the "else if" statement was in turn found to be *false*, then the program moves on to the "else" statement, and executes that code instead. So the lines that get read next are:

Code:
else {
        cout<<"This program only accepts 'a' or 'b' as input"<<endl;
    }
which tells the program (in theory) that if the input by the user does not match either the value of 'a' or 'b', it'll return a message saying "This program only accepts 'a' or 'b' as input".
So in the case of the "else if" statement being *true* (as in the input by the user matches the statement of "else if" by the 'b' variable being given by the user) then the "else" statement would be ignored, and the program would just read from the "else if" statement of course, and execute the code contained within the braces of that statement.

Now, all of that is well and good, except...I can't figure out why its not working then! :wtf: Everything I have just shown indicates that the code is correct, and should do exactly as I expect it to (i.e. display the correct output for either declared variable entered), but for some reason, its not doing it...

Jake

Addendum:

All of this over what *should* have been just a simple program to write...:glare:
 
Last edited:
Back
Top