reading strings in c++, compiling in c++

0
#include "pch.h"
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <sstream>
#include <istream>
using namespace std;
int main()
{
    std::string color, pluralNoun, celebrity;

    std::cout << "enter a color: ";
    std::getline(cin, color);  
    std::cout << "enter a plural noun: ";
    std::getline(cin, pluralNoun);
    std::cout << "enter a celebrity: ";
    std::getline(cin, celebrity);

    cout << "Roses are " << color << endl;
    cout << pluralNoun << "are blue" << endl;
    cout << "I love " <<celebrity <<endl;


    return 0;
}

Hallo developers..

To keep it short: I come from Java ..

is that enough? I already had enormous problems with reading the strings, and had to search about 50 pages for needed libraries. In addition, I have included in a statement the namespace std, and yet I get an error message when I remove std before the cout s.

"ConsoleApplication3.exe" (Win32): "D:\Users\nils\source\repos\ConsoleApplication3\Debug\ConsoleApplication3.exe" geladen. Symbole wurden geladen.
"ConsoleApplication3.exe" (Win32): "C:\Windows\SysWOW64\ntdll.dll" geladen. Symbole wurden geladen.
"ConsoleApplication3.exe" (Win32): "C:\Windows\SysWOW64\kernel32.dll" geladen. Symbole wurden geladen.
"ConsoleApplication3.exe" (Win32): "C:\Windows\SysWOW64\KernelBase.dll" geladen. Symbole wurden geladen.
"ConsoleApplication3.exe" (Win32): "C:\Windows\SysWOW64\msvcp140d.dll" geladen. Symbole wurden geladen.
"ConsoleApplication3.exe" (Win32): "C:\Windows\SysWOW64\vcruntime140d.dll" geladen. Symbole wurden geladen.
"ConsoleApplication3.exe" (Win32): "C:\Windows\SysWOW64\ucrtbased.dll" geladen. Symbole wurden geladen.
Der Thread 0x4d6c hat mit Code -1073741510 (0xc000013a) geendet.
Der Thread 0x4dc8 hat mit Code -1073741510 (0xc000013a) geendet.
Der Thread 0x486c hat mit Code -1073741510 (0xc000013a) geendet.
Das Programm "[2132] ConsoleApplication3.exe" wurde mit Code -1073741510 (0xc000013a) beendet.

Can anyone explain why this does not work, and if there are other ways to read the strings? As far as I can read consoles all required files were loaded (for the pdb file I used the visual studio server, dll I saved locally. (sry for the debugger, i am german, "geendet" means that it finished, not that there are any problems)

  • Nils Drees -
c++
string
compilation

4 Answers

1

If you just started learning c++ and using Visual Studio i would recommend that you create your project in this manner : Chosse Empty Project

As the option says it'll create an empty project where you'll have to manually add header and cpp files in those:

Just right click on the folder and select Add -> Add New Item

Header file should go to the Header Folder and cpp file to the Source folder.

The problem you encountered was because when you created your new project you selected "Windows Console Application", by default Visual Studio will create the project including the pre-compiled header named "pch.h".

This header is useful in a bit larger projects, basically you should include in "pch.h" headers you will use very often in multiple files like:

#include <iostream>
#include <string>

etc... And then you should include "pch.h" in cpp file at the very top.

Warning tho, this is not even the basics of the topic, but if you're only starting and trying things out i think it's going to be easier to deal with an empty project, but for later do some research on pre-compiled headers, they're very useful.

Also as other mentioned, try to avoid

using namespace std;

as much as possible

Hope this will help you for later.

answered on Stack Overflow Dec 13, 2018 by Ben • edited Jun 20, 2020 by Community
0

Thank you @"Radu Z". You were almost rigth: the problem was, that the header had to be used, i do not know why, but it had to be empty, and I also do not know where this makes sense, but it worked for me to delete the lines concerning the namespace, leaving me with:

#ifndef PCH_H
#define PCH_H


// TODO: add headers

#endif //PCH_H

I am very grateful that you all answered that quickly, and I promise to change my Language to english for the next questions. Concerning the namespace, I removed it since I had to use the stds anyway.

-1

First, I deleted the first line of your code, which is

#include "pch.h"

, as I didn't have that file and it compiled. Maybe you don't have that file either. Then, I put a space before "are blue", so the plural noun and the "are blue" aren't connected.

cout << pluralNoun << " are blue" << endl;

And it all worked perfectly. Maybe your problem is the compiler you use? I can't decipher your errors but my opinion is that you should check the pch.h file and the compiler.

answered on Stack Overflow Dec 12, 2018 by Radu Z.
-1

From your question I assume you are using Visual Studio. You should always have the first include line

#include "stdafx.h"

or

#include "pch.h" 

as introduced recently. See also.

Then go to Project -> Properties and then to C/C++ -> Precompiled Headers

In the first setting: Precompiled Header select Create (/Yc)

In the second setting: Precompiled Header File select stdafx.h

In the third setting: Precompiled Header Output File select $(IntDir)$(TargetName).pch

This is in fact where you define your .pch file.

answered on Stack Overflow Dec 12, 2018 by Michael Haephrati • edited Dec 12, 2018 by Michael Haephrati

User contributions licensed under CC BY-SA 3.0