Millions of Pesky Little Problems

As said in the title above ^^ I have got loads of problems I cant figure out or solve.

Okay, problem one. I have got no idea how you are supposed to use the scope resolution operator (::), I DO realize you use it for declaring that you're using std x, but what else do you use the scope resolution operator for? That was problem one :).

Problem two. What is a global variable, yes its a definition question :)

Problem 3. In the book I am learning from it says that you can use any or ALL operators in a condition, but the expr ? expr:expr is a condition, but how the HECK am I supposed to use it against a CONDITION?? Yep...

4:
int a=(n * (n + 1) / 2 >= k;
int b=((n + 1) * (n + 2) / 2 <=k;
if (a && b) {
execute this:
}

How is that supposed to evaluate? I found that line of code in another example in my book. I mean, if you are setting the int like that, won't it mean that you are testing the >= for no particular reason? As you are testing it in a non-condition, which just means it is testing it for no reason and it's just a waste. And then you are basically testing a MATHEMATIC EQUATION/ANSWER in the condition? What the heck is the point of that? That's not a condition... That's an expression! :3

Problem 5, level: hard. How am I supposed to set a variable's data into a new variable name with it's own data? Hence:
int test=5
test=test * 2

So what it is supposed to do is declare the variable test, then make a new variable named 5 and set the data to 10. What is it I am supposed to do???

Problem 5 (you are nearly there!):
Okay, I know that you declare a variable unsigned to make it so that variables will only be positive, but how do you declare a variable to be only NEGATIVE? Signed does not work, it is basically the same thing as positive + negative. So yeah...

Problem 6:
Okay, this is one of those very basic very crappy questions: If I just type in:
int myInt;

What will happen? Will it set myInt to 0, or basically nothing? Cause I want to do this next:
myInt+=1

What will happen? COMPLETELY NOTHING PLUS ONE EQUALS ONE, but it might be different in C++?

Yay, another crappy question! if I do:
if (myInt++==2) {
execute this shit)
}

Will it actually increment myInt, or just test the incremented version of myInt against the condition? Yeah...

Next Problem: If I just type in 'myTest=hi', without telling the computer what kind of variable I am declaring, then what will happen? Will it set it to string, char, int, bool, or WHAT??

Okay, we can do it! Sometimes when you are doing {}, you HAVE to add ; at the end of }! WHY? We don't need to do that for MOST of the }s!

Yeah, another question :( What happens if set a variable, say, myVariable, into TWO TYPES? Such as:
int myVariable=123;
string myVariable=string;

What happens? Will it override the int or what??

Next: Are we allowed to use the short and long prefixes on ALL variables? I learnt that they can be used on int, but in a recent example I noticed it can be used on FLOAT and DOUBLE as well!

Uh-huh next question: How do we make one line of code set two variables at a same time? Such as:
a= x > y ? x:y

I want that code to operate so that if x is bigger than y, then it will set a to x, but at the same time it will also set b to y. Comprehende? Is that possible? :)

We're 80% through! This is just a make-sure. If you put the increment operator as a suffix, then what it will do is execute the increment operator at the END of the line, right? But if you put the operator as a PREfix, then it will execute at first, right?

Another problem; I know that to display a string to the user is to type in:
cout << myString.c_str();

But are there any other things in C++ that need the '.c_str()'? For one that I realize that cout needs this to display, but what about any other statements? Yeah…


NEW SECTION: THE HARDCORE PART. If I make x this:
string x= cout << "hello friend";;

[i]And then I place the word x smack in the middle of nowhere in the code, will it execute the cout? Or if not, is there any other trick that can do this? What are the exact functions and differences between string and char? I have got NO IDEA which one to use at times… This also applies to other variables such as float and double (Wats the differences?).

Is there such thing as an… inactive variable? A variable that doesn't evaluate? Example:
int myInt= abc + 2 /4;

But then it DOESNT ACTUALLY EVALUATE myInt? It just sets it like that so I can set it in another statement later? Or can I do something like this?:
string evaluate=2+2*applez;
int myInt=evaluate;

What will happen then? Will it evaluate the string, or will it just save that into memory and set myInt into 2+2*applez? It is seriously confusing me, and I have got no idea what happens.
This question is similar to the one above. If I set a mathematical equation into a string, what happens? Does it evaluate it, or does it save it into memory? I have got no freaking idea since string isn't meant to solve equations.

Yeah, so the main point is… Is there such thing as an INACTIVE variable???

And something else, what exactly is a string? Something that stores text, that's all I know :P

Okay, final question. This is a quote from the book I am learning from:
If you have a long or multiline conditional statement, try breaking it into more than one statement.

Yea, I have got no idea how you use MORE THAN ONE LINE in a conditional statement, yet break something into more than one statement.


LAST QUESTION: WHAT IS A STATEMENT. I USED TO THINK IT WAS JUST SOMETHING LIKE COUT AND CIN, BUT UNFORTUNATELY IT ISNT! HELP

Thanks for helping! If anyone cant understand any of my questions, please tell me, and I will restate them! Thanks!

PS Sorry for the essay-long need-help document, I am a newbie at C++ :(
PS Sorry for the essay-long need-help document

Me too. Your style is annoying and too verbose. I suggest you'd try again, by using short questions put in lists (example):

1) How do I...
2) Why does...
3) What does...

Now let's see your post again...
1
2
int test=5
test=test * 2

You seem to be looking for the += -= /= and *= operators.
1
2
int test=5; // remember the semicolon
test *= 2


Okay, we can do it! Sometimes when you are doing {}, you HAVE to add ; at the end of }! WHY? We don't need to do that for MOST of the }s!

Why? Because. Learn the rules and go on. For blocks of code, you don't need semicolon, for blocks of data structure/class definition, you do. That said you can always put semicolon if you wanted to but that's bad style.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main()
{
	int i=0;

	while (i++ != 666)
	{
		if (i % 2 == 0)
		{
			std::cout << "Yay!\n";
		};
	};

	{
		{
			{
				std::cout << std::endl;
			};
		};
	};
} // <-- "pedantic" warning issued here 


But are there any other things in C++ that need the '.c_str()'? For one that I realize that cout needs this to display, but what about any other statements? Yeah…

What's that delicious smell, troll food? Yes it is.
First of all, you don't need to use c_str() to display std::strings (the operator<< is already overloaded). Secondly, you needed to use c_str() is passing the std::string as a filename to a file stream constructor - but this isn't the case anymore for C++11.

Okay, this is one of those very basic very crappy questions: If I just type in:
int myInt;

What will happen? Will it set myInt to 0, or basically nothing? Cause I want to do this next:
myInt+=1

Depends on the context. If it's a local (aka "auto") variable, it will probably contain a junk value. If it's a global/static variable, it will be set to whatever the default value is.

RTFT: http://www.cplusplus.com/doc/tutorial/
Topic archived. No new replies allowed.