#include <iostream>
using namespace std;
struct box{
char maker[40];
float height;
float width;
float length;
float volume;
};
void show(box x);
box xie(box x);
int main(){
box temp;
box xi=xie(temp);
show(xi);
}
void show(box x){
cout << "\nThe maker of the box is " << x.maker << "." << endl;
cout << "The height of the box is " << x.height << "." << endl;
cout << "The width of the box is " << x.width << "." << endl;
cout << "The length of the box is " << x.length << "." << endl;
cout << "The volume of the box is " << x.volume << "." << endl;
}
box xie(box x){
box xt=x;
cout << "The following is the example box."<<endl;
cout << "Please enter the box maker: ";
cin >> xt.maker;
cout << "Please enter the height: ";
cin >> xt.height;
cout << "Please enter the width: ";
cin >> xt.width;
cout << "Please enter the length: ";
cin >> xt.length;
cout << "Please enter the volume: ";
cin >> xt.volume;
return xt;
}
是你的环境的问题,程序本身是没有换行和空格的。你可以直接运行生成的exe看看
【以下回答由 GPT 生成】
这个C语言代码中出现换行和空格的原因是由于cout和cin语句在输出和输入数据时,会自动在末尾添加换行符或空格,以提供更好的可读性。
具体来说,在代码中的show函数中,每个cout语句结束后都添加了endl,它会在输出的文本后添加一个换行符,使得每个输出都在新的一行显示。
在代码中的xie函数中,cout语句在输出提示用户输入时末尾都没有添加endl,所以用户的输入在同一行显示。然后在下一行开始时,由于使用了大量的cout语句,每个cout语句都会在前一行之后添加一个换行符,使得输出的文本在新的一行显示,从而实现换行的效果。
如果你不想要这些额外的换行符和空格,可以使用"\n"代替endl来手动控制换行,或者使用特定的输入输出格式指定,避免自动添加换行和空格。 例如:
cout << "\nThe maker of the box is " << x.maker << ".";
cout << "The height of the box is " << x.height << ".";
cout << "The width of the box is " << x.width << ".";
cout << "The length of the box is " << x.length << ".";
cout << "The volume of the box is " << x.volume << "." << "\n";
这样就可以避免额外的换行符。
同样,在输入时,使用cin语句时,它会自动在用户输入完数据后添加一个空格,以便下一个输入。如果你不想要这个空格,可以使用cin.getline()函数代替cin,通过指定输入的长度和特定字符来防止插入空格。
以上是关于代码中出现换行和空格的解释,希望能帮到你!如果还有其他问题,请随时提问。
【相关推荐】