我已经告诉别人,编写using namespace std;代码是错误的,我应该用std::cout和std::cin直接代替.
为什么被using namespace std;认为是不好的做法?是低效还是冒着声明模糊变量(与名称std空间中的函数具有相同名称的变量)的风险?它会影响性能吗?
#include <string>
std::string input;
std::cin >> input;
Run Code Online (Sandbox Code Playgroud)
用户想要输入"Hello World".但是cin在两个词之间的空间失败了.我怎样才能cin完全接受Hello World?
我实际上是用结构做这个,cin.getline似乎不起作用.这是我的代码:
struct cd
{
std::string CDTitle[50];
std::string Artist[50];
int number_of_songs[50];
};
std::cin.getline(library.number_of_songs[libNumber], 250);
Run Code Online (Sandbox Code Playgroud)
这会产生错误.有任何想法吗?
这是我目前的C++代码.我想知道如何编写一行代码.我还会使用cin.getline(y)或不同的东西吗?我已经检查过,但找不到任何东西.当我运行它时,它完美地工作,除了它只键入一个单词而不是我需要输出的整行.这是我需要帮助的.我在代码中概述了它.
谢谢你的帮助
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>
using namespace std;
int main()
{
char x;
cout << "Would you like to write to a file?" << endl;
cin >> x;
if (x == 'y' || x == 'Y')
{
char y[3000];
cout << "What would you like to write." << endl;
cin >> y;
ofstream file;
file.open("Characters.txt");
file << strlen(y) << " Characters." << endl;
file << endl;
file << y; …Run Code Online (Sandbox Code Playgroud)