2016-07-11 8 views
-4

여기에 나는 사용자의 나이와 얼마나 많은 돈이 있는지 묻는 프로그램을 만들었습니다. 예를 들어 사용자가 500 달러를 받고 50 달러에 배트를 구입했다고 가정 해 보겠습니다. 500 달러를 450 달러로 내려면 어떻게해야합니까?번호를 어떻게 만들 수 있습니까?

#include <iostream> 
using namespace std; 

int main() 
{ 
    int age; 
int money; 
cout << "How old are you?" << endl; 
cin >> age; 
if (age <= 12) { 
    cout << "Not for kids. Sorry!" << endl; 
} 
else { 
    cout << "How much money do you have?" << endl; 
} 
cin >> money; 
if (money <= 50) { 
    cout << "Sorry not enough" << endl; 
} 
else { 
    cout << "Here are the items you can buy" << endl; 
    cout << " a = $50 Bat\n b = $100 Beats\n c = $500 Xbox One\n d = $500 PS4\n"; 
} 
int a; 
int b; 
int c; 
int d; 
if (cin >> a) { 
    money -= 50; //This is the part where I tried to make the $500 go down 
} 
cout << "You have " << money << "left" << endl; 

return 0; 
} 

이 내가 $ 500 $ 450 아래로 갈 수 있도록 노력하는 부분입니다 : 여기

코드입니다

또한이 내 프로그램을 만들 수있는 방법이 있는지
if (cin >> a) { 
    money -= 50; //This is the part where I tried to make the $500 go down 
} 
cout << "You have " << money << "left" << endl; 

return 0; 
} 

내 코드와 동일한 출력이지만 짧아서 좋을 것입니다!

+0

'a'라고 입력하셨습니다. 그것이 작동하는 방식이 아닙니다. Btw, 당신이 설정 한 전제 조건은 아무도 아무것도 사지 못하도록 막지 않으며, 실패하면 다음 질문을 인쇄하지 않습니다. 그리고 '돈 <= 50' -'50'만으로 충분하다. – LogicStuff

답변

0

사용자 입력의 선택을 문자로 나타내려면 char으로 읽어보십시오. 그들의 선택에 따라

파견 :

char choice; 
if (cin >> choice) { 
    switch (choice) { 
    case 'a': 
    money -= 50; 
    break; 
    case 'b': 
    money -= 100; 
    break; 
    case 'c': 
    money -= 500; 
    break; 
    case 'd': 
    money -= 500; 
    break; 
    default: 
    std::cerr << "invalid choice"; 
    } 
} else { 
    std::cerr << "invalid choice"; 
} 
+0

사용자가 450 $를 갖고 있고 케이스 d에 들어갔다면? – dreamBegin

+0

그러면 '돈'은'-50'이 될 것입니다. 오류 검사를 추가 할 수는 있지만 간단하게 대답 할 것입니다. –

+0

동생이 이해가 안되는 경우 – dreamBegin

1

당신은 논리적이고 읽을 수 있도록 코드를 수정해야합니다. 나는 이것을 작성한다.

#include <iostream> 
using namespace std; 

int main() 
{ 
int age, money; 
int priceA = 50, priceB = 100, priceC = 500, priceD = 500; 
char choice; 

cout << "How old are you?" << endl; 
cin >> age; 
if (age <= 12) { 
    cout << "Not for kids. Sorry!" << endl; 
    return 0; 
} 

cout << "How much money do you have?" << endl; 
cin >> money; 
if (money <= 50) { 
    cout << "Sorry not enough" << endl; 
    return 0; 
} 

cout << "Here are the items you can buy" << endl; 
cout << " a = $ " << priceA << " Bat" << endl; 
cout << " b = $ " << priceB << " Bat" << endl; 
cout << " c = $ " << priceC << " Bat" << endl; 
cout << " d = $ " << priceD << " Bat" << endl; 

cin >> choice; 

switch (choice) { 
    case 'a': 
    if(money >= priceA) { 
    money -= priceA; 
    break; 
    } else { 
    cout << "You have not enough money!" << endl; 
    return 0; 
    } 
    case 'b': 
    if(money >= priceB) { 
    money -= priceB; 
    break; 
    } else { 
    cout << "You have not enough money!" << endl; 
    return 0; 
    } 
    case 'c': 
    if(money >= priceC) { 
    money -= priceC; 
    break; 
    } else { 
    cout << "You have not enough money!" << endl; 
    return 0; 
    } 
    case 'd': 
    if(money >= priceD) { 
    money -= priceD; 
    break; 
    } else { 
    cout << "You have not enough money!" << endl; 
    return 0; 
    } 
    default: 
    cout << "Wrong input" << endl; 
    return 0; 
} 

cout << "You have " << money << " left" << endl; 

return 0; 
} 
+0

도와 주셔서 감사합니다 –

+0

51 달러를 갖고 있고 값이 음수로 내려가는 것보다 케이스 d를 선택하면 이해가되지 않습니까? – dreamBegin

0

다음은이 질문을 해결하는 올바른 방법입니다.

#include <iostream> 
using namespace std; 

int main() 
{ 
int age, money; 
int priceA = 50, priceB = 100, priceC = 500, priceD = 500; 
char choice; 

cout << "How old are you?" << endl; 
cin >> age; 
if (age <= 12) { 
    cout << "Not for kids. Sorry!" << endl; 
    return 0; 
} 

cout << "How much money do you have?" << endl; 
cin >> money; 
if (money <= 50) { 
    cout << "Sorry not enough" << endl; 
    return 0; 
} 

cout << "Here are the items you can buy" << endl; 
cout << " a = $ " << priceA << " Bat" << endl; 
cout << " b = $ " << priceB << " Bat" << endl; 
cout << " c = $ " << priceC << " Bat" << endl; 
cout << " d = $ " << priceD << " Bat" << endl; 

cin >> choice; 

if(choice=='a' && money >=50) 
{ 
    money-=50; 
} 
if(choice=='b' && money >=100) 
{ 
    money-=100; 
} 
if(choice=='c' && money >=500) 
{ 
    money-=500; 
} 
if(choice=='d' && money>=500) 
{ 
    money-=500; 
} 
cout << "You have " << money << " left" << endl; 

return 0; 
} 
관련 문제