初学者C ++项目。需要帮忙 。打印平行四边形。 [英] Beginner C++ project . Need help . Printing a parallelogram .

查看:104
本文介绍了初学者C ++项目。需要帮忙 。打印平行四边形。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们。初学者C ++编码器在这里。我有一个项目接受一定数量的行并使用循环打印平行四边形。有人可以帮忙吗?



我尝试过:



< pre lang =c ++> #include < < span class =code-leadattribute> iostream.h >

int GetNRows( int &);
int NumSpaces( int int );
int NumStars( int int );
void PrintChars( int char );
void DrawShape( int );

int main()
{
int nRows,maxC,space,ast,n,row;
char ch = ' ';


for int row = 1 ;行< = nRows;行++) //
{
if (row< = 1
{
GetNRows(nRows);
}
n = NumSpaces(nRows,row);
nRows = NumStars(nRows,row);
PrintChars(n,ch);
DrawShape(nRows);
}
return 0 ;
}

int GetNRows( int & nRows)
{
int maxC;
cout<< 输入行数(3-23):<<冲洗;
cin>> NROWS;

while (nRows< 3
{
cout<< 对于我来说,制作平行四边形的行太少了。
<< ENDL;
cout<< 输入行数(3-23):<<冲洗;
cin>> NROWS;
}
while (nRows> 23
{
cout<< 这对我来说太多行来制作平行四边形。
<< ENDL;
cout<< 输入行数(3-23):<<冲洗;
cin>> NROWS;
}
如果(nRows% 2 == 0
{
nRows ++;
}
}

int NumSpaces( int nRows, int 行)
{
int maxC =(nRows / 2 ),空格;
if (row< = maxC)
{
space = maxC - row;
}
其他
{
space = 0 ;
}
返回空间;
}

int NumStars( int nRows, int 行)
{
int ast,maxC =(nRows / 2 );
if (row< = maxC)
{
ast = row;
}
else
{
ast = nRows - row;
}
cout<< AST;
return ast;
}

void PrintChars( int n, char ch)
{
for int s = 0 ; s< n; s ++)
{
cout<< CH;
}
}

void DrawShape( int nRows)
{
int ast;
for int a = 0 ; a< nRows; a ++)
{
cout<< ' *';
}
cout<< ' \ n';
}





不起作用。

输出为1 *

解决方案

您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你接近一个bug。


Quote:

int nRows ,maxC,space,ast,n,row;

char ch ='';





for(int row = 1; row< = nRows ; row ++)// rows

您正在使用未初始化的变量,即 nRows


Hey guys . Beginner C++ coder here . I have a project to accept a certain number of rows and print a parallelogram using Loops . Can anybody help ??

What I have tried:

#include <iostream.h>

int GetNRows(int &);
int NumSpaces( int, int);
int NumStars( int, int);
void PrintChars( int, char);
void DrawShape(int);

int main()
{
	int nRows, maxC, space, ast, n, row;
	char ch = ' ';


	for(int row = 1;row <= nRows;row++) //rows
	{
		if(row <= 1)
		{
			GetNRows( nRows );
		}
		n = NumSpaces(nRows, row );
		nRows = NumStars( nRows, row );
		PrintChars( n, ch );
		DrawShape( nRows );
	}
	return 0;
}

int GetNRows( int &nRows )
{
	int maxC;
	cout << "Enter number of rows(3-23): " << flush;
	cin >> nRows;

	while(nRows < 3)
	{
		cout << "That is too few rows for me to make a parallelogram."
		<< endl;
		cout << "Enter number of rows(3-23): " << flush;
		cin >> nRows;
	}
	while(nRows > 23)
	{
		cout << "That is too many rows for me to make a parallelogram."
		<< endl;
		cout << "Enter number of rows(3-23): " << flush;
		cin >> nRows;
	}
	if(nRows % 2 == 0)
	{
		nRows++;
	}
}

int NumSpaces( int nRows, int row )
{
	int maxC = (nRows / 2), space;
	if(row <= maxC)
	{
		space = maxC - row;
	}
	else
	{
		space = 0;
	}
	return space;
}

int NumStars( int nRows, int row )
{
	int ast, maxC = (nRows / 2);
	if(row <= maxC)
	{
		ast = row;
	}
	else
	{
		ast = nRows - row;
	}
	cout << ast;
	return ast;
}

void PrintChars( int n, char ch )
{
	for(int s = 0; s < n; s++)
	{
		cout << ch;
	}
}

void DrawShape( int nRows )
{
	int ast;
	for(int a = 0; a < nRows; a++)
	{
		cout << '*';
	}
	cout << '\n';
}



Doesn't work .
output is 1 *

解决方案

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


Quote:

int nRows, maxC, space, ast, n, row;
char ch = ' ';


for(int row = 1;row <= nRows;row++) //rows

You are using an uninitialized variable, namely nRows.


这篇关于初学者C ++项目。需要帮忙 。打印平行四边形。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆