这个问题试图收集每年出版的数十本不良C++书籍中的少数珍珠.
与许多其他编程语言不同,这些编程语言经常从互联网上的教程中随处获取,很少有人能够快速学习C++,而无需学习编写精良的C++书籍.这样做太复杂了.事实上,它是如此庞大和复杂,有很多非常糟糕的C++书籍.我们并不是在谈论糟糕的风格,而是体育明显的事实错误和促进糟糕的编程风格.
请编辑接受的答案,以提供高质量的书籍和近似的技能水平 - 最好 在 C++聊天室讨论您的添加后.(如果他们不同意建议,常客可能会毫不留情地撤销你的工作.)添加一篇关于你亲自阅读/受益的每本书的简短描述/描述.随意讨论质量,标题等.符合标准的书籍将被添加到列表中.由C和C++用户协会(ACCU)撰写评论的图书都有指向评论的链接.
*注意:常见问题和其他资源可以在C++标签信息和c ++ - faq中找到.
我有以下代码:
#include <iostream>
#include <string>
using namespace std;
string name;
string age;
int main() {
cout <<"Name: ";
cin >> name;
cout << endl;
cout <<"Age: ";
cin >> age;
cout << endl;
cout << "Your name is " << name << ", and you are " << age << " years old." << endl;
cout << "Press enter to close this application" << endl;
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我注意到如果我在输入中输入一个空格来表示它不会给我输入名称的机会,它会在空格后查看该条目作为年龄.如果这是一个新手的错误,我很抱歉,这可能是.我之前编写过Java并决定转换到C++,因为它更适合我的需求.我也可能将我的代码格式化为您的标准,如果您愿意,请更正.

我也注意到了另一个错误,我在Java中从来没有遇到任何问题.我无法弄清楚如何防止它在完成处理后立即关闭.我听说你可以使用"(暂停")系统.";但我也被告知不要使用它,我什么就用我听说使用的getchar()真的很困惑;不过.它似乎没有做任何事情.
任何帮助都会非常感激,因为我在C++方面是一个完全的初学者.
我要求用户在控制台上输入一个字符串.但我不知道字符串的长度.
如何定义一个结构以适应具有可变长度的输入?
int main(){
int i;
char s[10];
cout << "input string:";
cin >> s;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果输入字符串长度超过10,示例代码将导致堆损坏.
我有这个C ++简单程序;
#include <iostream>
using std::endl;
using std::cout;
using std::cin;
using std::getline;
#include <string>
using std::string;
struct Repository
{
string name;
string path;
string type;
string command;
};
int main()
{
Repository rp;
cout << "\nEnter repo name: ";
cin >> rp.name;
cout << "Enter repo path: ";
cin >> rp.path;
cout << "Enter repo type: ";
cin >> rp.type;
cout << "Enter repo command: ";
getline(cin, rp.command);
cout << "\nRepository information: " << endl;
cout << rp.name << "\n" …Run Code Online (Sandbox Code Playgroud) 我的工作是编写一个程序,将句子转换为大写和小写.
#include <iostream>
using namespace std;
int main ()
{
int current;
string sent;
cout << "Please enter a sentence: ";
cin >> sent;
for (int i = 0; i < sent.length(); i++)
{
current = (int) sent[i];
cout << "Your sentence in all lower case: "<< sent;
cout << endl;
if (current >= 65 && current <= 90)
{
current += 32;
sent[i] = (char) current;
cout << "Your sentence in all upper case: " << sent;
}
} …Run Code Online (Sandbox Code Playgroud) 我正在为入门级编程课程创建一个基本的搜索功能,并遇到了这个问题.
string query;
cout<<"Enter a query: ";
cin>>query;
cout<<query<<endl; // debug line
find(query); // calls to another function
Run Code Online (Sandbox Code Playgroud)
Wellington, Florida,我得到输出:Wellington,Miami, Florida,我得到输出Miami,似乎cin忽略了空格之后的所有内容并使我的函数调用陷入困境.有没有办法解决 ?
我正在尝试编写一个类似游戏的冒险程序,我需要if else语句才能正常工作.但是,每当我运行if if语句时,它都不会输出我想要的内容.
#include <iostream>
using namespace std;
int main()
{
while(true)
{
cout << endl << "What will you do?: ";
string input;
cin >> input;
if (input == "Nothing" || input == "nothing")
{
cout << "Why would you do nothing?" << endl;
}
else if (input == "Look North" || input == "Look north" || input == "look North" || input == "look north")
{
cout << "You look north. There is nothing north." << endl;
} …Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序,需要用户输入。我需要输入在单词之间包含空格。我在寻找解决方案时遇到了麻烦。在您提出问题之前,我已经在stackoverflow上尝试了其他多个问题,其中同一问题。这些是我尝试过的一些。 如何在C ++中cin空间?
我的代码的问题在于,一旦调用我的setBusinessName方法,它便会自动完成。它输出然后返回自身,而无需等待我输入数据。需要帮助...
string setBusinessName()
{
string name = "";
cout << "The name you desire for your business:";
getline(cin, name, '\n');
cout << name;
return name;
}
Run Code Online (Sandbox Code Playgroud) 我正在编写一个函数int count_words(string str),它返回字符串中所有单词的计数.
问题是无论输入如何,结果都是1.任何人都可以帮忙吗?
#include <iostream>
#include <string>
using namespace std;
int count_words(string str)
{
int i,j;
j = 1;
int l = str.length();
for(i = 0; i < l; i++)
{
string c = str.substr(i,1);
if(c == " ")
{
j++;
}
}
cout << "The total word in the string: " << j << endl;
return j;
}
int main()
{
string str;
cout << "Please enter a string: ";
cin >> str;
int result = …Run Code Online (Sandbox Code Playgroud) 我想从给定的分隔符读取std::string命名。默认情况下,当我执行. 如果是,我可以使用.destinationstd::cinstd::istream::operator<<destination << std::cindestinationchar[]std::getline
我们有什么用 std::string?
我正在尝试编写一个简单的程序,将名称读取为C样式的字符串。然后垂直打印名称,每行一个字符。
当前,当程序提示用户输入其名称时,例如。亨利·詹姆斯(Henry James),仅垂直打印“亨利”(Henry)。在名称之间的中断处停止打印。
char myName[ 64 ] = "";
cout << "Enter your name: ";
cin.get( myName, 64 );
int i = 0;
while ( myName [ i ] != ' ' )
{
cout << myName[ i ] << endl;
i++;
}
getch();
return 0;
Run Code Online (Sandbox Code Playgroud)
我尝试将cin.ignore()放在cin.get()之前,但这最终破坏了程序。在while循环中我缺少什么?
我正在做一些你必须输入你的名字的东西,但我的字符串没有保存空格,为什么?
#include <iostream>
using namespace std;
string name;
int main()
{
cout<<"<System>: Please enter your name"<<endl;
cin>>name;
cout<<name;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我进入了:
测试123
我得到了:
测试