C++ newbie

I'm new to C++(no duh right?). Just wondering if there's a way to run the same code multiple times but have it act differently on each run around?

For instance:

1
2
3
4
5
 if((y==N)||(y==n))
{
cout << "This is correct!" << '\n';
cout << "This is not correct! << '\n';
} 



So is there a way to make only the first cout display the first run around but make only the second cout display in the second run. I'd imagine it would involve using a for loop but I just need to know the method.

Also I use codeblocks but I've noticed that in tutorials using M$ visual studio that things seem to be done slightly different despite being the same programing language. Like some bits of code are needed that to do certain things like keep the console window open that codeblocks can do without the extra code. Is that normal? Should I just ignore that and carry on?
Last edited on
In programming, mostly rely on flow-control.
Why you want the same code runs different output? It's none of my sense can imagine.

> "make only the first cout display the first run around but make only the second cout display in the second run"
Is that mean you'll close program and re-run it for another result?
If it's,maybe you should use something(on OS or File system, not only your program) to remember what's your program did in last running.
Btw, you still have to use a condition(if statement) in your program.
Last edited on
You could put the code in a loop, as you said, and then have a variable that change value from the first iteration to the next and then have an if statement testing this variable to decide which message to display.
Like some bits of code are needed that to do certain things like keep the console window open that codeblocks can do without the extra code. Is that normal? Should I just ignore that and carry on?

This is a matter of setting your tool chain up correctly. In MSVS (why do you feel the need to make one of those a dollar sign?) you need to make sure the project is set up to use the console subsystem. After all, if it's not intended to be a console program, why would the console need to remain open?

http://s2.postimg.org/r7sabcl09/subsystem.png

And, of course, you'll need to run it without debugging for the console to stay open. You're expected to control the flow of the program yourself when debugging.
"You could put the code in a loop, as you said, and then have a variable that change value from the first iteration to the next and then have an if statement testing this variable to decide which message to display."

I had theorized such a method but an example would help greatly.

"(why do you feel the need to make one of those a dollar sign?)"

I assumed anyone that knows anything about computers would know that. M$ has been used as a short for Microsoft for years because M$ is known to care more about money than people(note how it's now the only paid for OS and still going for big bux). Also Bill Gates didn't become the richest man in the world by giving people good deals.

I assumed anyone that knows anything about computers would know that. M$ has been used as a short for Microsoft for years because M$ is known to care more about money than people(note how it's now the only paid for OS and still going for big bux).

Yes, I know what it signifies. I don't know why you feel the need to inject your politics into the beginner's forum of a programming site.
I assumed anyone that knows anything about computers would know that. M$ has been used as a short for Microsoft for years


Yeah... if you're this guy:
http://www.penny-arcade.com/comic/2002/07/22/m

because M$ is known to care more about money than people


Do you have any examples to back up this claim? Or are you just talking out of your ass?

Microsoft is actually one of the more ethical big corporations. Bill Gates has donated extremely large sums of money to charity more than once, and MS keeps lots of jobs in the USA instead of outsourcing them overseas where labor is cheaper.

Also... lots of their commercial software has free versions available (Visual Studio, Word, Excel, etc)

I'm a pretty left-leaning anti-corporate guy. But MS gets a bad rap. They're not really all that evil -- especially when stacked against any of their peers (*cough*Apple*cough*).
Fine then, screw the politics. Can anyone give an example to help with the primary question I asked?
Also Bill Gates didn't become the richest man in the world by giving people good deals


I have a lot of respect for Bill Gates, people give him a lot of stick yet all that wealth you refer to he constantly ploughs back into charity - look up the Gates foundation. He has a good business head, and was in the right place at the right time... he got there through talent and hard work.

Anyway, back to topic..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

#include <iostream>

int main()
{

	int status = 0;

	while (status <= 5)
	{
		switch (status++)
		{
			case 1:
				std::cout << "Status 1" << std::endl;
			break;
			case 2:
				std::cout << "Status 2" << std::endl;
			break;
			case 3:
				std::cout << "Status 3" << std::endl;
			break;
			case 4:
				std::cout << "Status 4" << std::endl;
			break;
			case 5:
				std::cout << "Status 5" << std::endl;
			break;
		}
	}

	return 0;
}
Topic archived. No new replies allowed.