'Native' Program in Visual vs gcc

My background: I am a new programmer, taking an "intro to programming II" class (as in white board and desks in a classroom, not definition & implementation), and I am highly enjoying programming. It takes very little research on the internet to realize that I still have A TON to learn about programming. For this class, we program in a linux environment using a GNU compiler. Being the enthusiastic ignorant pre-programmer that I am, I installed Microsoft Visual C++ Express on my computer, to tinker around with the language a little bit more. Express highly annoys me, it formats my text in ways that I HATE, and in this case compiles differently than GNU.

Problem: Being a freshman, me and my friends realized that we are starting to curse far too much, so we came up with an idea for a swear jar. So I volunteered, why buy a jar when you can just program one? Luckily for me, Visual doesn't like the way I handle strings I guess.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
#include <fstream>

int main(int argc, char* argv[])
{
	ifstream in;
	in.open(argv[1]);
	string Name;

	while(!in.eof())
		{cin >> Name;
		 cout << Name << endl;};
	in.close();

	return 0;
}


This simple 'native' program (as Microsoft seems to call it) simply repeats the list of names that are in a text file. It works in my Linux environment, but Express won't Compile. Help?
You get a bonus point if you also come up with a good punishment for who ever's name gets selected from the swear jar at the end of this semester!
Thanks!

EDIT: The text editor has red wavy lines under the '>>' and '<<' operators, saying they don't match the operands.
Last edited on
closed account (S6k9GNh0)
Here's a tip: at this level of programming, if your code is conformant to a real standard, generally any popular compiler will accept your code in an unbiased manner. While I hate VC++ myself, it does compile almost all of C++03 (and some more crap that it shouldn't allow to compile at all).

Moving on, here some things I can point out:

1) What if argv[1] doesn't exist? You'd be reading garbage.
2) What line is iterating or reading from the file?
3) While this does compile under latest gcc on my PC, I'm not too certain on why it fails under VC++. I found a strange allowance under gcc which I'm not sure is so conformant (although I'm not at all certain) but I'd need the error to be sure:

1
2
3
4
5
6
7
int main(int argc, char* argv[])
{
        ;;;;;
	;;;;;;;

	return 0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
}


Can't tell if that's a good or bad thing. I'd hate to see the day I see code spammed with semi-colons "just to be safe".

4) I'm not so sure you should be using the "cin" stream here.... I really hate using the "cin" stream at all, it's really shitty.
Last edited on
Are you compiling for C++ or C++/CLI? C++/CLI is very different and might require different stuff.
#include <string> is required.
Which g++ seems to have gratituously included.

And the program should be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
        if( argc > 1 ) // required if you want to access argv[1]
        {
	     ifstream in( argv[1] ) ;  // constructor opens the stream
	     string Name;
             // check for failure after (not before) attempted input
	     while( in >> Name ) // as long as we have successfully read a name 
                      cout << Name << '\n' ; // std::endl (flush the stream) is not required here
        } // and the destructor closes the stream

        // return 0 ; is implied if you omit it
}

Thanks for the help everyone :) The code under the while loop was meant to be in, not cin. It was only cin because I was seeing if it would take any string arguments, even when brought in straight from the command prompt.

Simply including <string> worked. Now I know <string> is necessary when not using g++.

I will also put in a few lines of code to make sure my program doesn't spew nonsense i there's no argv[1], if only for the good programming practice. Thanks again!
Topic archived. No new replies allowed.