2

So, just started Programming I at college; unfortunately, this:

Submissions must be a notepad *.txt file in xx pt fixed width font

:facepalm:

That's besides the point though! So it is "required" (I haven't received a confirmation email on whether or not *nix is fine, so long as it works in Windows) to use VC++, which seems ridiculously unnecessary for:

#include <iostream>

int main()
{
    std::cout << "Hello, world." << endl;

    system("pause");

    return 0;
}

Besides the fact that I can't stand developing in a Windows environment, and prefer *nix machines...

My question is whether or not the following code will compile correctly in Windows, specifically getline in an attempt to duplicate system("pause") functionality:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string p;  

    cout << "Hello, world" << endl;
    cout << "Press enter...";
    getline(cin, p);

    return 0;
}

Are there any major differences between VC++ and *nix compilers that would affect the results of simple programs executing?

I've searched the web, and haven't found much of an answer.

spectre-d
  • 509
  • 4
  • 14
  • 3
    Conformant code should work on all compilers. However being a MSVS user I can say it lacks some features that g++ or clang have so you could get into trouble. – NathanOliver Aug 25 '16 at 17:44
  • Okay, I figured at some point it may become an issue, I'm sure not every Windows-Specific function has a platform-indepent solution. I was just wondering if there was some online reference material available. – spectre-d Aug 25 '16 at 17:46
  • @NathanOliver, all pretense of conformity is dropped with `system (pause)`. – SergeyA Aug 25 '16 at 17:46
  • @spectre-d Just use the stuff from [here](http://en.cppreference.com/w/) – NathanOliver Aug 25 '16 at 17:49
  • @NathanOliver, Awesome! Thank you! I was browsing that site before, I think, but C is my only venture so far into a "real" programming language (Python baby), and their documentation is good enough for a chimp. – spectre-d Aug 25 '16 at 17:50
  • Why not just remove the completely unnnecessary `system("pause")` from the code? – Konrad Rudolph Aug 25 '16 at 17:58
  • Because instructions are given to be followed, unfortunately. – spectre-d Aug 25 '16 at 18:00
  • @KonradRudolph It does kinda matter if you (or the professor) intends to run the app by double-clicking. – Mysticial Aug 25 '16 at 18:01
  • [FYI] more then likely you are going to run into [this](http://stackoverflow.com/questions/7786994/c-getline-isnt-waiting-for-input-from-console-when-called-multiple-times) when you use getline/cin to pause the program. – NathanOliver Aug 25 '16 at 18:02
  • @KonradRudolph, I'm not sure he is even going to compile it. He just wants the source and output in a single **formatted** text file. @ NathanOliver, right, I am aware of the obstacles regarding newline characters, thank you for the heads-up, though! – spectre-d Aug 25 '16 at 18:10
  • This is not about your teacher forcing you to use an operating system that your parents use (eww). What he's concerned about is that the file you submit is encoded in a predictable way so he can compile it and get the exact same result. You'll have to think like a programmer and address the *real* problem, *nix programs that convert to a Windows text file format are of course readily available. – Hans Passant Aug 25 '16 at 18:19
  • Regarding the .txt file, it's a matter of it not internally maintaining font formatting, is what I was getting at with the blockquote. – spectre-d Aug 25 '16 at 18:22
  • *"Submissions must be a notepad *.txt file in xx pt fixed width font"* - Notepad cannot write **any** kind of formatting. It is a plain text editor. Pretty challenging to turn in a *.txt file that uses a particular font and font size. Not sure what to make of that. But here is some unwanted advice: Get used to developing on Windows. For one thing, Visual Studio comes with a **capable** debugger. In comparison *nix has pretty little to offer. – IInspectable Aug 25 '16 at 19:28
  • @IInspectable, thank you, but I feel there's much more to learn where there are less luxuries, text-editor and terminal tools, please. There's something deeper to be learned as to why there is a problem and finding the mistake you made, rather than a debugger telling you what that is (of course very large software projects in an enterprise setting this would not be ideal). Not to mention, *nix is far more accessible to alterations of the system than Windows is, as well as being more transparent and mostly standardized. – spectre-d Aug 25 '16 at 19:57

2 Answers2

2

Answering specific question as asked: Yes, std::getline(std::cin, p) will suspend execution until the \n character is read from the std::cin. This assumes there is no such character before the prompt is displayed - for example, simply because user already pressed it.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • The question was not based upon the functionality of `getline`; rather whether or not it was a platform-independent operation. – spectre-d Aug 25 '16 at 17:54
  • 1
    @spectre-d, an I answered exactly that, didn't I? – SergeyA Aug 25 '16 at 17:56
  • Not directly, no. I asked whether it would compile in Windows, not what it does. – spectre-d Aug 25 '16 at 18:00
  • 3
    @spectre-d So you're only required to make it compile? Making it work correctly is not necessary? – Mysticial Aug 25 '16 at 18:04
  • 1
    @spectre-d, I would imagine, that when I tell you what it does on Windows, I also implicitly confirm it compiles there? – SergeyA Aug 25 '16 at 18:06
  • @Mystical, as long as get line works under vc++ with same headers idc tbh – spectre-d Aug 25 '16 at 18:11
  • @SergeyA, what's wrong with being explicit? It should be a commonality when explaining something to a party of which you do not know whether your explanation will be understood as you've put it, especially through a medium lacking other cues that humans use to communicate. – spectre-d Aug 25 '16 at 18:13
  • 1
    @spectre-d There's nothing wrong with being explicit. It's just that understanding that *compiling* is a necessary prerequisite to *working* is a basic concept that experienced programmers take for granted. Just like you can't have a *working* aircraft if you haven't *built* it yet. But it might not be obvious if you're completely new to the field. – Mysticial Aug 25 '16 at 18:19
  • @SergeyA, okay, I will apologize noting the answer to the question hidden in the entirety of the response being `Yes`, I was distracted by the rest of your response. Thank you. Also, I appreciate that you explained the possible design restrictions regarding the handling of newlines, I misinterpreted your answer. – spectre-d Aug 25 '16 at 18:27
1

Using one of the Visual Studio 2015 online compilers you can verify that it indeed compiles. There is the issue of not being able to type anything in, but you can at least verify if it compiles and see the output for future assignments.

nwp
  • 9,623
  • 5
  • 38
  • 68