I use Visual Studio 2010 Ultimate. After fixing one of my problems, I ran into another. I have 2 files in my project named, let's say, A and B. The problem is when I try to run the code from B, VS outputs code from A. Any ideas on how to fix this?

Help!

Please give more information.
What is A and B? What do they do? briefly.
Vinayak

Member Avatar for JSPMA1988

Are you building and running the entire project instead of just a single file?

A and B are both codes. I'm learning C++. I don't know where the build and run button is so, I don't know if I'm building and running the entire project but I press F5 to run my codes. A calculates the moon's weight by dividing the Earth's weight and B uses the "else" to output a line if one number(inputted by the user) is greater than another. But, when I try to run B, Visual studio runs the code from A.
And how do I run the code from just a single file?

since you haven't specified much, i will shoot in the dark
your A has the main() function

note that F5 in Visual Studio builds the project(if it is not up to date) and runs it. It is not for a single file.

While Visual Studio is the best(according to me), since you are learning C++ try out codeblocks as it has much simpler interface, and you can without trouble run single files. Do also remember that VS is not difficult, but since you don't seem at ease with it, go for codeblocks for now.

www.codeblocks.org

Vinayak

I have tried Code::Blocks, but I got the toolchain something error so I couldn't compile and run at all; I've also tried Dev-C++ but I didn't feel comfortable with it. Visual studio was working fine before. Yes, my A does have the main() function but after I put main() function in B as well, I got the "fatal error LNK1169: one or more multiply defined symbols found" error so I renamed the main() function in B to n_main(). Can I or can I not have 2 .cpp files in 1 project and can I not run them individually. Here is my code for A:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	double a, b;
	int c= 0;

	for (a = 1.0; a <= 100; a++)
	{
		b = a/17;
		cout << "If the Earth is " << a << " pounds, the moon is " << b << " pounds." << endl;
			 
	}

	c++;

	if (c <= 25)
	{
		cout << "/n";
		c == 0;
	}

	system ("PAUSE");
	return 0;

}

Code for B:

#include "stdafx.h"
#include <iostream>

using namespace std;

int n_main() 
{
	double a, b, c;
	c = a + b;

	cout << "Enter Argument 1: ";
	cin >> a;

	cout << "/n";

	cout << "Enter Argument 2: ";
	cin >> b;

	cout << "/n";

	if (a > b) 
	{
		cout << "Argument 1 is greater than Argument 2.";
	}
	else
	{
		cout << "Argument 2 is not greater than Argument 2";
	}

	system ("PAUSE");
	return 0;


}

So when I try to run code B, VS runs code A. Any ideas why and how I could fix it?

It is possible to have more than 1 *.cpp file in a project. In fact, for most projects, you'll want more than one. However, it looks like you have 2 different assignments/projects here. If you right-click the file for the assignment/project you don't want and choose "Exclude from project" that should clear up the issue. It will take the offending file out of the project, but not erase/delete it. Then, it will no longer be compiled as part of your build/debug operations.

@Fbody
But I don't want to exclude them from project. I want them both. I use one for learning, while the other .cpp file is used for refreshing my memory of what I've just learned. Code A and B are in the same project. I just named my files A and B here (this website)so I and/you wouldn't have to type the files' names again because they are long.

I wouldn't normally do this because I don't like all the extra files floating around in my project(s), but try this:

  1. Right-Click the file you don't want compiled/executed and select "Properties".
  2. On the Left, locate the property group Configuration Properties -> General (it will probably be highlighted by default).
  3. On the Right, locate the option "Excluded from build".
  4. Set the option to "Yes".
  5. Click "OK".
  6. Rebuild the project.
  7. Execute the program.

(This is how it works in VS 2008, I don't know if it's the same in 2010)

I didn't realize there was that option -- I normally just remove the file from the project.

@Fbody
Your tip worked but it would be such a pain to have to do that again and again just to run some code from different files. Why can't I just select the files I want to compile and run and do it? Do you use VS; if so how does yours work?

Also I got a "fatal error LNK1120: 1 unresolved externals". After a bit of googling, I found out that it could be fixed but doing this:

int main(void)

If I were to create another file within that project, could I do name my function that again(name it int main(void))?
Why can't I name it something like n_main?

Member Avatar for JSPMA1988

F5, by the way, builds and then goes into debug on VS 2010 Ultimate. You can also build and run single files. Your B file won't execute because of n_main(), which isn't a legal main() function in a file. It will always run file A because of the normal main().

I didn't realize there was that option -- I normally just remove the file from the project.

Normally, I would too, but the OP doesn't want to do that.

@OP:
I do use VS. Specifically, I use VS 2008. The process I described is how it works.

Another method I've used, that works fairly well, is to create a "Solution" for your class. Then, within the organizational structure of the solution, create a "project" for each of your assignments. You can then use the solution settings to tell VS which projects to build and/or run when you build the solution. The process for this would be similar to the previous process I described, but you start by right-clicking the solution instead of a specific file.

@Fbody
I am sorry to bother you with all these questions but exactly how do I do what you described? I am not familiar with VS's Interface. Do I have to right-click my solution in the Solution explorer or what?

@JSPMA1988
How do I build and run just one file?

Am I the only one having this problem?

Although you dont want to separate these files, which are not related to each other.
I will still suggest you to make 2 projects, A & B.

1.This way you can have both.
2.Dont have to bother with excluding one from project.
3.And you can have 2 main() without giving you error.

Also VS allows you to open multiple projects at the same time, and you can selectively build any of them.

(Code A can be improved)
Vinayak

@vinayakgarg
Your method didn't work for me. VS allows me to open up only 1 solution at a time. I also created 2 projects in the same solution but that didn't work as well. Looks like I'll just have to disable 1 while running the other. Unless, I'm doing what you suggested wrong. Please tell me how to do it.
Thanks.

@vinayakgarg
Your method didn't work for me. VS allows me to open up only 1 solution at a time. I also created 2 projects in the same solution but that didn't work as well. Looks like I'll just have to disable 1 while running the other. Unless, I'm doing what you suggested wrong. Please tell me how to do it.
Thanks.

Do you know the difference between a solution and a project? Based on that response, I don't think you do. If you want to continue to use F5 to run your program, you'll have to learn this and how to manipulate Visual Studio's settings.

In the simplest terms I can manage, a solution contains one or more projects that work together to solve a problem. A project is an individual program. The attached image is an example of a solution with multiple projects. The solution's name is "Lakewood ITA 2.0". Within the solution there are 5 projects named "Codelib", "ITA Admin", "ITA Client", etc...

You can control which projects get built and run by manipulating the solution's properties. To modify a solution's properties, right-click the solution and select "Properties". In the dialog that comes up, you can modify the settings to say which project(s)/program(s) to build when you rebuild the solution.

Also, if you right-click an individual project, you can select "Set as StartUp Project" to tell the system that it's the one you want to run.

The other option is to simply use the one-solution-multiple-project layout and get familiar with the options available on the right-click menu.

If this is still too much for you, do what was mentioned before.

Thank you everybody for all the replies. I will just stick with disabling one while I run the other.

FYI, in Visual Studio 2010, simply right click on the Solution and "Set Active Projects..." then choose the "Current Project" radio button.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.