그래서 여기에 내가 풀려고하는 원래의 질문이 있습니다.별도의 .cpp 파일에서 함수를 호출하는 방법은 무엇입니까?
* Lab5_Figures라는 프로젝트를 만듭니다. 이 프로젝트는 여러 파일을 포함해야합니다. 사용자에게 정사각형, 왼쪽 또는 오른쪽 삼각형 중 하나를 선택하라고 반복적으로 묻는 프로그램을 작성한 다음 그림 크기를 입력 한 다음 적절한 모양을 별 모양으로 인쇄합니다. 사각형의 경우, 프로그램은 사용자가 채워지거나 빈 사각형이 필요한지 여부를 묻습니다. 사용자가 잘못된 옵션을 입력하면 프로그램을 종료해야합니다.
- 평방
- 왼쪽 아래 삼각형
- 오른쪽 삼각형
선택 그림 : 1
선택 크기 : 4
작성 또는 중공 예를 들어 아래 대화보기 [f/h] : h
// 것은 다음
- 평방
- 왼쪽 아래 삼각형
- 오른쪽 삼각형 ...
당신은 반복 실험실에서 코드를 재사용 할 수 있습니다 (I 반복 적절한 그림을 인쇄 벌써 했어.) 별 인쇄 코드를 filledSquare, hollowSquare, leftTriangle, rightTriangle의 네 가지 기능으로 배치하십시오. 각 함수는 단일 정수 매개 변수를 받아 들여야합니다. 즉, Figure의 크기와 값을 반환하지 않습니다 (void 함수 임). figure.cpp, figures.h 및 figuresInput.cpp라는 세 개의 개별 파일을 만듭니다. Figures.cpp에 삼각형 및 사각형 함수 정의를 배치하고 figures.h에 프로토 타입을 배치합니다. 헤더 파일이 다중 포함되지 않도록 보호되어 있는지 확인하십시오. FiguresInput.cpp *에 main 함수를 추가하십시오.
좋습니다. 이제 내 파일들입니다.
이 figuresInput.cpp
// This program creates shapes based on user input
#include <iostream>
#include <string>
#include "figures.h"
using std::cin; using std::cout; using std::string; using std::endl;
int main()
{
int option = 1;
while (option == 1 || option == 2 || option == 3)
{
//determine choice
cout << "1. Square" << endl << "2. Left Triangle" << endl << "3. Right Triangle" << endl;
cout << "Select an option: ";
cin >> option;
if (option == 1)
{
char fillHollow;
cout << "Filled or hollow? [f/h]";
cin >> fillHollow;
if (fillHollow == 'f')
{
int size;
cout << "Input Size: ";
cin >> size;
void filledSquare(int size);
}
else if (fillHollow = 'h')
{
int size;
cout << "Input Size: ";
cin >> size;
void hollowSquare(int size);
}
}
else if (option == 2)
{
int size;
cout << "Input Size: ";
cin >> size;
void leftTriangle(int size);
}
else if (option == 3)
{
int size;
cout << "Input Size: ";
cin >> size;
void rightTriangle(int size);
}
else
exit(EXIT_FAILURE);
}
} //end main
figures.cpp
//function defintions
#include "figures.h"
#include <iostream>
using std::cin; using std::cout; using std::string; using std::endl;
void filledSquare(int a)
{
//print stars for first square
for (int b = 0; b < a; b++)
{
for (int c = 0; c < a; c++)
cout << "*";
cout << endl; //new line
}
cout << endl; //new line
} //end
void hollowSquare(int a)
{
for (int b = 0; b < a; b++)
{
int spaces = a - 2;
if (b == 0 || b == (a - 1))
{
for (int i = 0; i < a; i++)
cout << "*";
cout << endl; //new line
}
else
{
cout << "*";
for (int i = 0; i < spaces; i++)
cout << " ";
cout << "*";
cout << endl; //new line
}
}
} //end
void leftTriangle(int a)
{
//get user input and print stars for first triangle
for (int b = a; b < a; b--)
{
for (int c = 0; c < b; c++)
cout << "*";
cout << endl; //new line
}
cout << endl; //new line
} //end
void rightTriangle(int a)
{
//get user input and print stars for second triangle
for (int b = 0; b < a; b++)
{
int stars = a - b;
for (int i = 0; i < b; i++)
cout << " ";
for (int i = 0; i < stars; i++)
cout << "*";
cout << endl; //new line
}
cout << endl; //new line
} //end
마지막
및 figures.h (내 포맷이 꺼져 있으면 나는 :(사과)
//funtion prototypes
#ifndef FIGURES_H
#define FIGURES_H
void filledSquare(int);
void hollowSquare(int);
void leftTriangle(int);
void rightTriangle(int);
#endif;
그래, 내 문제는 내가 main에서 함수 정의를 올바르게 호출하지 않는다고 생각한다. 내가 딱 맞는 것을 포함시키지 않았는지 나는 확신 할 수 없다. 내가 얻을 수있는 도움에 정말 감사 할 것입니다.
내 결과는 다음과 같습니다.
1. Square
2. Left Triangle
3. Right Triangle
Select an option: 1
Filled or hollow? [f/h]f
Input Size: 4
1. Square
2. Left Triangle
3. Right Triangle
Select an option:
[MCVE]에 축 어적 오류 메시지를 넣어주세요. –
문제가 무엇인지 잘 모르겠습니다. 함수는 오류나 경고없이 컴파일하고 모든 것을 잘 인쇄합니다. 루프가 올바르게 반복되어 입력을 올바르게 선택합니다. 그러나 결코 모양을 인쇄하지 않습니다. – Inert
이러한 문제를 해결하는 올바른 도구는 디버거입니다. 스택 오버플로를 묻기 전에 코드를 단계별로 실행해야합니다. 자세한 도움말은 [작은 프로그램 디버깅 방법 (Eric Lippert 작성)] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)을 참조하십시오. 문제를 재현하는 [최소, 완료 및 확인 가능] (http://stackoverflow.com/help/mcve) 예제와 함께 해당 질문을 \ [편집]해야합니다. 디버거. –