小编Sta*_*yle的帖子

C++随机数猜谜游戏

我必须编写一个程序来运行随机猜谜游戏.游戏是1-100的数字,猜测者有20次尝试,最后应该被问到他们是否想再玩一次.如果猜测者是高或低,还必须有多种打印输出选项.我已经完成了部分程序我知道我仍然需要为打印输出添加其他选项但是现在我的问题是,当我尝试运行我所拥有的它说成功但是然后有一个错误,表示变量"数字"正在使用而未初始化.我不知道该怎么办才能明确初始化它.(我是C++的新手) 我已经更新了我的程序,现在我遇到了另一个问题.我的程序运行,但如果猜测低于它打印的数字太高尝试再次尝试再次尝试但是当数字太高时它只打印太高再尝试.我还注意到,当用户选择再次播放时,尝试计数器不会随游戏重置.最后一件事我必须添加更多的消息,当他们赢,输,并被要求玩另一个游戏,我必须使用随机数来选择他们.所以关于做到这一点的最佳途径的任何建议都会很棒

#include <iostream>
#include <iomanip> 
#include <ctime>
using namespace std;
char chr;

int main()
{
srand(time(NULL));                                           //the function that generates random numbers
int number=rand()%100+1;                                     //the range of the random numbers
int guess;                                                   //The guess is stored here
int tries=0;                                                 //The number of tries stored here
   char answer;                                                 //The answer to the question is stored here
answer='y';                  

while(answer=='y'||answer=='Y') 
{
    while (tries<=20 && answer=='y'|| answer=='Y')
    {
    cout<<"Enter a number between 1 and 100 "<<endl;          //The user is asked for a guess …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
2万
查看次数

编写输入数字1-12的程序并输出相应的月份

我的程序假设提示用户输入数字1-12并输出相应的月份.好的我知道我错过了这个项目的一个非常重要的部分,但是我知道我正在努力弄清楚要使用什么.我是否需要一个包含所有月份名称的字符串?此外,我知道我需要在cout <<"月份"之后放置一些东西.<<必须要去的地方所以答案会打印出来,但我现在还不确定.我也认为我需要有int int = something但不确定它应该是1-12还是monthname.这是我正在编辑的程序,但现在它有一个调试错误,变量"month"正在使用而没有被初始化.那是什么意思?

#include <iostream>
#include <string>
using namespace std;
char chr;
int main()
{

int month;
cout<<"Enter a number from 1-12.";
if (month ==1)
    cout<<"January";
else if (month==2)
    cout<< "February";
else if (month==3)
    cout<<"March";
else if (month==4)
    cout<<"April";
else if (month==5)
    cout<<"May";
else if (month==6)
    cout<<"June";
else if (month==7)
    cout<<"July";
else if (month==8)
    cout<<"August";
else if (month==9)
    cout<<"September";
else if (month==10)
    cout<<"October";
else if (month==11)
    cout<<"November";
else if (month==12)
    cout<<"December";
else if (month>12)
    cout<<"Sorry I need …
Run Code Online (Sandbox Code Playgroud)

c++

-2
推荐指数
1
解决办法
4万
查看次数

标签 统计

c++ ×2