巧克力游戏:输入循环,不插入循环或打印游戏统计数据
Chocolate Game: input loops without inserting loop or printing game stats
我一直在尝试做一个Chocolate Manager游戏(更具体地说是一个控制台测试),它在我输入命令后就循环了!忽略命令值cin和幸福。。。我不知道幸福会发生什么,因为它甚至都没有表现出来!
**Main.cpp**
// Include Space
//======
#include <iostream>
#include "timer.cpp"
#include <string>
#include "clrscr.cpp"
//======
using namespace std;
int main() {
bool exit = false;
int numchocolate = 15;
double money = 25;
string commandname;
string commandvalue;
string placeholder;
double happiness;
int cmdvl;
while (exit == false) {
commandname = ""; //Empties the variables so that they can be reused.
commandvalue = ""; //
cout << "Chocolate: " << numchocolate << "nMoney:" << money << "nCommand:"; //stats and initial prompt
cin >> commandname; //Input for command name
cout << "nCommand value:"; //Second prompt
cin >> commandvalue; //Input for command's value
cmdvl = StringToNumber(&commandvalue); //Converts the string into a int. But this always fails for some reason....
//Wait! I found the problem... wait, I don't know why it's caused so let's go on find more problems :3
if (cmdvl == -1) {
cout << "Something gone wrong in conversion! Exiting...";
return 1;
}
if (commandname == "buy n chocolates") {
numchocolate += buyChocolate(money, cmdvl);
}
else if (commandname == "eat n chocolates") {
numchocolate -= cmdvl;
happiness += cmdvl * 2.5;
}
else if (commandname == "go work n hours") {
money += cmdvl * 2;
happiness -= cmdvl / 3.5;
}
else if (commandname == "exit") {
exit = true;
}
else {
cout << "nInvalid command! Happiness penalty!n";
}
if (happiness > 101.0) {
happiness = 101.0;
}
if (happiness > 1.0) {
happiness -= 1.0;
}
cout << "nNow you're " << happiness << "% happy.n";
cout << "Press Enter to continue to next simulation cycle.";
placeholder = "";
getline(cin, placeholder);
ClearScreen();
}
return 0;
}
Timer.cpp(我知道,与定时器无关)
// Include Space
//======
#include <string>
#include <sstream>
//======
using namespace std;
int buyChocolate(double money, int amount) {
if (money > amount * 3.5) {
return amount;
}
else if (money == 0) {
return 0;
}
else {
double newAmont = money / 3.5;
return (int) (newAmont);
}
return -1;
}
int StringToNumber ( const string * sometxt ) //Why do you always fail? :(
{
stringstream ss(*sometxt); //A string stream declarer and initializer. Nothing much.
int result; //Results are good :)
return ss >> result ? result : -1; //Returns the int of the string. If it isn't int-compatible returns the error int -1.
}
clrscr.cpp
#include <windows.h>
void ClearScreen(){
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };
hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
if (hStdOut == INVALID_HANDLE_VALUE) return;
/* Get the number of cells in the current buffer */
if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
cellCount = csbi.dwSize.X *csbi.dwSize.Y;
/* Fill the entire buffer with spaces */
if (!FillConsoleOutputCharacter(
hStdOut,
(TCHAR) ' ',
cellCount,
homeCoords,
&count
)) return;
/* Fill the entire buffer with the current colors and attributes */
if (!FillConsoleOutputAttribute(
hStdOut,
csbi.wAttributes,
cellCount,
homeCoords,
&count
)) return;
/* Move the cursor home */
SetConsoleCursorPosition( hStdOut, homeCoords );
}
它的主要目标是增加幸福感,同时避免破产或失去巧克力。祝你好运(修复和播放)!:3
哦,感谢谁制作了清晰的屏幕代码,因为它不是我的,我忘记了是谁制作的……我想在发布简单游戏的控制台版本之前避免版权问题,我不知道如何:(
代码也更新了,但字符串到int的转换不起作用。我认为调试它不会有帮助,因为它就像一个明显的4行函数,我已经检查了一千次了!
我建议您使用调试器仔细检查代码,并设置断点,以查看它到底在哪里停止以及会导致什么错误。
顺便说一句,你忘了这里有一个分号(;):
if (cmdvl == -1) {
cout << "Something gone wrong in conversion! Exiting..." <-
return 1;
}
从使用getline开始,cin读取第一个白色字符:
cout << "Chocolate: " << numchocolate << "nMoney:" << money << "nCommand:";
getline(cin,commandname);
cout << "nCommand value:";
getline(cin,commandvalue);
但数学似乎也失败了。
Now you're 1.4822e-323% happy.
相关文章:
- 我的代码运行良好,但在游戏循环中中断
- 在定义字符数组(井字游戏)的 for 循环中应用输入限制
- 为什么我的 while 循环永远不会在 C++ 中的数字猜测游戏中结束?
- 如何简化多个类的游戏循环
- Craps游戏在循环不满足退出条件时执行
- 在游戏循环中创建类对象
- C++ do/while 循环和 if/else: 骰子游戏 -- 第 75 行错误:"Cout"之前的预期"While" -- 如何解决这个问题?
- 如何在主游戏循环中使用 sfml 只播放一次声音
- 如何在 SFML C++游戏中将资产加载与主循环分开
- 需要井字游戏中的 for 循环帮助
- Win32 Sleep()游戏循环中的精度
- 如何退出这个Tictactoe游戏循环
- 如何防止无限循环游戏中的堆栈溢出
- 跳跃游戏中嵌套的循环问题
- 在游戏更新循环中计算布尔的真/假变化
- 为什么要对固定时间步的游戏循环使用集成?(游戏上的头目)
- Qt在游戏循环中短时间内停止关键事件
- 绘图线在SDL2 C 中闪烁.如何修改我的游戏循环
- 简单的游戏,同时循环和检查字符串
- 如何循环游戏追踪角色