Sign in to follow this  
GROUDON

How can the same code work in 1 project and not another?

Recommended Posts

GROUDON    122
//MyExample1
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

void MyFunc(int *nVar);

int main(void)
{
   int x;
   cout << "Enter a number: " << endl;
   cin  >> x;
   MyFunc(&x);
   cout << "the new number is: ";
   cout << x << endl;
   system("PAUSE");
   return 0;
}

void MyFunc(int* nVar)
{
   ++*nVar;
}
It doesn't work! here is a line from the error output...
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(17) : error C2143: syntax error : missing '{' before ':'
Then i made another project, and chose "Empty Project", then made a new item, chose "C++ FIle (.cpp)". I then copied the code text from "MyExample1" and put it in "MyExample2", this is next code is copied directly from "MyExample2"
//MyExample2
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

void MyFunc(int *nVar);

int main(void)
{
   int x;
   cout << "Enter a number: " << endl;
   cin  >> x;
   MyFunc(&x);
   cout << "the new number is: ";
   cout << x << endl;
   system("PAUSE");
   return 0;
}

void MyFunc(int* nVar)
{
   ++*nVar;
}
It works perfectly! Pretty much this exact incident happened several times before with other projects. The funny thing is, that procedure of making a project is the same as when i first made it. The only other difference between the two projects that i can think of is: that the other was made yesterday, where this one^ was made today, the last one previously had a header file in, where this one didn't. i remade the previous program to include iostream, and that's when it stopped working then i got rid of the header and put the code in the .cpp file, since this program is small enough for that. The first one doesn't work, the last one does. I can't even begin to wonder how in the world one works and another doesn't, provided they have the EXACT same code. What am i doing wrong?

Share this post


Link to post
Share on other sites
Palidine    1315
Likely there are differences in the project configuration. You either made them, or maybe you clicked on a different template or any number of reasons. The one true thing about computers is that they are 100% deterministic; they don't just act differently in different situations for no logical reason. =)

If you're curious, then open 2 instances of VS each with one of the projects. then go through each and every tab and option in the project settings to find a line or option that is at all different. When you find the difference, that's your cause.

-me

Share this post


Link to post
Share on other sites
GROUDON    122
Quote:
Original post by PhoenixAdmin
Syn. is it possible that you could have misspelled something?


Turns out, yes i did mispell something...

The extention to the file "main"!
I spelled it "main.c"
Where it should be "main.cpp"

I must've accidently added an extention, the MVS .NET 2003 adds the extension for you when you don't.

I found out by opening two instances of MVS then after a little while of looking through the properties of both projects, (like Palidine said) i noticed a little difference in the little tabs near the top that show you what file your looking at, and as i double-took a few times, i notice that i named MyFunc1's source code file "main.c", the other, working one, was named "main.cpp".

That MIGHT prove to be a problem. so i went into windows explorer by pressing a little shortcut button on my keyboard, (isn't that cool) and went to "main.c" and changed it to "main.cpp". I opened up MVS, deleted the main.c in the project, then went to "Add Existing Item" and clicked on "main.cpp", hit "F5" and it ran just like MyFunc2! At least i found the problem!

IN SHORT: i mispelled the file extention.

Share this post


Link to post
Share on other sites
GROUDON    122
Quote:
Original post by PhoenixAdmin
Syn. is it possible that you could have misspelled something?


Turns out, yes i did mispell something...

The extention to the file "main"!
I spelled it "main.c"
Where it should be "main.cpp"

I must've accidently added an extention, the MVS .NET 2003 adds the extension for you when you don't.

I found out by opening two instances of MVS then after a little while of looking through the properties of both projects, (like Palidine said) i noticed a little difference in the little tabs near the top that show you what file your looking at, and as i double-took a few times, i notice that i named MyFunc1's source code file "main.c", the other, working one, was named "main.cpp".

That MIGHT prove to be a problem. so i went into windows explorer by pressing a little shortcut button on my keyboard, (isn't that cool) and went to "main.c" and changed it to "main.cpp". I opened up MVS, deleted the main.c in the project, then went to "Add Existing Item" and clicked on "main.cpp", hit "F5" and it ran just like MyFunc2! At least i found the problem!

IN SHORT: i mispelled the file extention.

EDIT: double post! sry!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this