Best programming language?

If you had this code and it didnt work than thats fine but let me post it in case you missed something:

#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 const char 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;
}
}


Addendum:


I think i'll be getting the compiler your using to have a go at it myself when I get the chance. Right now I've really been trying to help you based off knowledge alone that I already have programming in VB, though both of us trying to learn C++ at the same time isn't helping.

When times get tough, browse through a couple of code examples on the net to see how stuff is done. I've been doing that quite often lately...
 
Last edited:
Ok, so I tried this code (you left out the '(' in the "else if" line, and didn't have spaces between the = signs and the variables, which I wasn't sure would work, so I couldn't use your exact code for sure):

#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 const char 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;
}
}

and that returned me the following errors:

In the if (inputstring = "a") { line:

  • assignment of read-only variable 'inputstring'
  • invalid conversion from 'const char*' to 'char'

In the else if (inputstring = "b") { line:


  • assignment of read-only variable 'inputstring'
  • invalid conversion from 'const char*' to 'char'

so yes, that does not work either. :frowning:

Addendum:

Addendum:


I think i'll be getting the compiler your using to have a go at it myself when I get the chance. Right now I've really been trying to help you based off knowledge alone that I already have programming in VB, though both of us trying to learn C++ at the same time isn't helping.

When times get tough, browse through a couple of code examples on the net to see how stuff is done. I've been doing that quite often lately...

Alright...yes, that would probably be the best thing to do, just so we're on the same page here, and you can see firsthand what I'm experiencing. :wink:
The compiler I'm using is Code::Blocks. The one I downloaded is "codeblocks-802mingw-setup.exe" for Windows 2000/XP/Vista.
And then I simply followed these instructions to install it.
 
Last edited:
In the meantime they seem to have a decent forum. I understand CG's lack on involvement here but like I said us trying to learn C++ together isn't going all too well. There's bound to be someone there that could help you...

I've got to go, good night.
 
Code:
#include <iostream>

using namespace std;

int main()
{
	int a = 4 * 6; // (Note use of comments and of semicolon) a is 24
	int b = a + 5; // b equals the original value of a with five added to it
	cout<<"Please enter letter 'a' or 'b': ";
	char inputstring; // imputstring equals the imput by the user, which is in this case, 'a' or 'b'
	cin >> inputstring;
	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;
	}
}

Characters are marked with ' and not "
Using "=" means "assign the value"
Using "==" means "compare the value with"

Your old if statement had "=" which forced the value to be 'a' regardless of its initial value.
 
= assigns values even in if statements? Anytime I've ever seen == used before it meant a non case-sensitive checking of the condition. No wonder we weren't getting anywhere.

Edit: Cool, here's a tutorial on C++. Looking through the operator section alone to confirm CG's post I was quite amazed by the crazy stuff you can do with C++.
 
Last edited:
Characters are marked with ' and not "
Using "=" means "assign the value"
Using "==" means "compare the value with"

Your old if statement had "=" which forced the value to be 'a' regardless of its initial value.

AWESOME! :joy: It works! I knew it had to be something really simple like that I was missing, but I couldn't figure out what it was. Thanks a bunch, CG. The program now works perfectly, and the way it was originally intended. :grinning:
Thanks for the tut, Justin. That will probably help me a lot with understanding the language.

Jake

Addendum:

For all involved, the code I eventually ended up using (which works the way intended) 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 char 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;
}

}
Though I could have used CG's exact code, which would also give the correct output when a certain character is given in the user's input, I prefer to use the above code, so as to have the variables 'a', 'b', and 'inputstring' as *permanently stored* variables. Now, with a bit more of playing around, I may actually be able to design a program that is interesting. :??
 
Last edited:
Static is great when you need to keep the variables throughout the life of the program. I put it into the code above though it didn't matter because its a really small program anyway. But when you move to bigger programs though, it is important to be efficent with use of the system memory so if the variables are only to be used one time for that function when the function is ran than static isn't needed.
 
Static is great when you need to keep the variables throughout the life of the program. I put it into the code above though it didn't matter because its a really small program anyway. But when you move to bigger programs though, it is important to be efficent with use of the system memory so if the variables are only to be used one time for that function when the function is ran than static isn't needed.

True...good point. :smile: But I was thinking about maybe expanding on the program, and so the variables may get used more than one time. :tongueout:
 
Last edited:
using static and global variables is considered to be bad object-oriented programming.

Huh? :wtf: First of all, what is "object-oriented programming" and why is it considered bad practice to use static variables? And if that's the case, then why have static in the first place?
 
Last edited:
Object Oriented programming is the "coding style" used in most modern applications.

It refers to creating models of objects in your code called "classes" and tying them together to build a system.

For instance, to make a flash-card learning game, you'd make a "class" called "card," "question," "answer," and tie them together in a class called "game."
 
Object Oriented programming is the "coding style" used in most modern applications.

It refers to creating models of objects in your code called "classes" and tying them together to build a system.

For instance, to make a flash-card learning game, you'd make a "class" called "card," "question," "answer," and tie them together in a class called "game."

Ok, I guessed as much. I just wanted you to expand on why it is bad practice to use static variables in object oriented programming...? :wink:
 
They're best practices. Static is still there if you need it, but shouldn't be used unless you do because it doesn't go away at the end of its running code block's scope. In other words, it stays in memory after the function has finished, which is bad thing if you only need it when the function is called and at its default value.

Your already taking advantage of objects whether you realize it or not. The .get method is part of the cin object/class for example.

While its not required, you should read up on best programming practices and try to stick to them. At first I thought it was pretty silly myself, but its made programming a lot easier when I can look at a variable or object and instantly know its type just by three letter prefixses added onto the names or create a blueprint class for my application and use it multiple times throughout.
 
Ok...alright. I'll stick with CG's original code then. :grinning: But you are after all the one who first pointed out use of "static" to me...and now you're suggesting I don't use it for this program? :brows:
 
Well at first I thought it was the only way to use a string variable with C++, but you got the keyword string for that and I realized what your code was doing (taking the input and storing its character code). C++ is a little confusing cause some keywords have more than one purpose. Looking through the c++ keywords link I gave showed me static is used the same way in C++ as it is in VB.
 
Last edited:
Does your ide allow you to click on the errors to jump straight to the lines containing them? You could alos use breakpoints to see where things are going wrong.
 
Hello..
This is a difficult question to answer generically and generally depends on the types of application that you would like to create. If you would like to simply create online pages, you should definitely start with these languages, in this order: HTML, CSS, and JavaScript. The two best options for High-Level Programming are Java and C++. So all the best to you.
Thanks Matthew. :smile:
I've actually already started learning all that now. HTML is a piece of cake compared to C++...:brows:
I've been learning a bit of everything here and there, now that I've got a website.

Cheers.

Jake
 
Back
Top