除以 str.length() 什么也没显示

  • 本文关键字:显示 str length 除以 c++
  • 更新时间 :
  • 英文 :


以下给了我分割错误:

#include <conio.h>
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
#include <cmath>
using namespace std;
void func(string str){
if (str == "")
{
cout << "Wrong input";
return;
}
int length = str.length();
int div = length;
cout<<div;
}
int main(){
string str;
getline(cin, str);
func(str);
return 0;
}

如果我省略 round((:

int div = 1/length;
cout<<div;

我看不到输出。

正在处理的 string.length(( 函数可能有问题。

编辑:完整代码:

#include <conio.h>
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
#include <cmath>
using namespace std;
bool shw = false;
void sleep(int millis)
{
this_thread::sleep_for(chrono::milliseconds(millis));
}
void func(string s)
{
string tmp = "";
if (s == "")
{
cout << "Wrong input";
return;
}
//int l=s.length();
int iv = s.length();
cout<<iv;
sleep(2000);
if (!shw)
{
if (rand() % 100 < 50)
{
for (int i = 0; i < s.length(); i++)
{
tmp += s[i];
clrscr();
cout << tmp << endl;
shw = true;
sleep(iv);
}
}
else
{
for (int i = s.length() - 1; i < s.length(); i--)
{
string tmp2 = "";
for (int j = 0; j < i; j++)
tmp2 += " ";
tmp = s[i] + tmp;
clrscr();
cout << tmp2 + tmp << endl;
shw = true;
sleep(iv);
}
}
}
else
{
if (rand() % 100 < 50)
{
for (int i = 0; i < s.length(); i++)
{
string tmp2 = "";
for (int j = 0; j < i; j++)
tmp2 += " ";
tmp = s.substr(i);
clrscr();
cout << tmp2 + tmp << endl;
shw = false;
sleep(iv);
}
}
else
{
for (int i = s.length(); i > 0; i--)
{
tmp = s.substr(0, i);
clrscr();
cout << tmp << endl;
shw = false;
sleep(iv);
}
}
}
func(s);
}
int main()
{
string str;
getline(cin, str);
func(str);
return 0;
}

这是因为整数除法代替round(1.0/length);。也使用floatdouble.

最新更新