我试图用c ++来计算文本文件中的字符,这是我到目前为止,由于某种原因我得到4.即使你有123456个字符.如果我增加或减少角色我仍然得到4,请提前帮助和感谢
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const char FileName[] = "text.txt";
int main ()
{
string line;
ifstream inMyStream (FileName);
int c;
if (inMyStream.is_open())
{
while( getline (inMyStream, line)){
cout<<line<<endl;
c++;
}
}
inMyStream.close();
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个动态内存.数组的大小由用户输入决定.我收到以下错误,
"expression must have a constant value".
好像我做错了什么.请帮我!我怎样才能让这种动态变化?
这是我到目前为止:
int* IntPtr = NULL;
int main(){
int arraySize;
cout << "How many numbers will be on the list? ";
cin >> arraySize;
IntPtr = new int[arraySize];
Contact list[arraySize]; // <-- expression must be constant
//more code
delete [] IntPtr;
Run Code Online (Sandbox Code Playgroud) 请协助,我正在尝试格式化电话号码(111)111-1111.我有以下代码,但我想写得更短.
int main(){
string phone_number;
cin >> phone_number;
cout<<"(";
for(int i = 0; i < 3; i++) {
cout << phone_number[i];
}
cout << ")";
for(int i = 3; i < 6; i++) {
cout << phone_number[i];
}
cout << "-";
for(int i = 6; i < 10; i++) {
cout << phone_number[i];
}
cout<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
请协助