简体   繁体   English

在C ++中读取字符串,在C ++中进行编译

[英]reading strings in c++, compiling in c++

#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 .. 简而言之:我来自Java ..

is that enough? 够了吗? I already had enormous problems with reading the strings, and had to search about 50 pages for needed libraries. 我在读取字符串时已经遇到了很大的问题,不得不搜索大约50页来查找所需的库。 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. 另外,我在语句中包括了命名空间std,但是在cout之前删除std时却收到错误消息。

"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) 据我所知,控制台已加载了所有必需的文件(对于我使用Visual Studio服务器的db文件(我在本地保存的dll。)(对于调试器,我是德国人,“ geendet”表示完成,而不是在那里)。有什么问题)

  • Nils Drees - 尼尔斯·德里斯-

If you just started learning c++ and using Visual Studio i would recommend that you create your project in this manner : 如果您刚刚开始学习c ++并使用Visual Studio,我建议您以这种方式创建项目: 选择空项目

As the option says it'll create an empty project where you'll have to manually add header and cpp files in those: 如选项所述,它将创建一个空项目,您必须在其中手动添加标头和cpp文件:

只需右键单击该文件夹,然后选择添加->添加新项

Header file should go to the Header Folder and cpp file to the Source folder. 头文件应转到“头文件夹”,而cpp文件应转到“源”文件夹。

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". 您遇到的问题是因为在创建新项目时选择了“ Windows控制台应用程序”,默认情况下,Visual Studio会创建该项目,其中包括名为“ 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: 这个头文件在较大的项目中很有用,基本上,您应该在“ pch.h”头文件中添加头文件,这些头文件将经常用于多个文件中,例如:

#include <iostream>
#include <string>

etc... And then you should include "pch.h" in cpp file at the very top. 等等...然后,您应该在最顶部的cpp文件中包含“ pch.h”。

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. 希望这对您有所帮助。

Thank you @"Radu Z". 谢谢@“ 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. 关于名称空间,我删除了它,因为无论如何我都必须使用stds。

From your question I assume you are using Visual Studio . 根据您的问题,我假设您正在使用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 然后转到Project-> Properties ,然后转到C / C ++-> Precompiled Headers

In the first setting: Precompiled Header select Create (/Yc) 在第一个设置中: 预编译头,选择创建(/ Yc)

In the second setting: Precompiled Header File select stdafx.h 在第二个设置中: 预编译的头文件,选择stdafx.h

In the third setting: Precompiled Header Output File select $(IntDir)$(TargetName).pch 在第三个设置中: 预编译的头输出文件中选择$(IntDir)$(TargetName).pch

This is in fact where you define your .pch file. 实际上,您可以在其中定义.pch文件。

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. 我无法破译您的错误,但我认为您应该检查pch.h文件和编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM