14. 커피를 주문하는 간단한 C++ 프로그램을 작성해보자. 커피 종류는 "에스프레소", "아메리카노", "카푸치노"의 3가지이며 가격은 각각 2000원, 2300원, 2500원이다. 하루에 20000원 이상 벌게 되면 카페를 닫는다. 실행 결과와 같이 작동하는 프로그램을 작성하라.

...더보기

sol1.

 교재에서 시키는대로 한 풀이

#include <iostream>
using namespace std;
int main() {
	cout << "에스프레소 2000원, 아메리카노 2300원, 카푸치노 2500원입니다." << endl;
	char coffee[100];
	int num,total=0;
	do {
		cout << "주문>>";
		cin >> coffee >> num;
		if (strcmp(coffee, "에스프레소") == 0) {
			total += 2000 * num;
			cout << 2000 * num << "원입니다. 맛있게 드세요" << endl;
		}
		else if (strcmp(coffee, "아메리카노") == 0) {
			total += 2300 * num;
			cout << 2300 * num << "원입니다. 맛있게 드세요" << endl;
		}
		else if (strcmp(coffee, "카푸치노") == 0) {
			total += 2500 * num;
			cout << 2500 * num << "원입니다. 맛있게 드세요" << endl;
		}
		else {
			cout << "잘못 입력하셨습니다." << endl;
		}
	} while (total <= 20000);
	cout << "오늘 " << total << "원을 판매하여 카페를 닫습니다. 내일 봐요~~~";
}

sol2.

#include <iostream>
#include <string>
using namespace std;
int main() {
	cout << "에스프레소 2000원, 아메리카노 2300원, 카푸치노 2500원입니다." << endl;
	string menu;
	int servings,total=0;
	do {
		cout << "주문>>";
		getline(cin, menu, ' '); // 1
		cin >> servings;
		cin.ignore(); // 2
		if (menu == "에스프레소") {
			cout << 2000 * servings << "원입니다. 맛있게 드세요" << endl;
			total += 2000 * servings;
		}
		else if (menu == "아메리카노") {
			cout << 2300 * servings << "원입니다. 맛있게 드세요" << endl;
			total += 2300 * servings;
		}
		else if (menu == "카푸치노") {
			cout << 2500 * servings << "원입니다. 맛있게 드세요" << endl;
			total += 2500 * servings;
		}
		else
			cout << "잘못 입력하셨습니다." << endl;
		
	} while (total <= 20000);
	cout << "오늘 " << total << "원을 판매하여 카페를 닫습니다. 내일 봐요~~~";
}
// 1. 띄어쓰기로 버퍼를 구분한 것
// 2. 버퍼에 남아있는 개행 문자를 지워준다. 지우지 않으면 두번째 입력시 개행 문자가 menu에 같이 남게됨
// string 클래스는 == 연산자가 재정의 되어있다. 연산자 재정의 또한 뒤에서 배우게 된다.

BELATED ARTICLES

more