내 프로그램이 작동하지 않아 마커 (goto)로 이미 시도해 보았습니다. 하지만 작동하지 않습니다. 또한 루프를 개선하려고 시도했지만 무한 루프로 끝납니다. 도와 주셔서 감사합니다! C++ 루프 (삼각형 서랍)
#include <iostream> //includes
using namespace std;
int makedreieck(int länge) // function for drawing a triangle
{
int rows=0; //rows
int draw=0; //drawed
while(draw <= länge)
{
draw++;
cout << "*";
if(draw == länge-1)
{
rows++;
draw = länge-rows;
cout << endl;
}
if(draw == 1)
{
getchar();
return 0;
}
}
}
int main(char argument)
{
int dreieck;
cin >> dreieck;
makedreieck(dreieck);
getchar();
return 0;
}
마지막으로 모든 작업을 가지고 ... 사람이 코드를 필요로하는 경우 : 첫 번째 반복에
#include <iostream>
#include <time.h>
#include <windows.h>
#include <tchar.h>
using namespace std;
int createtriangle(int length, bool custom, char symbol)
{
clock_t start, finish;
length++;
int OK=0;
int OK_ABORT;
int TRIANGLE_NOT_ALLOWED;
enum returnvalues{OK, OK_ABORT, TRIANGLE_NOT_ALLOWED};
if(length<=0)
{
cerr << "Dreieck mit einer Laenge von " << length-1 <<" unerlaubt.";
return TRIANGLE_NOT_ALLOWED;
}
if(length>81)
{
cout << "Es wurde eine ueber 80 Sternchen lange Treppe entdeckt." << endl << "Es wird empfohlen die Operation abzubrechen"<< endl << "1: abbrechen 2: weiter"<<endl<<"Auswahl: ";
int sel;
cin >> sel;
getchar();
if(sel>2)
{
cout << "Es gibt die Auswahl: "<<sel<<" nicht.";
}
system("cls");
switch(sel)
{
case 1:
return OK_ABORT;
break;
case 2:
break;
}
}
int rows=0;
int draw=0;
start = clock();
while(draw <= length)
{
draw++;
if(rows>=1&&custom==false)
{
cout << "*";
}
if(rows>=1&&custom==true)
{
cout << symbol;
}
if(draw == length)
{
if(rows>=1)
{
cout << endl;
}
rows++;
draw = length-rows;
}
if(rows == length)
{
finish=clock();
if(custom==false)
{
cout << "Es wurde eine "<<length-1<<" Sternchen lange Treppe gebaut."<<endl;
}
else if(custom==true)
{
cout << "Es wurde eine "<<length-1<<" "<<symbol<<" lange Treppe gebaut."<<endl;
}
cout << "Es wurden "<< static_cast<double>(finish - start) /CLOCKS_PER_SEC<< " Sekunden gebraucht.";
return OK;
}
}
return OK;
}
int main()
{
START:
system("cls");
int dreieck;
char time[9];
cout << "Wie gross soll die Treppe sein?: ";
cin >> dreieck;
system("cls");
cout << "Soll ein custom symbol verwendet werden?"<<endl<<"Ja:1 Nein:2"<<endl<<"Auswahl: ";
int menu2;
cin >> menu2;
system("cls");
bool customtrue;
char symbolcustom;
if(menu2>2)
{
cout << "Die Auswahl: " << menu2 << " ist nicht verfügbar";
getchar();
goto START;
}
switch(menu2)
{
case 1:
customtrue = true;
cout << "Symbol angeben(1Char max): ";
cin >> symbolcustom;
break;
case 2:
customtrue = false;
break;
}
system("cls");
createtriangle(dreieck,customtrue,symbolcustom);
getchar();
getchar();
menumark:
system("cls");
cout << "Soll erneut eine Treppe gebaut werden?"<<endl<<"1: Ja 2: Nein"<<endl<<"Auswahl: ";
int menu;
cin >> menu;
if(menu>2)
{
cout <<"Es gibt keine "<<menu<<" Auswahl.";
getchar();
goto menumark;
}
switch(menu)
{
case 1:
goto START;
break;
case 2:
exit(0);
break;
}
return 0;
}
무엇이 작동하지 않습니다. 디버그 했니? –
여기에 대해서는별로 중요하지 않습니다. 어떤 출력을 기대합니까? 그리고 어떤 출력을 얻고 있습니까? –
'makedreieck'에'return 0; '이 하나 있습니다; 당신은 끝에 하나도 가지고 있지 않습니다. 함수는 반환 값을 무시하기 때문에'void'를 반환해야하지만 값을 반환하지 않는 함수에 대해서는 컴파일러 경고를 무시하지 않아야합니다. 여기에 코드를 게시하기 전에 컴파일러 경고가 수정되어야합니다. (컴파일러가 경고를하지 않으면 경고를 켜는 방법을 찾고, 컴파일러가 경고를 할 수없는 경우 더 나은 컴파일러를 얻으십시오.) 이것은 문제와는 아무런 관련이 없습니다. 'makedreieck()'의'getchar()'은 무엇일까? 그것은 불필요한 것처럼 보입니다. –