CPP-方形空心-怎么了
CPP- Square Hollow- What is the mistake?
有人能帮我吗?我担心这段代码,它在第一轮编译时编译得很好,但当它循环时,它不会正确打印出来。我找不到错误。。非常感谢。当我第一次用Xcode编译它时,它给出了右边的正方形空心,但当我第二次输入时,它没有打印出任何正方形。
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;
#include <cstdlib>
int main()
{
int a;
int b;
int num=0;
string buf;
//initialize a and b
a = 1;
b = 1;
// ask user to repeat the process again at end of the first promt
while( true )
{
cout << "Please enter size of square between #1-20: n";
cin >> buf; num = atoi (buf.c_str());
cin.ignore(1000, 10);
if( num < 1 || num > 20 )
break;
//process of printing square
while ( num >= a)
{
b = 1;
while ( num >= b )
{
if ( a == 1 || a == num || b == 1 || b == num )
cout << "*";
else
cout << " ";
b++;
}
cout << endl;
a++;
}
}
}
您没有重置a和b变量,您需要将这些行放入
//initialize a and b
a = 1;
b = 1;
在while(true)
循环内部的开始
#include<iostream>
#include<conio>
main()
{
int squareHeight, squareWidth;
cout<< "Enter Height: ";
cin>> squareHeight;
cout<< "Enter Widht: ";
cin>> squareWidth;
for(int width=1; width<=squareHeight; width++)
{
if(width <= 1)
for(int width=1; width<=squareWidth; width++)
{
cout<< "*";
}
else if(width<squareHeight)
{
cout<< endl;
for(int width2=1; width2<=squareWidth; width2++)
{
if(width2==1 || width2==squareWidth)
cout<< "*";
else
cout<< " ";
}
}
else
{
cout<< endl;
for(int width3=1; width3<=squareWidth; width3++)
{
cout<<"*";
}
}
}
getch ();
}
相关文章:
- 不断收到相同的错误...怎么了?C++
- 编译器没有显示错误,但我没有得到任何输出.怎么了?
- qsort 不适用于我的 impl 中的浮点类型。怎么了?
- 在C++中表现不佳.怎么了?
- 我的程序怎么了?(C )
- 这里怎么了?单位转换器
- 错误:未定义对 .. 的引用.怎么了
- 我试图编写一个代码来查找一个单词是否是回文,但它不起作用。怎么了?
- 我在C++文件中的简单代码块上有 2 个错误。错误 C2059:语法错误:"?"。第二智能感知:预期表达式。怎么了?
- 我的代码怎么了?带有QT的原始输入API
- 使用 cin.get() 似乎没有读取我期望的字符。怎么了?
- 当其中一个字符串来自cin时,无法比较两个字符串.这里怎么了
- "5"怎么了?纸上一切都很好
- 我的cout怎么了
- C++:腹肌怎么了
- Qt OpenGL with Widgets:怎么了?
- 程序为某些条目提供了错误的输出!怎么了?
- 自动和复制构造函数:怎么了?
- 怎么了???只有七行
- CPP-方形空心-怎么了