New student in C++ with Linux/Windows question

Pages: 12
Hi I've decided to major in Computer Information Systems in college and am taking my first C++ class. My instructor recommended we use Windows 7 or 8 with Visual Studio 2010 or 2012 for the class because that is what he uses and will provide instructions and troubleshooting based on that specifically. He said he'll basically be checking the source code of the work.

He said we are permitted to use other writing/compiling programs but he won't be able to assist much. With that being said, My wife uses Windows 8 at home so I can use the Visual Studio if I need to but would rather work on my assignments while I'm at work. I'd be using my Chromebook with Xubuntu dual-booted. I tried using Code:Blocks with the Hello World! program and had no problem on my laptop but it wouldn't work when I copied and pasted into Visual Studio and tried to run it...

Is there a way I can write my work in Linux and have it run on Visual Studio (mainly for peace of mind so I can see it runs right) or should I just break down and do my work at home?

Thanks for any assist,

Joe
"Hello World" isn't that large. Do you mind copy-pasting your code here?

For simple projects that don't use API's the code should work on both systems, unless you are using the system() function from cstdlib in which case just comment it out for now. MS VS usually has a specific header that it requires but you shouldn't need it from this program if you've included iostream.
Last edited on
> but it wouldn't work
That is not an error message.


> would rather work on my assignments while I'm at work
That's the spirit.
My instructor supplied instructions for what he said would work in Visual Studio.. On a side note, I read on a discussion thread he posted for our class that we will learn to write ANSI C++ to develop console (DOS) applications... I have a feeling my problem is that I using the GNU/G++ format?

1
2
3
4
5
6
7
8
9
//P01 - Hello World! by: Joseph K.

#include <iostream>
using namespace std;

void main()
{
    cout << "Hello World! by: Joseph K. \n\n";
}


The error I got when running this through codeblocks on Xubuntu was error: '::main" must return 'int'

The following is the re-write that successfully ran in codeblocks...

1
2
3
4
5
6
7
8
9
//P01 - Hello World! by: Joseph K.

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World! by: Joseph K. \n\n" << endl;
}
closed account (jwkNwA7f)
Add return 0; at the end of main().

1
2
3
4
5
6
7
8
9
10
//P01 - Hello World! by: Joseph K.

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World! by: Joseph K. \n\n" << endl;
    return 0;
}
In C++, not returning anything from main is the equivalent of returning 0. It is not required.
closed account (jwkNwA7f)
@cire Really, I have been to A LOT of tutorial and books and no one has told me that.
I can run Hello World with and without return 0; when in Xubuntu using codeblocks.

My main issue is not being able to copy/paste the code into Visual Studios on Windows and have the same result. Is my code not written in ANSI C++? I'm guessing I'll need to find and use a different IDE on Xubuntu to allow the code to run on Visual Studios and the IDE in Xubuntu.
@op ,
As gcc said main must have a return type of int , void main is platform/compiler specific and won't work everywhere but int main should work everywhere so just keep int main.
@cppprogrammer297 - Yes, really. It has been that way since C++98.
popojoe wrote:
My main issue is not being able to copy/paste the code into Visual Studios on Windows and have the same result.


You should be able to, assuming you've chosen the right project/settings. All you are saying is "I cannot" without any specifics as to what is happening. If you want constructive input, provide more information.
I'll run the program on Visual Studios when I get home and post errors, if any.

Thanks for all the responses
Just to reiterate the takeaway here:
he posted for our class that we will learn to write ANSI C++
void main()

This is not ANSI C++.
I think that it is even better that you will use two compilers. You can compare results of compilations that to understand what does not satisfy the C++ Standard. By default VC++ has switched on option to use Microsoft language extensions. You should switch off this option if you got an unpredictable result.
Last edited on
For reference I found that the best way for me to learn programming was to decide on something I want to make and get stuck into it at home, the internet is your friend in this case as just copying code will NOT teach you how the language works, all it will do is make you think that you learnt the language when in actual fact you have learnt nothing at all which will come back to bite you.

With my method you experience the errors as you code and then learn via the net why you get those errors, in turn you will usually remember those experiences in the back of your mind and you will begin to naturally use the language correctly, only after this happens will you start to get the hang of system APIs.

You should also point out to your teacher that he is not doing his job properly if he is teaching his students bad habits such as skipping return in a function the compiler is expecting to return something in. Side note, IDEs aren't usually the source of a project's problems, what normally is the problem is missing/incomplete path variables or bad code.
@awsdert
You should also point out to your teacher that he is not doing his job properly if he is teaching his students bad habits such as skipping return in a function the compiler is expecting to return something in.


It is a good practice to skip return in main for simple programs. It does not distract attention from the body of main.
How is it EVER good practice to skip the return, it doesn't distract at all for that matter, any programmer who does skip it is a bad programmer as far I'm concerned. All errors (potential or actual in this case) should be dealt with if at all possible, people like you who skip it invite problems to a program. I'm a hobby programmer but I ALWAYS deal with whatever error I can find so that if anyone ever used my code they could focus on learning what it actually does instead of fixing errors caused by BAD programming practice. I would be willing to bet that it is these sorts of habits that cause the problems that closed source programs usually have when they first start out.
@awsdert

How is it EVER good practice to skip the return, it doesn't distract at all for that matter, any programmer who does skip it is a bad programmer as far I'm concerned


I think the problem is that you have little experience in C/C++. There is neither need to use return in simple programs that can be fit in several lines.

By the way the smallest valid program in C++ is

int main() {}

Can you say what is the usefulness to place here the return statement?!:)
Last edited on
It would have problems on older compilers or stricter settings.
Portability is the best way to attract users &/or developers and as such it is better to develop good habits then it is to develop bad habits and then correct them later when the compiler screams at you for doing so.
Re: Not returning a value from main
awsdert wrote:
It would have problems on older compilers or stricter settings.


It's been standard since 1998. If you're using a compiler older than that, you should consider upgrading. I don't see how "stricter" settings would be an issue since it is perfectly legal and well defined.

awsdert wrote:
Portability is the best way

It is portable.
Pages: 12