我希望输出说出用户按下的箭头键.我得到的输出是:
"You pressed the Dz? button."
Run Code Online (Sandbox Code Playgroud)
我写的是:
unsigned int key;
string K;
do
{
key = getch();
if (key==72)
K=" up";
else if(key==80)
K="down";
else if (key==77)
K="right";
else if (key==75)
K="left";
else
{}
if (key!=224)
printf("You pressed the %s button.\n", K);
else
{}
}while(key !='q');
return;
Run Code Online (Sandbox Code Playgroud) 这是我到目前为止的代码.我要做的是让程序显示超过60英寸的孩子数和他们的身高.该程序现在显示超过60英寸的儿童数量,但我还需要它来显示超过60英寸的儿童的身高.提前致谢!
#include <iostream>
using namespace std;
int main ()
{
double childHeight[10];
int numChildren = 0;
for (int x = 0; x < 10; x = x + 1)
{
childHeight[x] = 0.0;
}
cout << "You will be asked to enter the height of 10 children." << endl;
for (int x = 0; x < 10; x = x + 1)
{
cout << "Enter the height of child: ";
cin >> childHeight[x];
}
cout << "The number of …Run Code Online (Sandbox Code Playgroud) 我是C++的新手.我通常用C#编程,所以我遇到了数组和循环的麻烦.当我尝试使用循环打印动态数组的内容时,它表示请求区域已损坏...例如,我将让它识别与数组内容一起使用的条件但不打印它的内容:
// Array.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void main()
{
int size=3;
int *p;
int myarray[10];
myarray[3]=4;
p=new int[size];
p[2]=3;
if(myarray[3]==4){
cout << myarray[3] +"/n";
cout << "Why?";
}
else
cout << "Not equal " << endl;
cin.get();
delete [] p;
}
Run Code Online (Sandbox Code Playgroud) #include <iostream>
using namespace std;
int main()
{
cout.precision(50);
cout<<"Anmol's Square Root Calculator!"<<endl;
cout<<""<<endl;
cout << "This program will compute the square root\n";
cout << "of a number using the Babylonian algorithm!\n";
cout<<""<<endl;
cout<<"Only positive numbers work with this algorithm!"<<endl;
cout<<""<<endl;
cout<<"All cycle #1 approximate square roots are guessed\n"<<"using 1 as the first approximate."<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
char playAgain='y';
while (playAgain !='n')
{
int count(25), cycle(1);
double guess, sqrt, num;
cout << "Please enter the number you would like to …Run Code Online (Sandbox Code Playgroud) 我试图让我的头围指针,并且无法理解为什么使用dereference运算符来打印一个值在"\n";添加到行时工作正常,但由于某种原因我在使用时没有得到任何输出endl;.终端显示没有输出endl;.这与输出缓冲区刷新有关吗?
#include <iostream>
using namespace std;
int main()
{
int arrayA[] = {0, 1, 2, 3, 4, 5};
int * ptr_P;
ptr_P = arrayA;
for (int i; i < 6; i++)
{
cout << *ptr_P << "\n"; // Works fine, but endl; does not
ptr_P++;
}
return(0);
}
Run Code Online (Sandbox Code Playgroud) 我通过逐个矩阵乘法遇到了麻烦.即一个看起来很像的表达式总是返回一个接近[1 0]的向量,而一个看起来相似的表达式返回正确的结果:
// version WITH temp var, correct
Eigen::Vector3d lcoord_eig(lcoord[0], lcoord[1], lcoord[2]);
auto lcoord2d = P3to2 * lcoord_eig;
std::cout << std::endl << lcoord2d << std::endl;
// version WITHOUT temp var, always [1 0]
auto lcoord2d_2 = P3to2 * Eigen::Vector3d(lcoord[0], lcoord[1], lcoord[2]);
std::cout << std::endl << lcoord2d_2 << std::endl;
Run Code Online (Sandbox Code Playgroud)
where P3to2是2乘3矩阵(Eigen::MatrixXd)并且lcoord是其他库的3d矢量类型,上面的代码包含在for循环中.
一些输出(由我注释):
-0.0036135
2.1684e-18 // correct
1
0 // [1 0], wrong
0.00209583
0.000388139 // correct
1
5.55112e-17 // [1 0], wrong
0.00148429
-0.000435008 // correct …Run Code Online (Sandbox Code Playgroud) 根据 的定义endl,它用于插入换行符并刷新流。我记得如果插入新行,则缓冲区将自动刷新。如果是这样,为什么endl在插入新行后仍然需要刷新流。
我有一个奇怪的问题,简单的程序,fe
main()
{
int i=1;
std::cout << i;
}
Run Code Online (Sandbox Code Playgroud)
结果用
1%
Run Code Online (Sandbox Code Playgroud)
在屏幕上.我无法摆脱那个"%".有谁知道发生了什么?我在Arch Linux上使用g ++(GCC)4.8.0 20130502.
我在做图表时遇到问题。我希望在同一行中输出图表,而不更改代码,也不使其水平。我希望使用 for 循环来解决这个问题,因为我可以迭代所有内容,因为我有相同的元素。
代码显示如下:
# include <iostream>
using namespace std;
class InterestCalculator
{
protected:
float principal_amount = 320.8;
float interest_rate = 60.7;
float interest = interest_rate/100 * principal_amount;
public:
void printInterest()
{
cout<<"Principal Amount: RM "<<principal_amount<<endl;
cout<<"Interest Rate(%): "<<interest_rate<<endl;
cout<<"Interest: RM"<<interest<<endl;
}
};
class LoanCalculator : public InterestCalculator
{
private:
int loan_term;
int month;
float month_payment;
public:
void displayVariable()
{
cout<<"Enter loan amount (RM): ";
cin>>principal_amount;
cout<<"\n\nEnter annual interest rate(%): ";
cin>>interest_rate;
interest_rate = interest_rate / 100;
cout<<"\n\nEnter …Run Code Online (Sandbox Code Playgroud) #include <studio.h>
int main() {
std::cout << "apple" << endl << "banana";
}
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
using namespace std;
int main(void) {
cout << "apple" << endl;
cout << "banana" << endl;
}
Run Code Online (Sandbox Code Playgroud)
为什么我错了?我知道答案是第二个,但我想知道为什么我的第一个代码是错误的。请帮我!