将数组传递到C++中的函数?[彩票号码] 我正在用C++制作一个彩票游戏。 我创建了一个名为GetLottoPicks的函数,它要求用户输入7个数字。 我有另一个函数叫做GenWinNums,它可以生成7个随机数
问题是,它会连续7次生成以下内容:-858993460 我相信我对WinningNums[]的论点做出了贡献,但显然不是吗 多谢各位将数组传递到C++中的函数?[彩票号码] 我正在用C++制作一个彩票游戏。 我创建了一个名为GetLottoPicks的函数,它要求用户输入7个数字。 我有另一个函数叫做GenWinNums,它可以生成7个随机数,c++,arrays,C++,Arrays,问题是,它会连续7次生成以下内容:-858993460 我相信我对WinningNums[]的论点做出了贡献,但显然不是吗 多谢各位 int main() { string name; int choice; int user_picked[7]; int chosen_lotto[7]; cout << "LITTLETON CITY LOTTO MODEL:" << endl; cout << "---------------------------"
int main()
{
string name;
int choice;
int user_picked[7];
int chosen_lotto[7];
cout << "LITTLETON CITY LOTTO MODEL:" << endl;
cout << "---------------------------" << endl;
cout << " 1) Play Lotto " << endl;
cout << " Q) Quit Program " << endl;
cout << " Please make a selection: ";
cin >> choice;
if (choice == 1)
{
cout << "\n Please enter your name: ";
cin >> name;
getLottoPicks(user_picked);
GenWinNums(chosen_lotto);
for(int i = 0; i < 7; i++)
{
cout << chosen_lotto[i];
}
for(int i = 0; i < 7; i++)
{
cout << user_picked[i];
}
}
else if (choice == 'Q' || 'q')
{
exit(0);
}
else
{
cout << "That is not a valid choice." << endl;
}
system("PAUSE");
return 0;
}
int getLottoPicks(int UserTicket[])
{
const int amount = 7;
UserTicket[amount];
int ticket = UserTicket[amount];
for (int i = 0; i < amount; i++)
{
cout << "\n Please enter number " << i + 1 << ":";
cin >> UserTicket[i];
while (UserTicket[i] < 1 || UserTicket[i] > 40)
{
cout << "Your selection cannot be less than 1 and/or greater than 40. Try Again." << endl;
cin >> UserTicket[i];
}
if(i > 0)
{
for(int check = 0; check < i; check++)
{
while(UserTicket[i] == UserTicket[check])
{
cout << "You cannot use the same lotto number twice. Try again: " << endl;
cin >> UserTicket[i];
}
}
}
}
return ticket;
}
int GenWinNums(int WinningNums[])
{
const int amount = 7;
int numbers[amount];
int ticket = WinningNums[amount];
for(int i = 0; i < amount; i++)
{
numbers[i] = (rand() % 40) + 1;
while(numbers[i] < 1 || numbers[i] > 40)
{
numbers[i] = (rand() % 10) + 1;
}
if(i > 0)
{
for(int check = 0; check < i; check++)
{
while(numbers[i] == numbers[check])
{
numbers[i] = (rand() % 10) + 1;
}
}
}
}
return ticket;
}
看起来您从未在例程中为WinningNums数组赋值,这可能解释了为什么您总是得到相同的数字
我建议删除数字数组并直接使用WinningNums生成随机数。看起来您在例程中从未为WinningNums数组赋值,这可能解释了为什么您总是得到相同的数字
我建议删除数字数组,直接使用WinningNums生成您的随机数。为了好玩,这里有大量修复方法。我不打算一一列举。基本上: 通过引用传递固定大小的数组,以避免不安全的使用 严格检查输入错误,并在单个循环中执行 几个错误的数组分配,要么什么也不做,要么阻止调用者看到结果。。。 使用char作为输入类型,这样Q将永远得到匹配,并且更喜欢aa切换而不是重复的ifs 下面是一个更典型的C++风格解决方案,包括赢或输的检查 读者练习:为你的兰德生成器添加种子,或者在上一张中奖彩票中粘贴此彩票非常简单: 演示运行的输出还显示了错误处理:
LITTLETON CITY LOTTO MODEL:
---------------------------
1) Play Lotto
Q) Quit Program
Please make a selection: 1
Please enter your name: me
Please enter number 1:34
Please enter number 2:7
Please enter number 3:16
Please enter number 4:18
Please enter number 5:24
Please enter number 6:24
Duplicate entry 24
Please enter number 6:7
Duplicate entry 7
Please enter number 6:37
Please enter number 7:whoops
Bad input 'whoops' discarded
Please enter number 7:what was the last number again?
Bad input 'what' discarded
Please enter number 7:Bad input 'was' discarded
Please enter number 7:Bad input 'the' discarded
Please enter number 7:Bad input 'last' discarded
Please enter number 7:Bad input 'number' discarded
Please enter number 7:Bad input 'again?' discarded
Please enter number 7:27
User ticket: 7; 16; 18; 24; 27; 34; 37;
Winning ticket: 7; 16; 18; 24; 27; 34; 36;
Result of draw: WINNER
更典型的C++解决方案
只是为了好玩,这里有大量的补丁。我不打算一一列举。基本上: 通过引用传递固定大小的数组,以避免不安全的使用 严格检查输入错误,并在单个循环中执行 几个错误的数组分配,要么什么也不做,要么阻止调用者看到结果。。。 使用char作为输入类型,这样Q将永远得到匹配,并且更喜欢aa切换而不是重复的ifs 下面是一个更典型的C++风格解决方案,包括赢或输的检查 读者练习:为你的兰德生成器添加种子,或者在上一张中奖彩票中粘贴此彩票非常简单: 演示运行的输出还显示了错误处理:
LITTLETON CITY LOTTO MODEL:
---------------------------
1) Play Lotto
Q) Quit Program
Please make a selection: 1
Please enter your name: me
Please enter number 1:34
Please enter number 2:7
Please enter number 3:16
Please enter number 4:18
Please enter number 5:24
Please enter number 6:24
Duplicate entry 24
Please enter number 6:7
Duplicate entry 7
Please enter number 6:37
Please enter number 7:whoops
Bad input 'whoops' discarded
Please enter number 7:what was the last number again?
Bad input 'what' discarded
Please enter number 7:Bad input 'was' discarded
Please enter number 7:Bad input 'the' discarded
Please enter number 7:Bad input 'last' discarded
Please enter number 7:Bad input 'number' discarded
Please enter number 7:Bad input 'again?' discarded
Please enter number 7:27
User ticket: 7; 16; 18; 24; 27; 34; 37;
Winning ticket: 7; 16; 18; 24; 27; 34; 36;
Result of draw: WINNER
更典型的C++解决方案
信不信由你,你不知道如何使用数组!您调用了WinningNums[amount],其中amount=7,数组的大小为7!出界了!好吧,也许这与问题无关。这是关于你需要知道的。信不信由你,你不知道如何使用数组!您调用了WinningNums[amount],其中amount=7,数组的大小为7!出界了!好吧,也许这与问题无关。这是你需要知道的,GenWinNums会生成7个介于1和40之间的随机数。您看到的其他两个循环让您感到困惑,您只需确保没有重复的循环,并且它们不大于40且小于1。WinningNums[]参数是否应该包含计算后的7个随机数?是的,它应该包含生成的7个数字。好,然后,在某个时候,您需要分配给该数组的元素,即WinningNums[i]=numbers[i],或者干脆去掉numbers数组,直接使用WinningNums参数。很好,我会试试。非常感谢您的帮助。GenWinNums会生成7个介于1和40之间的随机数。您看到的其他两个循环让您感到困惑,您只需确保没有重复的循环,并且它们不大于40且小于1。WinningNums[]参数是否应该包含计算后的7个随机数?是的,它应该包含生成的7个数字。好,然后,在某个时候,您需要分配给该数组的元素,即WinningNums[i]=numbers[i],或者干脆去掉numbers数组,直接使用WinningNums参数。很好,我会试试。非常感谢你的帮助。
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <set>
const size_t amount = 7;
typedef std::set<int> ticket_t;
ticket_t getLottoPicks()
{
ticket_t ticket;
while (ticket.size() < amount)
{
std::cout << "\n Please enter number " << (ticket.size()+1) << ":";
int entry;
if(std::cin >> entry)
{
if(entry >= 1 && entry <= 40)
{
if (ticket.end() != ticket.find(entry))
std::cout << "Duplicate entry " << entry;
else
ticket.insert(entry);
} else
std::cout << "Entry out of range [1..40]: " << entry;
continue;
}
std::cin.clear();
std::string discard;
std::cin >> discard;
std::cout << "Bad input '" << discard << "' discarded";
}
return ticket;
}
ticket_t genWinNums()
{
ticket_t ticket;
while (ticket.size() < amount)
ticket.insert(rand() % 40 + 1);
return ticket;
}
std::ostream& operator<<(std::ostream& os, ticket_t const& t)
{
std::copy(t.begin(), t.end(), std::ostream_iterator<int>(os, "; "));
return os;
}
int main()
{
std::cout << "LITTLETON CITY LOTTO MODEL:" << std::endl;
std::cout << "---------------------------" << std::endl;
std::cout << " 1) Play Lotto " << std::endl;
std::cout << " Q) Quit Program " << std::endl;
std::cout << "Please make a selection: ";
char choice;
std::cin >> choice;
switch(choice)
{
case '1':
{
std::cout << "\n Please enter your name: ";
std::string name;
std::cin >> name;
const ticket_t user = getLottoPicks();
const ticket_t winning = genWinNums();
std::cout << "User ticket: " << user << "\n";
std::cout << "Winning ticket: " << winning << "\n";
// check win?
bool ok = 0 == std::lexicographical_compare(
user.begin(), user.end(),
winning.begin(), winning.end());
std::cout << "Result of draw: " << (ok? "WINNER":"No luck") << "\n";
}
case 'Q': case 'q':
return 0;
default:
std::cout << "\n That is not a valid choice." << std::endl;
}
std::cin.ignore(1<<24,'\n');
std::cout << "\n Press enter...";
std::string dummy;
std::getline(std::cin, dummy);
}