0
我的循环有问题。该程序应该类似于打印出一张 乐透彩票券。用户输入他/她想要的多少组乐透号码。每行都按字母顺序标记,但如果有人想要乐透号码超过十行(字母J),程序应该再次从A开始。我的问题是,如果有人进入10“特大”(或十个任何间隔)被印像这样:在特定时间终止循环的问题
“兆丰”只应再次打印,如果有乐透号码的另一条线。 在“for()”里面的“int main()”是我试图解决这个问题的。
#include <iostream> //I/O
#include <iomanip> //setw
#include <ctime> //seeding srand
#include <string> //size
#define RAND(a,b) (a+rand()% (b-a+1))
#define die(errmsg) {cerr << errmsg << endl; exit(1);}
using namespace std;
/*
Author: Zachary Stow
Date: July/20/15
Homework #5
Objective: To design a program that imitates the print out
of a lottery ticket.
*/
//********************************fillup()********************************
void fillup(int lotto[], int n, int from, int to)
{
void bubble_sort(int x[], int n);
for(int i = 0; i < n; i++)
{
lotto[i] = RAND(from,to);
}
bubble_sort(lotto,5);
}
//*****************************bubble_sort()******************************
void bubble_sort(int x[], int n)
{
for(int i = 0; i < n-1; i++)
{
int temp;
for(int j=i+1; j<n ; j++)
{
if(x[i] > x[j])
{
temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
}
//********************************print()*********************************
void print(int x[], int n)
{
for(int i = 0; i < n; i++)
{
cout << setfill('0') << setw(2) << x[i] <<" ";
}
cout <<" ";
cout << setfill('0') << setw(2) << RAND(1,46);
cout << endl;
}
//********************************isNumber********************************
bool isNumber(string str)
{
for(int i = 0; i < str.size(); i++)
{
if(!isdigit(str[i]))return(false);
}
return(true);
}
//**********************************check*********************************
void check(int argc, char **argv)
{
bool isNumber(string);
if(argc != 2)die("usage: megaMillion number_tickets");
string num_tickets = argv[1];
if(!isNumber(num_tickets))die("Not a digit."); //removed num_tickets for now
int num;
num = atoi(num_tickets.c_str());
if(num <= 0)die("Zero or negative number."); //doesnt work
}
//*********************************printmega()****************************
void printmega(int letter)
{
if(letter == 65)
{
cout << endl;
cout <<" Mega" << endl; //10 you get a mega
}
}
//*********************************main()*********************************
int main(int argc, char **argv)
{
void fillup(int x[], int n, int from, int to);
void print(int x[], int n);
void check(int argc, char **argv);
void printmega(int letter);
check(argc, argv);
srand(time(NULL));
cout <<" Mega" << endl;
int letter = 65;
for(int i = 0; i < atoi(argv[1]); i++)
{
if(i == atoi(argc[1]))cout << "Hi"; //my attempt to stop the loop from printing
//only mega after J
cout <<(char)letter++; //when theres no more lines
cout <<" ";
if(letter == 75)letter = 65;
int lotto[5];
fillup(lotto,5,1,56);
print(lotto,5);
printmega(letter);
}
return(0);
}
发布时尝试创建一个仍然可以演示您的问题的最小示例。在这个问题中,我们并不需要知道你是如何实现你的冒泡排序的。 – ilent2
这似乎与在由逗号分隔的列表中输出元素的问题类似。天真的方法要么打印逗号,空格和元素,要么打印元素,逗号和空格。第一个元素不好,因为第一个元素之前有一个逗号。第二,因为在最后一个元素之后有一个逗号。在这种情况下,修正是打印一个逗号和一个空格_IF_这不是第一个元素,_THEN_只打印一个元素。这也是很自然的,也是我们在纸上列出清单的方式。我想,同样的方法可以解决您的问题。 ;) – enhzflep
请注意,在“尝试停止循环”中有一个拼写错误argc是一个整数,您可能意味着argv [1]或者只是写'if((i%10)== 0)...'十是你的程序中的固定限制。 –